예제 #1
0
        public void Run()
        {
            BayesianPCAModel bpca = new BayesianPCAModel();

            if (!(bpca.engine.Algorithm is VariationalMessagePassing))
            {
                Console.WriteLine("This example only runs with Variational Message Passing");
                return;
            }

            // Set a stable random number seed for repeatbale 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();
        }
예제 #2
0
		public void Run()
		{
			BayesianPCAModel bpca = new BayesianPCAModel();
			if (!(bpca.engine.Algorithm is VariationalMessagePassing))
			{
				Console.WriteLine("This example only runs with Variational Message Passing");
				return;
			}
	
			// Set a stable random number seed for repeatbale 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;
			IDistribution<double[,]> wMarginal = bpca.engine.Infer<IDistribution<double[,]>>(bpca.vW);
			IDistribution<double[]> muMarginal = bpca.engine.Infer<IDistribution<double[]>>(bpca.vMu);
			IDistribution<double[]> piMarginal = bpca.engine.Infer<IDistribution<double[]>>(bpca.vPi);
			// Convert distributions over arrays of doubles to arrays of distributions
			Gaussian[,] inferredW = Distribution.ToArray<Gaussian[,]>(wMarginal);
			Gaussian[] inferredMu = Distribution.ToArray<Gaussian[]>(muMarginal);
			Gamma[] inferredPi = Distribution.ToArray<Gamma[]>(piMarginal);
			// 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();
		}