public async Task CheckLogisticRegressions() { SetupSimpleMocks(out Mock <ILocationClustering> mockLocationClustering, out Mock <ILegRepository> mockLegRepository, out Mock <IGeocodingDbSync> mockGeocodingDbSync, out Mock <ISupervisedLearning <LogisticRegression, double[], double> > mockLogisticRegressionAnalysis); // train module PickupPrediction instance = new PickupPrediction(mockLocationClustering.Object, mockLegRepository.Object, mockGeocodingDbSync.Object, mockLogisticRegressionAnalysis.Object) { FareClassIntervals = new double[] { 8.9, 23.0, 30.5 } }; await instance.LearnFromDates(DateTime.Now.AddDays(-2), DateTime.Now.AddDays(2)); // test method IEnumerable <LogisticRegression> regressions = instance.GetLogisticRegressions(0, 1); Assert.Equal(3, regressions.Count()); for (int i = 0; i < 3; i++) { LogisticRegression lrx = new LogisticRegression { Intercept = 0, NumberOfClasses = 2, NumberOfInputs = 3, NumberOfOutputs = 1 }; AssertRegressionsEqual(lrx, regressions.ElementAt(i)); } }
// check a function that gives a list of probabilities for classifications such as // fare class or number of pickups private async Task CheckProbabilityFunctionGivenFare(LogisticRegression[] lr, Func <PickupPrediction, double[], double[], double, double, double, double, IEnumerable <double> > testFunc, double pickupDelay, double duration, double fare, double interval, bool nullPickupDelays ) { Console.WriteLine("CheckProbabilityFunctionGivenFare called"); Mock <ILocationClustering> mockLocationClustering; Mock <ILegRepository> mockLegRepository; Mock <IGeocodingDbSync> mockGeocodingDbSync; Mock <ISupervisedLearning <LogisticRegression, double[], double> > mockLogisticRegressionAnalysis; if (nullPickupDelays) { SetupSimpleMocks2(out mockLocationClustering, out mockLegRepository, out mockGeocodingDbSync, out mockLogisticRegressionAnalysis); } else { SetupSimpleMocks(out mockLocationClustering, out mockLegRepository, out mockGeocodingDbSync, out mockLogisticRegressionAnalysis); } double[] fareClassIntervals = { 8.9, 23.0, 30.5 }; IEnumerable <Leg> legs = await mockLegRepository.Object.ListAsync(); SetupMockLogisticRegressionAnalysis(lr, mockLogisticRegressionAnalysis, fareClassIntervals, legs, out LogisticRegression[] mockLr); PickupPrediction instance = new PickupPrediction(mockLocationClustering.Object, mockLegRepository.Object, mockGeocodingDbSync.Object, mockLogisticRegressionAnalysis.Object) { FareClassIntervals = fareClassIntervals }; await instance.LearnFromDates(DateTime.Now.AddDays(-2), DateTime.Now.AddDays(2)); IEnumerable <double> actualProbabilities = testFunc(instance, new double[] { 0, 0 }, new double[] { 0, 1 }, pickupDelay, duration, fare, interval); double[] pickupProbs = new double[actualProbabilities.Count()]; for (int i = 0; i < pickupProbs.Length; i++) { pickupProbs[i] = ComputeDensityEstimation(pickupDelay, duration, interval, legs.Where(l => l.NumOfPassengersPickedUp == i + 1)); } CheckProbabilityResultsGivenFare(pickupDelay, duration, mockLr, actualProbabilities, pickupProbs, fare, await instance.GetMaxNumberOfPickups(), fareClassIntervals, legs, mockLogisticRegressionAnalysis); }
// check a function that gives a list of probabilities for classifications such as // fare class or number of pickups private async Task CheckProbabilityFunction <T3>(LogisticRegression[] lr, Func <PickupPrediction, double[], double[], double, double, T3, double, IEnumerable <double> > testFunc, double pickupDelay, double duration, T3 in3, double interval, bool nullPickupDelays, Func <Leg, bool> predicate = null ) { Console.WriteLine("CheckProbabilityFunction called"); Mock <ILocationClustering> mockLocationClustering; Mock <ILegRepository> mockLegRepository; Mock <IGeocodingDbSync> mockGeocodingDbSync; Mock <ISupervisedLearning <LogisticRegression, double[], double> > mockLogisticRegressionAnalysis; if (nullPickupDelays) { SetupSimpleMocks2(out mockLocationClustering, out mockLegRepository, out mockGeocodingDbSync, out mockLogisticRegressionAnalysis); } else { SetupSimpleMocks(out mockLocationClustering, out mockLegRepository, out mockGeocodingDbSync, out mockLogisticRegressionAnalysis); } double[] fareClassIntervals = { 8.9, 23.0, 30.5 }; IEnumerable <Leg> legs = await mockLegRepository.Object.ListAsync(); if (predicate != null) { legs = legs.Where(predicate); } SetupMockLogisticRegressionAnalysis(lr, mockLogisticRegressionAnalysis, fareClassIntervals, legs, out LogisticRegression[] mockLr); PickupPrediction instance = new PickupPrediction(mockLocationClustering.Object, mockLegRepository.Object, mockGeocodingDbSync.Object, mockLogisticRegressionAnalysis.Object) { FareClassIntervals = fareClassIntervals }; await instance.LearnFromDates(DateTime.Now.AddDays(-2), DateTime.Now.AddDays(2)); IEnumerable <double> actualProbabilities = testFunc(instance, new double[] { 0, 0 }, new double[] { 0, 1 }, pickupDelay, duration, in3, interval); double pickupProb = ComputeDensityEstimation(pickupDelay, duration, interval, legs); CheckProbabilityResults(pickupDelay, duration, mockLr, actualProbabilities, pickupProb); }