public void Mixture2() { double[] data = { .5, 12, 11 }; int T = data.Length; VariableArray <double> x = Variable.Constant(data).Named("data"); Range i = x.Range; Variable <Vector> D = Variable.Dirichlet(new double[] { 1, 1 }); VariableArray <int> c = Variable.Array <int>(i); using (Variable.ForEach(i)) { c[i] = Variable.Discrete(D); using (Variable.Case(c[i], 0)) { x[i] = Variable.GaussianFromMeanAndPrecision(5, 1); } using (Variable.Case(c[i], 1)) { x[i] = Variable.GaussianFromMeanAndPrecision(10, 1); } } InferenceEngine ie = new InferenceEngine(new VariationalMessagePassing()); double[] DVibesResult = new double[] { -1.08333333348476, -0.58333333335936 }; VmpTests.TestDirichletMoments(ie, D, DVibesResult); double[][] cVibesResult = new double[T][]; cVibesResult[0] = new double[] { 1.00000000000000, 0.00000000000000 }; cVibesResult[1] = new double[] { 0.00000000000000, 1.00000000000000 }; cVibesResult[2] = new double[] { 0.00000000000000, 1.00000000000000 }; VmpTests.TestDiscrete(ie, c, cVibesResult); }
public void Mixture1() { double[] data = { 7 }; int T = data.Length; VariableArray <double> x = Variable.Constant(data).Named("data"); Range i = x.Range; Variable <Vector> D = Variable.Dirichlet(new double[] { 1, 1 }); VariableArray <int> c = Variable.Array <int>(i); using (Variable.ForEach(i)) { c[i] = Variable.Discrete(D); using (Variable.Case(c[i], 0)) { x[i] = Variable.GaussianFromMeanAndPrecision(5.5, 1); } using (Variable.Case(c[i], 1)) { x[i] = Variable.GaussianFromMeanAndPrecision(8, 1); } } InferenceEngine ie = new InferenceEngine(new VariationalMessagePassing()); double[] DVibesResult = new double[] { -1.15070203880043, -0.67501576717763 }; VmpTests.TestDirichletMoments(ie, D, DVibesResult); double[][] cVibesResult = new double[T][]; cVibesResult[0] = new double[] { 0.24961241199438, 0.75038758800562 }; VmpTests.TestDiscrete(ie, c, cVibesResult); }
public void MixtureOfDiscretePartialObs() { int K = 2; Range Krange = new Range(K).Named("K"); Variable <Vector> D = Variable.Dirichlet(Krange, new double[] { 1, 1 }).Named("D"); VariableArray <Vector> pi = Variable.Array <Vector>(Krange).Named("pi"); pi[Krange] = Variable.Dirichlet(new double[] { 1, 1 }).ForEach(Krange); VariableArray <Vector> pi2 = Variable.Array <Vector>(Krange); pi2[Krange] = Variable.Dirichlet(new double[] { 2, 2 }).ForEach(Krange).Named("pi2"); int[] xData = new int[] { 1, 0 }; int[] yData = new int[] { 1, 0 }; Range T = new Range(2).Named("T"); VariableArray <int> x = Variable.Constant(xData, T).Named("x"); VariableArray <int> y = Variable.Constant(yData, T).Named("y"); VariableArray <int> c = Variable.Array <int>(T).Named("c"); using (Variable.ForEach(T)) { c[T] = Variable.Discrete(D); using (Variable.Switch(c[T])) { x[T] = Variable.Discrete(pi[c[T]]); y[T] = Variable.Discrete(pi2[c[T]]); } } int[] xObsData = new int[] { 1 }; int[] yObsData = new int[] { 1 }; int[] cObsData = new int[] { 1 }; Range TObs = new Range(1).Named("TObs"); VariableArray <int> xObs = Variable.Constant(xObsData, TObs).Named("xObs"); VariableArray <int> yObs = Variable.Constant(yObsData, TObs).Named("yObs"); VariableArray <int> cObs = Variable.Constant(cObsData, TObs).Named("cObs"); using (Variable.ForEach(TObs)) { cObs[TObs] = Variable.Discrete(D); using (Variable.Switch(cObs[TObs])) { xObs[TObs] = Variable.Discrete(pi[cObs[TObs]]); yObs[TObs] = Variable.Discrete(pi2[cObs[TObs]]); } } InferenceEngine ie = new InferenceEngine(new VariationalMessagePassing()); VmpTests.TestDirichletMoments(ie, D, new double[] { -1.33369062459866, -0.45559642996801 }); double[][] cVibesResult = new double[2][]; cVibesResult[0] = new double[] { 0.12507345314883, 0.87492654685117 }; cVibesResult[1] = new double[] { 0.53017394027554, 0.46982605972446 }; VmpTests.TestDiscrete(ie, c, cVibesResult); double[][] piVibesResult = new double[2][]; piVibesResult[0] = new double[] { -0.71224535522876, -1.16495845985210 }; piVibesResult[1] = new double[] { -1.34159372292073, -0.47735375561174 }; VmpTests.TestDirichletMoments(ie, pi, piVibesResult); double[][] pi2VibesResult = new double[2][]; pi2VibesResult[0] = new double[] { -0.70890921479891, -0.92632718538028 }; pi2VibesResult[1] = new double[] { -1.07851393492695, -0.54677568970466 }; VmpTests.TestDirichletMoments(ie, pi2, pi2VibesResult); // bound: -5.7515783 }