public void Run() { BayesianPCAModel bpca = new BayesianPCAModel(); if (!(bpca.engine.Algorithm is Algorithms.VariationalMessagePassing)) { Console.WriteLine("This example only runs with Variational Message Passing"); return; } // Set a stable random number seed for repeatable runs Rand.Restart(12347); double[,] data = generateData(1000); // Set the data bpca.data.ObservedValue = data; // Set the dimensions bpca.observationCount.ObservedValue = data.GetLength(0); bpca.featureCount.ObservedValue = data.GetLength(1); bpca.componentCount.ObservedValue = 6; // Set the priors bpca.priorMu.ObservedValue = Gaussian.FromMeanAndPrecision(0.0, 0.01); bpca.priorPi.ObservedValue = Gamma.FromShapeAndRate(2.0, 2.0); bpca.priorAlpha.ObservedValue = Gamma.FromShapeAndRate(2.0, 2.0); // Set the initialization bpca.initW.ObservedValue = randomGaussianArray(bpca.componentCount.ObservedValue, bpca.featureCount.ObservedValue); // Infer the marginals bpca.engine.NumberOfIterations = 200; var inferredW = bpca.engine.Infer <IArray2D <Gaussian> >(bpca.W); var inferredMu = bpca.engine.Infer <IReadOnlyList <Gaussian> >(bpca.mu); var inferredPi = bpca.engine.Infer <IReadOnlyList <Gamma> >(bpca.pi); // Print out the results Console.WriteLine("Inferred W:"); printMatrixToConsole(inferredW); Console.Write("Mean absolute means of rows in W: "); printVectorToConsole(meanAbsoluteRowMeans(inferredW)); Console.Write(" True bias: "); printVectorToConsole(trueMu); Console.Write("Inferred bias: "); printVectorToConsole(inferredMu.Select(d => d.GetMean())); Console.Write(" True noise:"); printVectorToConsole(truePi); Console.Write("Inferred noise:"); printVectorToConsole(inferredPi.Select(d => d.GetMean())); Console.WriteLine(); }
public void Run() { BayesianPCAModel bpca = new BayesianPCAModel(); if (!(bpca.engine.Algorithm is Algorithms.VariationalMessagePassing)) { Console.WriteLine("This example only runs with Variational Message Passing"); return; } // Set a stable random number seed for repeatable runs Rand.Restart(12347); double[,] data = generateData(1000); // Set the data bpca.vData.ObservedValue = data; // Set the dimensions bpca.vN.ObservedValue = data.GetLength(0); bpca.vD.ObservedValue = data.GetLength(1); bpca.vM.ObservedValue = 6; // Set the priors bpca.priorMu.ObservedValue = Gaussian.FromMeanAndPrecision(0.0, 0.01); bpca.priorPi.ObservedValue = Gamma.FromShapeAndRate(2.0, 2.0); bpca.priorAlpha.ObservedValue = Gamma.FromShapeAndRate(2.0, 2.0); // Initialize the W marginal to break symmetry bpca.vW.InitialiseTo(randomGaussianArray(bpca.vM.ObservedValue, bpca.vD.ObservedValue)); // Infer the marginals bpca.engine.NumberOfIterations = 200; Gaussian[,] inferredW = bpca.engine.Infer <Gaussian[, ]>(bpca.vW); Gaussian[] inferredMu = bpca.engine.Infer <Gaussian[]>(bpca.vMu); Gamma[] inferredPi = bpca.engine.Infer <Gamma[]>(bpca.vPi); // Print out the results Console.WriteLine("Inferred W:"); printMatrixToConsole(inferredW); Console.Write("Mean absolute means of rows in W: "); printVectorToConsole(meanAbsoluteRowMeans(inferredW)); Console.Write(" True bias: "); printVectorToConsole(trueMu); Console.Write("Inferred bias: "); printVectorToConsole(inferredMu); Console.Write(" True noise:"); printVectorToConsole(truePi); Console.Write("Inferred noise:"); printVectorToConsole(inferredPi); Console.WriteLine(); }