Skip to content

Instantly share code, notes, and snippets.

@davidandrzej
davidandrzej / grasshopper.m
Created March 1, 2017 15:45
Code for "Improving Diversity in Ranking using Absorbing Random Walks" (NAACL HLT 2007)
function [l v] = grasshopper(W, r, lambda, k)
%
% Reranking items by random walk on graph with absorbing states.
% (CREDIT: Jerry Zhu [email protected])
%
% INPUT
%
% W: n*n weight matrix with non-negative entries.
% W(i,j) = edge weight for the edge i->j
% The graph can be directed or undirected. Self-edges are allowed.

Keybase proof

I hereby claim:

  • I am davidandrzej on github.
  • I am davidandrzej (https://keybase.io/davidandrzej) on keybase.
  • I have a public key whose fingerprint is 5203 3009 B157 505D DF21 9C16 B86B BB05 D29E 1BFE

To claim this, I am signing this object:

scala> implicit val em = EvaluationMonoid
em: EvaluationMonoid.type = EvaluationMonoid$@34f5b235
scala> implicit val mm = mapMonoid[String,Evaluation]
mm: scalaz.Monoid[Map[String,Evaluation]] = scalaz.std.MapInstances$$anon$4@13105b09
scala> val dataset1 = Map("modelA" -> Evaluation(3,2),
| "modelB" -> Evaluation(4,1))
dataset1: scala.collection.immutable.Map[String,Evaluation] =
Map(modelA -> Evaluation(3,2), modelB -> Evaluation(4,1))
val evalGen = for {total <- Gen.choose(0, 1000);
correct <- Gen.choose(0, total)}
yield Evaluation(total,correct)
"Evaluation Monoid" should {
import EvaluationMonoid._
implicit val eq = Equal.equalA[Evaluation]
"obey Monoid typeclass Law" in {
"Evaluation Monoid" should {
import EvaluationMonoid._
implicit val eq = Equal.equalA[Evaluation]
val testEvaluation = Evaluation(3, 2)
"obey Monoid typeclass Law" in {
Monoid.monoidLaw.leftIdentity(testEval) should be (true)
Monoid.monoidLaw.rightIdentity(testEval) should be (true)
}
object EvaluationMonoid extends Monoid[Evaluation] {
def zero = Evaluation(0,0)
def append(x: Evaluation, y: => Evaluation) =
Evaluation(x.total + y.total, x.correct + y.correct)
}
case class Evaluation(total: Int, correct: Int)
@davidandrzej
davidandrzej / lawblog-monoidlaw.scala
Last active August 29, 2015 14:04
Excerpted from https://github.com/scalaz/scalaz for instructional purposes
trait Monoid[F] extends Semigroup[F] {
...
trait MonoidLaw extends SemigroupLaw {
def leftIdentity(a: F)(implicit F: Equal[F]) =
F.equal(a, append(zero, a))
def rightIdentity(a: F)(implicit F: Equal[F]) =
F.equal(a, append(a, zero))
}
...
}
def addItUp[F : Monoid](items: Seq[F]): F = {
// Combine a bunch of items
val m = implicitly[Monoid[F]]
items.foldLeft(m.zero){case (total, next) => m.append(total,next)}
}
scala> addItUp(Seq("day ", "after ", "day"))
res1: String = "day after day"
scala> addItUp(Seq(1,2,3))
res2: Int = 6
@davidandrzej
davidandrzej / lawblog-monoid.scala
Created August 3, 2014 02:59
Excerpted from https://github.com/scalaz/scalaz for instructional purposes
trait Monoid[F] {
def zero: F
def append(f1: F, f2: => F): F
}