public void Visit <T1>(RandomPrimitive <T1> erp) { if (erp is Constant <T1> ) { // do not add to trace as we do not want to // resample constants this.sample = new Weighted <T1>(erp.Sample(-1)); return; } var prev = FindChoice(this.oldTrace, this.stack, erp); var reuse = prev.HasValue && ((RandomPrimitive)prev.Value.Erp).ForceRegen == false; if (reuse) { var entry = prev.Value; this.sample = new Weighted <T1>((T1)entry.Sample); entry.Reused = true; this.trace.Add(entry); } else { var sample = erp.Sample(this.generation); var score = Math.Log(erp.Score(sample)); this.sample = new Weighted <T1>(sample); var entry = new TraceEntry(this.stack, erp, sample, score, false); this.trace.Add(entry); // keep from forcing a regeneration of this RandomPrimitive // either on this iteration (i.e., because of a programmer // induced dependence) or in future iterations. ((RandomPrimitive)erp).ForceRegen = false; } }
public TraceEntry(Address location, RandomPrimitive erp, object sample, double score, bool reuse = false) { this.Location = location; this.Erp = erp; this.Sample = sample; this.Score = score; this.Reused = reuse; }
protected override bool StructuralEquals(RandomPrimitive other) { if (other is Flip) { return(((Flip)other).p == this.p); } return(false); }
protected override bool StructuralEquals(RandomPrimitive other) { if (other is Constant <T> ) { return(this.constant.Equals(((Constant <T>)other).constant)); } return(false); }
protected override bool StructuralEquals(RandomPrimitive other) { if (other is Exponential) { return(((Exponential)other).lambda == this.lambda); } return(false); }
protected override bool StructuralEquals(RandomPrimitive other) { if (other is Uniform <T> ) { var tmp = other as Uniform <T>; return(tmp.min == this.min && tmp.max == this.max); } return(false); }
protected override bool StructuralEquals(RandomPrimitive other) { if (other is Gaussian) { var tmp = other as Gaussian; return(tmp.stdev == this.stdev && tmp.mu == this.mu); } return(false); }
protected override bool StructuralEquals(RandomPrimitive other) { if (other is Multinomial <T> ) { var tmp = other as Multinomial <T>; if (tmp.options.Count() != this.options.Count()) { return(false); } foreach (var pairs in this.options.Zip(tmp.options, Tuple.Create)) { if (pairs.Item1.Value.Equals(pairs.Item2.Value) == false || pairs.Item1.Probability != pairs.Item2.Probability) // toddm: comparing on double! :( { return(false); } } } return(true); }
private IEnumerable <Weighted <T1> > GetEnumerator <T1>(Uncertain <T1> uncertain) { /// TODO: I am still building a caching mechanism! var regenFrom = 0; RandomPrimitive sampled = null; var initstack = this.stack; var sampler = new SingleSampler(); do { this.stack = initstack; // Run the program. uncertain.Accept(this); //var samples = (from e in this.trace select e.Sample).ToArray(); //int count; //if (!this.cache.TryGetValue(samples, out count)) //{ // count = 0; //} //else //{ // count = count; //} //this.cache[samples] = count + 1; // TODO: should we return 0 mass samples? // 0 IS a reasonable weight if all // one wants to do is know about a // posterior - need to compute the score // from the Weighted<T1> object rather // than the trace. var returnval = (Weighted <T1>) this.sample; //if (returnval.Probability > 0) // yield return returnval; yield return(returnval); var roll = Extensions.NextRandom(); var acceptance = Accept(trace, oldTrace, regenFrom); if (this.generation > 1 && !(Math.Log(roll) < acceptance)) { // rollback proposal trace.Clear(); trace.AddRange(oldTrace); } Swap(ref trace, ref oldTrace); if (oldTrace.Count > 0) { // A programmer induced dependence implies the trace can have // more than one copy of any given Random Primitive // only sample from the set of distinct RandomPrimitives // to avoid oversampling any particular RandomPrimitive. //var dict = new Dictionary<RandomPrimitive, IList<int>>(); //var pos = 0; //foreach(var e in oldTrace) //{ // IList<int> lst; // if (! dict.TryGetValue(e.Erp, out lst)) // { // lst = new List<int>(); // dict[e.Erp] = lst; // } // lst.Add(pos++); //} //regenFrom = (int)Math.Floor(Extensions.NextRandom() * dict.Keys.Count); //sampled = dict.Keys.ElementAt(regenFrom); var distinct = oldTrace.Select(e => e.Erp).Distinct().ToList(); regenFrom = (int)Math.Floor(Extensions.NextRandom() * distinct.Count); sampled = distinct[regenFrom]; // force regeneration of this random primitive // note we reset this to false in the // Visit(RandomPrimitive) method sampled.ForceRegen = true; sampled.Accept(sampler); //var key = (from e in this.trace select e.Sample).ToArray(); } trace.Clear(); this.generation++; } while (true); }
private static TraceEntry?FindChoice(IEnumerable <TraceEntry> trace, Address stack, RandomPrimitive erp) { foreach (var e in trace) { if (StructuralComparisons.StructuralEqualityComparer.Equals(e.Location, stack) && StructuralComparisons.StructuralEqualityComparer.Equals(e.Erp, erp)) { return(e); } } return(null); }
public void Visit <T>(RandomPrimitive <T> erp) { this.Sample = erp.Sample(-1); }
protected override bool StructuralEquals(RandomPrimitive other) { throw new NotImplementedException(); }
public void Visit <T1>(RandomPrimitive <T1> erp) { var sample = erp.Sample(this.generation); this.sample = new Weighted <T1>(sample); }