/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="UsesEqualDefGibbsOp{T}"]/message_doc[@name="GibbsEvidence{TDist}(IList{TDist}, TDist, GibbsMarginal{TDist, T})"]/*'/> /// <typeparam name="TDist">The type of the distribution over the variable.</typeparam> public static double GibbsEvidence <TDist>(IList <TDist> Uses, TDist Def, GibbsMarginal <TDist, T> to_marginal) where TDist : IDistribution <T>, Sampleable <T>, CanGetLogAverageOf <TDist>, SettableTo <TDist>, SettableToProduct <TDist> { if (Uses.Count == 1) { // the total evidence contribution of this variable should be Def.GetLogAverageOf(Uses[0]). // but since this variable is sending a sample to Def and Use, and those factors will send their own evidence contribution, // we need to cancel the contribution of those factors here. return(Def.GetLogAverageOf(Uses[0]) - Def.GetLogProb(to_marginal.LastSample) - Uses[0].GetLogProb(to_marginal.LastSample)); } else { //throw new InferRuntimeException("Gibbs Sampling does not support variables defined within a gate"); double z = 0.0; TDist productBefore = (TDist)Def.Clone(); TDist product = (TDist)Def.Clone(); for (int i = 0; i < Uses.Count; i++) { if (i > 0) { product.SetToProduct(productBefore, Uses[i - 1]); } z += product.GetLogAverageOf(Uses[i]); productBefore.SetTo(product); } // z is now log(sum_x Def(x)*prod_i Uses[i](x)), which is the desired total evidence. // but we must also cancel the contribution of the parent and child factors that received a sample from us. z -= Def.GetLogProb(to_marginal.LastSample); for (int i = 0; i < Uses.Count; i++) { z -= Uses[i].GetLogProb(to_marginal.LastSample); } return(z); } }
/// <summary>Computations that depend on the observed value of firstCoin and numberOfIterations</summary> /// <param name="numberOfIterations">The number of times to iterate each loop</param> private void Changed_firstCoin_numberOfIterations(int numberOfIterations) { if (this.Changed_firstCoin_numberOfIterations_isDone) { return; } bool[] secondCoin_uses_F; Bernoulli[] secondCoin_uses_B; // Create array for 'secondCoin_uses' Forwards messages. secondCoin_uses_F = new bool[1]; // Create array for 'secondCoin_uses' Backwards messages. secondCoin_uses_B = new Bernoulli[1]; secondCoin_uses_B[0] = Bernoulli.Uniform(); bool bothHeads_F = default(bool); for (int iteration = this.numberOfIterationsDone; iteration < numberOfIterations; iteration++) { // Message to 'secondCoin_marginal' from UsesEqualDef factor this.secondCoin_marginal_F = UsesEqualDefGibbsOp <bool> .MarginalGibbs <Bernoulli>(secondCoin_uses_B, this.vBernoulli0, this.secondCoin_marginal_F); // Message to 'secondCoin_uses' from UsesEqualDef factor secondCoin_uses_F[0] = UsesEqualDefGibbsOp <bool> .UsesGibbs <Bernoulli>(this.secondCoin_marginal_F, 0, secondCoin_uses_F[0]); // Message to 'bothHeads' from And factor bothHeads_F = Factor.And(this.FirstCoin, secondCoin_uses_F[0]); // Message to 'bothHeads_marginal' from ReplicateWithMarginal factor this.bothHeads_marginal_F = ReplicateGibbsOp <bool> .MarginalGibbs <Bernoulli>(bothHeads_F, this.bothHeads_marginal_F); this.OnProgressChanged(new ProgressChangedEventArgs(iteration)); } this.Changed_firstCoin_numberOfIterations_isDone = true; }
/// <summary>Computations that depend on the observed value of numberOfIterationsDecreased</summary> private void Changed_numberOfIterationsDecreased() { if (this.Changed_numberOfIterationsDecreased_isDone) { return; } this.firstCoin_marginal_F = new GibbsMarginal <Bernoulli, bool>(this.vBernoulli0, 100, 5, true, true, false); this.secondCoin_marginal_F = new GibbsMarginal <Bernoulli, bool>(this.vBernoulli0, 100, 5, true, true, false); this.bothHeads_marginal_F = new GibbsMarginal <Bernoulli, bool>(new Bernoulli(), 100, 5, true, true, false); this.Changed_numberOfIterationsDecreased_isDone = true; }
/// <summary>Computations that depend on the observed value of numberOfIterationsDecreased and must reset on changes to firstCoin</summary> /// <param name="initialise">If true, reset messages that initialise loops</param> private void Changed_numberOfIterationsDecreased_Init_firstCoin(bool initialise) { if (this.Changed_numberOfIterationsDecreased_Init_firstCoin_isDone && ((!initialise) || this.Changed_numberOfIterationsDecreased_Init_firstCoin_isInitialised)) { return; } this.secondCoin_marginal_F = new GibbsMarginal <Bernoulli, bool>(this.vBernoulli0, 100, 5, true, true, false); this.bothHeads_marginal_F = new GibbsMarginal <Bernoulli, bool>(new Bernoulli(), 100, 5, true, true, false); this.Changed_numberOfIterationsDecreased_Init_firstCoin_isDone = true; this.Changed_numberOfIterationsDecreased_Init_firstCoin_isInitialised = true; }
[Stochastic] // must be labelled Stochastic to get correct schedule, even though it isn't Stochastic public static GibbsMarginal <TDist, T> MarginalGibbs <TDist, T>( T Use, [IgnoreDependency] TDist Def, GibbsMarginal <TDist, T> to_marginal) where TDist : IDistribution <T>, Sampleable <T> { GibbsMarginal <TDist, T> result = to_marginal; TDist marginal = result.LastConditional; marginal.Point = Use; result.LastConditional = marginal; // Allow a sample to be drawn from the last conditional, and add it to the sample // list and conditional list result.PostUpdate(); return(result); }
[Stochastic] // must be labelled Stochastic to get correct schedule, even though it isn't Stochastic public static GibbsMarginal <TDist, TDomain> MarginalGibbs <TDist, TDomain>( TDomain Def, GibbsMarginal <TDist, TDomain> to_marginal) where TDist : IDistribution <TDomain>, Sampleable <TDomain> { GibbsMarginal <TDist, TDomain> result = to_marginal; TDist marginal = result.LastConditional; marginal.Point = Def; result.LastConditional = marginal; // Allow a sample to be drawn from the last conditional, and add it to the sample // list and conditional list result.PostUpdate(); return(result); }
public static GibbsMarginal <TDist, T> MarginalGibbs <TDist>( IList <TDist> Uses, T Def, GibbsMarginal <TDist, T> to_marginal) // must not be called 'result', because its value is used where TDist : IDistribution <T>, SettableToProduct <TDist>, SettableToRatio <TDist>, SettableTo <TDist>, Sampleable <T> { GibbsMarginal <TDist, T> result = to_marginal; TDist marginal = result.LastConditional; marginal.Point = Def; result.LastConditional = marginal; // Allow a sample to be drawn from the last conditional, and add it to the sample // list and conditional list result.PostUpdate(); return(result); }
public static GibbsMarginal <TDist, T> MarginalGibbs <TDist, T>( TDist Use, [SkipIfUniform] TDist Def, GibbsMarginal <TDist, T> to_marginal) where TDist : IDistribution <T>, SettableToProduct <TDist>, Sampleable <T> { GibbsMarginal <TDist, T> result = to_marginal; TDist marginal = result.LastConditional; marginal.SetToProduct(Def, Use); result.LastConditional = marginal; // Allow a sample to be drawn from the last conditional, and add it to the sample // list and conditional list result.PostUpdate(); return(result); }
public static GibbsMarginal <TDist, T> MarginalGibbs <TDist, T>( T Use, [SkipIfUniform] TDist Def, GibbsMarginal <TDist, T> to_marginal) // must not be called 'result', because its value is used where TDist : IDistribution <T>, Sampleable <T> { GibbsMarginal <TDist, T> result = to_marginal; TDist marginal = result.LastConditional; marginal.Point = Use; result.LastConditional = marginal; // Allow a sample to be drawn from the last conditional, and add it to the sample // list and conditional list result.PostUpdate(); return(result); }
[Stochastic] // must be labelled Stochastic to get correct schedule, even though it isn't Stochastic public static GibbsMarginal <TDist, T> MarginalGibbs <TDist>( T[] Uses, GibbsMarginal <TDist, T> to_marginal) where TDist : IDistribution <T>, Sampleable <T> { GibbsMarginal <TDist, T> result = to_marginal; TDist marginal = result.LastConditional; if (Uses.Length != 1) { throw new ArgumentException("Uses.Length (" + Uses.Length + ") != 1"); } marginal.Point = Uses[0]; result.LastConditional = marginal; // Allow a sample to be drawn from the last conditional, and add it to the sample // list and conditional list result.PostUpdate(); return(result); }
public static GibbsMarginal <TDist, T> MarginalGibbs <TDist>( IList <T> Uses, [SkipIfUniform] TDist Def, GibbsMarginal <TDist, T> to_marginal) // must not be called 'result', because its value is used where TDist : IDistribution <T>, SettableToProduct <TDist>, SettableToRatio <TDist>, SettableTo <TDist>, Sampleable <T> { if (Uses.Count > 1) { throw new ArgumentException("Uses.Count > 1"); } GibbsMarginal <TDist, T> result = to_marginal; TDist marginal = result.LastConditional; marginal.Point = Uses[0]; result.LastConditional = marginal; // Allow a sample to be drawn from the last conditional, and add it to the sample // list and conditional list result.PostUpdate(); return(result); }
/// <summary>Computations that depend on the observed value of bothHeads and numberOfIterations</summary> /// <param name="numberOfIterations">The number of times to iterate each loop</param> private void Changed_bothHeads_numberOfIterations(int numberOfIterations) { if (this.Changed_bothHeads_numberOfIterations_isDone) { return; } bool[] firstCoin_uses_F; Bernoulli[] firstCoin_uses_B; // Create array for 'firstCoin_uses' Forwards messages. firstCoin_uses_F = new bool[1]; // Create array for 'firstCoin_uses' Backwards messages. firstCoin_uses_B = new Bernoulli[1]; firstCoin_uses_B[0] = Bernoulli.Uniform(); bool[] secondCoin_uses_F; // Create array for 'secondCoin_uses' Forwards messages. secondCoin_uses_F = new bool[1]; for (int iteration = this.numberOfIterationsDone; iteration < numberOfIterations; iteration++) { // Message to 'secondCoin_marginal' from UsesEqualDef factor this.secondCoin_marginal_F = UsesEqualDefGibbsOp <bool> .MarginalGibbs <Bernoulli>(this.secondCoin_uses_B, this.vBernoulli0, this.secondCoin_marginal_F); // Message to 'secondCoin_uses' from UsesEqualDef factor secondCoin_uses_F[0] = UsesEqualDefGibbsOp <bool> .UsesGibbs <Bernoulli>(this.secondCoin_marginal_F, 0, secondCoin_uses_F[0]); // Message to 'firstCoin_uses' from And factor firstCoin_uses_B[0] = BooleanAndOp.AAverageConditional(this.BothHeads, secondCoin_uses_F[0]); // Message to 'firstCoin_marginal' from UsesEqualDef factor this.firstCoin_marginal_F = UsesEqualDefGibbsOp <bool> .MarginalGibbs <Bernoulli>(firstCoin_uses_B, this.vBernoulli0, this.firstCoin_marginal_F); // Message to 'firstCoin_uses' from UsesEqualDef factor firstCoin_uses_F[0] = UsesEqualDefGibbsOp <bool> .UsesGibbs <Bernoulli>(this.firstCoin_marginal_F, 0, firstCoin_uses_F[0]); // Message to 'secondCoin_uses' from And factor this.secondCoin_uses_B[0] = BooleanAndOp.BAverageConditional(this.BothHeads, firstCoin_uses_F[0]); this.OnProgressChanged(new ProgressChangedEventArgs(iteration)); } this.Changed_bothHeads_numberOfIterations_isDone = true; }
public static T UsesGibbs <TDist>([SkipIfUniform] GibbsMarginal <TDist, T> marginal, T def, int resultIndex, T result) where TDist : IDistribution <T>, Sampleable <T> { if (def is bool[]) { if (!Util.ValueEquals((bool[])(object)def, (bool[])(object)marginal.LastSample)) { throw new Exception("gotcha"); } } else if (def is double[]) { if (!Util.ValueEquals((double[])(object)def, (double[])(object)marginal.LastSample)) { throw new Exception("gotcha"); } } else if (!def.Equals(marginal.LastSample)) { throw new Exception("gotcha"); } return(marginal.LastSample); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="UsesEqualDefGibbsOp{T}"]/message_doc[@name="DefGibbs{TDist}(GibbsMarginal{TDist, T}, T)"]/*'/> /// <typeparam name="TDist">The type of the distribution over the variable.</typeparam> public static T DefGibbs <TDist>([SkipIfUniform] GibbsMarginal <TDist, T> to_marginal, T result) where TDist : IDistribution <T>, Sampleable <T> { return(to_marginal.LastSample); }
/// <summary> /// Gibbs evidence /// </summary> /// <returns></returns> public static double GibbsEvidence <TDist, T>(TDist Use, TDist Def, GibbsMarginal <TDist, T> marginal) where TDist : IDistribution <T>, Sampleable <T>, CanGetLogAverageOf <TDist> { return(Def.GetLogAverageOf(Use) - Def.GetLogProb(marginal.LastSample) - Use.GetLogProb(marginal.LastSample)); }
/// <summary> /// Gibbs sample message to 'Uses' /// </summary> /// <typeparam name="TDist">Gibbs marginal type</typeparam> /// <typeparam name="T">Domain type</typeparam> /// <param name="marginal">The Gibbs marginal</param> /// <param name="def"></param> /// <param name="result">Result</param> /// <returns></returns> /// <remarks><para> /// The outgoing message is the current Gibbs sample. /// </para></remarks> public static T UseGibbs <TDist, T>([SkipIfUniform] GibbsMarginal <TDist, T> marginal, TDist def, T result) where TDist : IDistribution <T>, Sampleable <T> { // This method must depend on Def, even though Def isn't used, in order to get the right triggers return(marginal.LastSample); }
public static T UsesGibbs <TDist>([SkipIfUniform] GibbsMarginal <TDist, T> marginal, TDist def, int resultIndex, T result) where TDist : IDistribution <T>, Sampleable <T> { return(marginal.LastSample); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="ReplicateGibbsOp{T}"]/message_doc[@name="UsesGibbs{TDist}(GibbsMarginal{TDist, T}, TDist, int, T)"]/*'/> /// <typeparam name="TDist">The type of the distribution over the replicated variable.</typeparam> public static T UsesGibbs <TDist>([SkipIfUniform] GibbsMarginal <TDist, T> to_marginal, [IgnoreDependency] TDist def, [IgnoreDependency] int resultIndex, T result) where TDist : IDistribution <T>, Sampleable <T> { // This method must depend on Def, even though Def isn't used, in order to get the right triggers return(to_marginal.LastSample); }