-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInferDemoProgram_explicado.cs
More file actions
executable file
·58 lines (56 loc) · 2.94 KB
/
InferDemoProgram_explicado.cs
File metadata and controls
executable file
·58 lines (56 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ML.Probabilistic.Models;
using Microsoft.ML.Probabilistic.Algorithms;
using Microsoft.ML.Probabilistic.Distributions;
using Microsoft.ML.Probabilistic.Math;
namespace ConsoleApp1
{
class InferDemoProgram
{
static void Main(string[] args)
{
//**************Step 1: Define a probabilistic model*************************
//Assume that we know temperature of three days:
//Day 1: 13 Celsius, Day 2: 17 Celsius, and Day 3: 16 Celsius
double[] temp = new double[3] { 13, 17, 16 };
// Creating a Gaussian distribution and a Gamma distribution
Variable<double> averageTemp = Variable.GaussianFromMeanAndPrecision(15, 0.01).Named("Average Temperature");
Variable<double> precision = Variable.GammaFromShapeAndScale(2.0, 0.5).Named("Precision");
// Train the model
// using the Range object to handle the array of data efficiently
Range dataRange = new Range(temp.Length).Named("n");
VariableArray<double> daysTemp = Variable.Array<double>(dataRange);
daysTemp[dataRange] = Variable.GaussianFromMeanAndPrecision(averageTemp, precision).ForEach(dataRange);
daysTemp.ObservedValue = temp;
//**************Step 2: Creating an inference engine*************************
InferenceEngine ie = new InferenceEngine();
ie.ShowFactorGraph = true;
//**************Step 3: Execution of an inference query*************************
//Make predictions
//Add a prediction variable and retrain the model
Variable<double> tomorrowsTemp = Variable.GaussianFromMeanAndPrecision(averageTemp, precision).Named("Tomorrows Predicted Temperature");
//get the Gaussian distribution
Gaussian tomorrowsTempDist = ie.Infer<Gaussian>(tomorrowsTemp);
// get the mean
double tomorrowsMean = tomorrowsTempDist.GetMean();
//get the variance
double tomorrowsStdDev = Math.Sqrt(tomorrowsTempDist.GetVariance());
//Using Expectation Propagation - the default algorithm
if (!(ie.Algorithm is VariationalMessagePassing))
{
// Write out the results.
Console.WriteLine("Tomorrows predicted temperature: {0:f2} Celsius plus or minus {1:f2}", tomorrowsMean, tomorrowsStdDev);
// Ask other questions of the model
double probTempLessThan18Celsius = ie.Infer<Bernoulli>(tomorrowsTemp < 18.0).GetProbTrue();
Console.WriteLine("Probability that the temperature is less than 18 Celsius: {0:f2}", probTempLessThan18Celsius);
}
else
Console.WriteLine("Not run with Variational Message Passing!");
Console.ReadKey();
}
}
}