Closed longouyang closed 8 years ago
discovered while tinkering with the ppaml election stuff:
///fold: var dist0 = { d: 0.47, r: 0.43, u: 0.10, }; var alpha = 200; var step = function(curDist) { var d = curDist.d, r = curDist.r, u = curDist.u; var joiners = uniform(0, 0.2 ) * u; var leaversD = uniform(0, 0.001) * d; var leaversR = uniform(0, 0.001) * r; // assume that the deciders noisily mirror the people who have already decided var joinerSides = dirichlet(T.mul(Vector(_.values(_.omit(curDist, 'u'))), alpha)), joinersD = T.get(joinerSides, 0) * joiners, joinersR = T.get(joinerSides, 1) * joiners; return { d: d - leaversD + joinersD, r: r - leaversR + joinersR, u: u - joiners + leaversR + leaversD } } var evolve = function(dists, steps) { if (steps == 0) { return dists } else { var curDist = _.last(dists), nextDist = step(curDist); return evolve(dists.concat(nextDist), steps - 1) } } var steps = 5; /// var doPoll = function(pref, size) { _.object(_.keys(pref), multinomial({ps: _.values(pref), n: size})) } var KL = function(pProbs, qProbs) { return sum(map2(function(p, q) { p * (Math.log(p) - Math.log(q)); }, pProbs, qProbs)) } var pollsDist = Infer( {method: 'SMC', particles: 300}, function() { var path = evolve([dist0], 5); var poll0 = doPoll(path[0], 100); var poll3 = doPoll(path[3], 100); var goodness0 = -1 * KL(_.values(poll0), _.values(_.last(path))); var goodness3 = -1 * KL(_.values(poll3), _.values(_.last(path))); return {"rq": goodness3 - goodness0} }); util.seedRNG(4); viz.auto(pollsDist); viz.table(pollsDist, {top: 10})
never mind -- my KL didn't account for 0 log 0, so my distribution had NaNs in it (maybe we should filter these out during viz?)
0 log 0
discovered while tinkering with the ppaml election stuff: