예제 #1
0
        public void Run()
        {
            // Set random seed for repeatable example
            Rand.Restart(12347);

            // Create a new model
            WetGlassSprinklerRainModel model = new WetGlassSprinklerRainModel();

            if (model.Engine.Algorithm is GibbsSampling)
            {
                Console.WriteLine("This example does not run with Gibbs Sampling");
                return;
            }

            // In this example, each variable just takes two states - true (index 0)
            // and false (index 1). The example is written so that extensions to
            // problems of more than two states is straightforward.

            // -------------------------------------------------------------
            // Usage 1: Query the model when we know the parameters exactly
            // -------------------------------------------------------------
            Console.WriteLine("\n*********************************************");
            Console.WriteLine("Querying the model with a known ground truth");
            Console.WriteLine("*********************************************");

            Vector probCloudy = Vector.FromArray(0.5, 0.5);

            Vector[]   cptSprinkler = new Vector[] { Vector.FromArray(0.1, 0.9) /* cloudy */, Vector.FromArray(0.5, 0.5) /* not cloudy */ };
            Vector[]   cptRain      = new Vector[] { Vector.FromArray(0.8, 0.2) /* cloudy */, Vector.FromArray(0.2, 0.8) /* not cloudy */ };
            Vector[][] cptWetGrass  = new Vector[][]
            {
                new Vector[] { Vector.FromArray(0.99, 0.01) /* rain */, Vector.FromArray(0.9, 0.1) /* not rain */ },          // Sprinkler
                new Vector[] { Vector.FromArray(0.9, 0.1) /* rain */, Vector.FromArray(0.0, 1.0) /* not rain */ }             // Not sprinkler
            };

            double probRainGivenWetGrass = model.ProbRain(
                null, null, 0,
                probCloudy, cptSprinkler, cptRain, cptWetGrass);
            double probRainGivenWetGrassNotCloudy = model.ProbRain(
                1, null, 0,
                probCloudy, cptSprinkler, cptRain, cptWetGrass);

            Console.WriteLine("P(rain | grass is wet)              = {0:0.0000}", probRainGivenWetGrass);
            Console.WriteLine("P(rain | grass is wet, not cloudy ) = {0:0.0000}", probRainGivenWetGrassNotCloudy);

            // -------------------------------------------------------------
            // Usage 2: Learn posterior distributions for the parameters
            // -------------------------------------------------------------
            // To validate the learning, first sample from a known model
            int[][] sample = WetGlassSprinklerRainModel.Sample(1000, probCloudy, cptSprinkler, cptRain, cptWetGrass);

            Console.WriteLine("\n*********************************************");
            Console.WriteLine("Learning parameters from data (uniform prior)");
            Console.WriteLine("*********************************************");

            // Now see if we can recover the parameters from the data - assume uniform priors
            model.LearnParameters(sample[0], sample[1], sample[2], sample[3]);

            // The posteriors are distributions over the probabilities and CPTs. Print out the means of these
            // distributions, and compare with the ground truth
            Console.WriteLine("Prob. Cloudy:                              Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.5, model.ProbCloudyPosterior.GetMean()[0]);
            Console.WriteLine("Prob. Sprinkler | Cloudy:                  Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.1, model.CPTSprinklerPosterior[0].GetMean()[0]);
            Console.WriteLine("Prob. Sprinkler | Not Cloudy:              Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.5, model.CPTSprinklerPosterior[1].GetMean()[0]);
            Console.WriteLine("Prob. Rain      | Cloudy:                  Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.8, model.CPTRainPosterior[0].GetMean()[0]);
            Console.WriteLine("Prob. Rain      | Not Cloudy:              Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.2, model.CPTRainPosterior[1].GetMean()[0]);
            Console.WriteLine("Prob. Wet Grass | Sprinkler, Rain:         Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.99, model.CPTWetGrassPosterior[0][0].GetMean()[0]);
            Console.WriteLine("Prob. Wet Grass | Sprinkler, Not Rain      Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.9, model.CPTWetGrassPosterior[0][1].GetMean()[0]);
            Console.WriteLine("Prob. Wet Grass | Not Sprinkler, Rain:     Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.9, model.CPTWetGrassPosterior[1][0].GetMean()[0]);
            Console.WriteLine("Prob. Wet Grass | Not Sprinkler, Not Rain: Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.0, model.CPTWetGrassPosterior[1][1].GetMean()[0]);

            // -------------------------------------------------------------
            // Usage 3: Querying the model taking into account uncertainty
            //          of the parameters
            // -------------------------------------------------------------

            // Use posteriors we have just learnt
            Console.WriteLine("\n**********************************************");
            Console.WriteLine("Querying the model with uncertain ground truth");
            Console.WriteLine("**********************************************");
            double probRainGivenWetGrass1 = model.ProbRain(
                null, null, 0,
                model.ProbCloudyPosterior, model.CPTSprinklerPosterior, model.CPTRainPosterior, model.CPTWetGrassPosterior);
            double probRainGivenWetGrassNotCloudy1 = model.ProbRain(
                1, null, 0,
                model.ProbCloudyPosterior, model.CPTSprinklerPosterior, model.CPTRainPosterior, model.CPTWetGrassPosterior);

            Console.WriteLine("P(rain | grass is wet)              = {0:0.0000}", probRainGivenWetGrass1);
            Console.WriteLine("P(rain | grass is wet, not cloudy ) = {0:0.0000}", probRainGivenWetGrassNotCloudy1);
            Console.WriteLine("");
        }
예제 #2
0
		public void Run()
		{
			// Set random seed for repeatable example
			Rand.Restart(12347);

			// Create a new model
			WetGlassSprinklerRainModel model = new WetGlassSprinklerRainModel();
			if (model.Engine.Algorithm is GibbsSampling)
			{
				Console.WriteLine("This example does not run with Gibbs Sampling");
				return;
			} 
			
			// In this example, each variable just takes two states - true (index 0)
			// and false (index 1). The example is written so that extensions to
			// problems of more than two states is straightforward.

			// -------------------------------------------------------------
			// Usage 1: Query the model when we know the parameters exactly
			// -------------------------------------------------------------
			Console.WriteLine("\n*********************************************");
			Console.WriteLine("Querying the model with a known ground truth");
			Console.WriteLine("*********************************************");

			Vector probCloudy = Vector.FromArray(0.5, 0.5);
			Vector[] cptSprinkler = new Vector[] { Vector.FromArray(0.1, 0.9) /* cloudy */, Vector.FromArray(0.5, 0.5) /* not cloudy */ };
			Vector[] cptRain = new Vector[] { Vector.FromArray(0.8, 0.2) /* cloudy */, Vector.FromArray(0.2, 0.8) /* not cloudy */ };
			Vector[][] cptWetGrass = new Vector[][]
			{
			    new Vector[] { Vector.FromArray(0.99, 0.01) /* rain */,  Vector.FromArray(0.9, 0.1) /* not rain */}, // Sprinkler
			    new Vector[] { Vector.FromArray(0.9, 0.1) /* rain */, Vector.FromArray(0.0, 1.0) /* not rain */}  // Not sprinkler
			};

			double probRainGivenWetGrass = model.ProbRain(
				null, null, 0,
				probCloudy, cptSprinkler, cptRain, cptWetGrass);
			double probRainGivenWetGrassNotCloudy = model.ProbRain(
				1, null, 0,
				probCloudy, cptSprinkler, cptRain, cptWetGrass);

			Console.WriteLine("P(rain | grass is wet)              = {0:0.0000}", probRainGivenWetGrass);
			Console.WriteLine("P(rain | grass is wet, not cloudy ) = {0:0.0000}", probRainGivenWetGrassNotCloudy);

			// -------------------------------------------------------------
			// Usage 2: Learn posterior distributions for the parameters
			// -------------------------------------------------------------
			// To validate the learning, first sample from a known model
			int[][] sample = WetGlassSprinklerRainModel.Sample(1000, probCloudy, cptSprinkler, cptRain, cptWetGrass);

			Console.WriteLine("\n*********************************************");
			Console.WriteLine("Learning parameters from data (uniform prior)");
			Console.WriteLine("*********************************************");

			// Now see if we can recover the parameters from the data - assume uniform priors
			model.LearnParameters(sample[0], sample[1], sample[2], sample[3]);

			// The posteriors are distributions over the probabilities and CPTs. Print out the means of these
			// distributions, and compare with the ground truth
			Console.WriteLine("Prob. Cloudy:                              Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.5, model.ProbCloudyPosterior.GetMean()[0]);
			Console.WriteLine("Prob. Sprinkler | Cloudy:                  Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.1, model.CPTSprinklerPosterior[0].GetMean()[0]);
			Console.WriteLine("Prob. Sprinkler | Not Cloudy:              Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.5, model.CPTSprinklerPosterior[1].GetMean()[0]);
			Console.WriteLine("Prob. Rain      | Cloudy:                  Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.8, model.CPTRainPosterior[0].GetMean()[0]);
			Console.WriteLine("Prob. Rain      | Not Cloudy:              Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.2, model.CPTRainPosterior[1].GetMean()[0]);
			Console.WriteLine("Prob. Wet Grass | Sprinkler, Rain:         Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.99, model.CPTWetGrassPosterior[0][0].GetMean()[0]);
			Console.WriteLine("Prob. Wet Grass | Sprinkler, Not Rain      Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.9, model.CPTWetGrassPosterior[0][1].GetMean()[0]);
			Console.WriteLine("Prob. Wet Grass | Not Sprinkler, Rain:     Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.9, model.CPTWetGrassPosterior[1][0].GetMean()[0]);
			Console.WriteLine("Prob. Wet Grass | Not Sprinkler, Not Rain: Ground truth: {0:0.00}, Inferred: {1:0.00}", 0.0, model.CPTWetGrassPosterior[1][1].GetMean()[0]);

			// -------------------------------------------------------------
			// Usage 3: Querying the model taking into account uncertainty
			//          of the parameters
			// -------------------------------------------------------------

			// Use posteriors we have just learnt
			Console.WriteLine("\n**********************************************");
			Console.WriteLine("Querying the model with uncertain ground truth");
			Console.WriteLine("**********************************************");
			double probRainGivenWetGrass1 = model.ProbRain(
				null, null, 0,
				model.ProbCloudyPosterior, model.CPTSprinklerPosterior, model.CPTRainPosterior, model.CPTWetGrassPosterior);
			double probRainGivenWetGrassNotCloudy1 = model.ProbRain(
				1, null, 0,
				model.ProbCloudyPosterior, model.CPTSprinklerPosterior, model.CPTRainPosterior, model.CPTWetGrassPosterior);
			Console.WriteLine("P(rain | grass is wet)              = {0:0.0000}", probRainGivenWetGrass1);
			Console.WriteLine("P(rain | grass is wet, not cloudy ) = {0:0.0000}", probRainGivenWetGrassNotCloudy1);
			Console.WriteLine("");
		}