public ModelData[] InferModelData(double[] trainingData1, double[] trainingData2) { ModelData[] posteriors = new ModelData[2]; posteriors[0] = cyclist1.InferModelData(trainingData1); posteriors[1] = cyclist2.InferModelData(trainingData2); return posteriors; }
public static void RunCyclingTime2() { double[] trainingData = new double[] { 13, 17, 16, 12, 13, 12, 14, 18, 16, 16 }; ModelData initPriors = new ModelData( Gaussian.FromMeanAndPrecision(1.0, 0.01), Gamma.FromShapeAndScale(2.0, 0.5)); //Train the model CyclistTraining cyclistTraining = new CyclistTraining(); cyclistTraining.CreateModel(); cyclistTraining.SetModelData(initPriors); ModelData posteriors1 = cyclistTraining.InferModelData(trainingData); Console.WriteLine("Average travel time = " + posteriors1.AverageTimeDist); Console.WriteLine("Traffic noise = " + posteriors1.TrafficNoiseDist); //Make predictions based on the trained model CyclistPrediction cyclistPrediction = new CyclistPrediction(); cyclistPrediction.CreateModel(); cyclistPrediction.SetModelData(posteriors1); Gaussian tomorrowsTimeDist = cyclistPrediction.InferTomorrowsTime(); double tomorrowsMean = tomorrowsTimeDist.GetMean(); double tomorrowsStdDev = Math.Sqrt(tomorrowsTimeDist.GetVariance()); Console.WriteLine("Tomorrows average time: {0:f2}", tomorrowsMean); Console.WriteLine("Tomorrows standard deviation: {0:f2}", tomorrowsStdDev); Console.WriteLine("Probability that tomorrow's time is < 18 min: {0}", cyclistPrediction.InferProbabilityTimeLessThan(18.0)); //Second round of training double[] trainingData2 = new double[] { 17, 19, 18, 21, 15 }; cyclistTraining.SetModelData(posteriors1); ModelData posteriors2 = cyclistTraining.InferModelData(trainingData2); Console.WriteLine("\n2nd training pass"); Console.WriteLine("Average travel time = " + posteriors2.AverageTimeDist); Console.WriteLine("Traffic noise = " + posteriors2.TrafficNoiseDist); //Predictions based on two rounds of training cyclistPrediction.SetModelData(posteriors2); tomorrowsTimeDist = cyclistPrediction.InferTomorrowsTime(); tomorrowsMean = tomorrowsTimeDist.GetMean(); tomorrowsStdDev = Math.Sqrt(tomorrowsTimeDist.GetVariance()); Console.WriteLine("Tomorrows average time: {0:f2}", tomorrowsMean); Console.WriteLine("Tomorrows standard deviation: {0:f2}", tomorrowsStdDev); Console.WriteLine("Probability that tomorrow's time is < 18 min: {0}", cyclistPrediction.InferProbabilityTimeLessThan(18)); }
public void SetModelData(ModelData modelData) { cyclist1.SetModelData(modelData); cyclist2.SetModelData(modelData); }
public virtual void SetModelData(ModelData priors) { AverageTimePrior.ObservedValue = priors.AverageTimeDist; TrafficNoisePrior.ObservedValue = priors.TrafficNoiseDist; }
public void SetModelData(ModelData[] modelData) { cyclist1.SetModelData(modelData[0]); cyclist2.SetModelData(modelData[1]); }
public static void RunCyclingTime5() { double[] trainingData1 = new double[] { 13, 17, 16, 12, 13, 12, 14, 18, 16, 16, 27, 32 }; double[] trainingData2 = new double[] { 16, 18, 21, 15, 17, 22, 28, 16, 19, 33, 20, 31 }; ModelData initPriors = new ModelData( Gaussian.FromMeanAndPrecision(15.0, 0.01), Gamma.FromShapeAndScale(2.0, 0.5)); //Train the model TwoCyclistsTraining cyclistsTraining = new TwoCyclistsTraining(); cyclistsTraining.CreateModel(); cyclistsTraining.SetModelData(initPriors); ModelData[] posteriors1 = cyclistsTraining.InferModelData(trainingData1, trainingData2); Console.WriteLine("Cyclist 1 average travel time: {0}", posteriors1[0].AverageTimeDist); Console.WriteLine("Cyclist 1 traffic noise: {0}", posteriors1[0].TrafficNoiseDist); Console.WriteLine("Cyclist 1 average travel time: {0}", posteriors1[1].AverageTimeDist); Console.WriteLine("Cyclist 2 traffic noise: {0}", posteriors1[1].TrafficNoiseDist); //Make predictions based on the trained model TwoCyclistsPrediction cyclistsPrediction = new TwoCyclistsPrediction(); cyclistsPrediction.CreateModel(); cyclistsPrediction.SetModelData(posteriors1); Gaussian[] posteriors2 = cyclistsPrediction.InferTomorrowsTime(); Console.WriteLine("Cyclist1 tomorrow's travel time: {0}", posteriors2[0]); Console.WriteLine("Cyclist2 tomorrow's travel time: {0}", posteriors2[1]); Gaussian timeDifference = cyclistsPrediction.InferTimeDifference(); Bernoulli cyclist1IsFaster = cyclistsPrediction.InferCyclist1IsFaster(); Console.WriteLine("Time difference: {0}", timeDifference); Console.WriteLine("Probability that cyclist 1 is faster: {0}", cyclist1IsFaster); }
public static void RunCyclingTime4() { double[] trainingData = new double[] { 13, 17, 16, 12, 13, 12, 14, 18, 16, 16, 27, 32 }; ModelData initPriors = new ModelData( Gaussian.FromMeanAndPrecision(15.0, 0.01), Gamma.FromShapeAndScale(2.0, 0.5)); CyclistWithEvidence cyclistWithEvidence = new CyclistWithEvidence(); cyclistWithEvidence.CreateModel(); cyclistWithEvidence.SetModelData(initPriors); double logEvidence = cyclistWithEvidence.InferEvidence(trainingData); ModelDataMixed initPriorsMixed; initPriorsMixed.AverageTimeDist = new Gaussian[] { new Gaussian(15.0, 100), new Gaussian(30.0, 100) }; initPriorsMixed.TrafficNoiseDist = new Gamma[] { new Gamma(2.0, 0.5), new Gamma(2.0, 0.5) }; initPriorsMixed.MixingDist = new Dirichlet(1, 1); CyclistMixedWithEvidence cyclistMixedWithEvidence = new CyclistMixedWithEvidence(); cyclistMixedWithEvidence.CreateModel(); cyclistMixedWithEvidence.SetModelData(initPriorsMixed); double logEvidenceMixed = cyclistMixedWithEvidence.InferEvidence(trainingData); Console.WriteLine("Log evidence for single Gaussian: {0:f2}", logEvidence); Console.WriteLine("Log evidence for mixture of two Gaussians: {0:f2}", logEvidenceMixed); }