Beispiel #1
0
        private double[] PredictNextValue(IGaussianMixtureModelState model, IPredictionRequest request, double[][] trainingSet)
        {
            var N      = trainingSet.Length;
            var K      = trainingSet[0].Length;
            var result = new double[K];

            var yesterday           = trainingSet[N - 1];
            var yesterdayLikelihood = LogLikelihood.Calculate(new[] { yesterday }, model.Mixture.Coefficients, GetCovariances(model.Mixture.Components), GetMeans(model.Mixture.Components));

            Debug.WriteLine("Yesterday Likelihood : " + new Vector(yesterday) + " : " + yesterdayLikelihood + " ");

            var guessess       = FindMostSimilarObservations(model, trainingSet, yesterdayLikelihood, request.Tolerance);
            var bestGuessPlace = FindBestGuess(request, guessess);
            var tomorrow       = trainingSet[bestGuessPlace.PlaceInSequence + 1];
            var mostSimilar    = trainingSet[bestGuessPlace.PlaceInSequence];

            for (var k = 0; k < K; k++)
            {
                if (bestGuessPlace.PlaceInSequence != trainingSet.Length)
                {
                    result[k] = yesterday[k] + (tomorrow[k] - mostSimilar[k]);
                }
            }

            Debug.WriteLine("Predicted : " + new Vector(result) + " : " + LogLikelihood.Calculate(new[] { result }, model.Mixture.Coefficients, GetCovariances(model.Mixture.Components), GetMeans(model.Mixture.Components)));

            return(result);
        }
Beispiel #2
0
        private ObservationWithLikelihood <double[]> FindBestGuess(IPredictionRequest request, IList <ObservationWithLikelihood <double[]> > guessess)
        {
            var result = new ObservationWithLikelihood <double[]>()
            {
                Observation = new double[request.TrainingSet[0].Length]
            };
            var maxLogLikelihood = double.NegativeInfinity;

            for (var i = 0; i < guessess.Count; i++)
            {
                if (maxLogLikelihood < guessess[i].LogLikelihood)
                {
                    maxLogLikelihood       = guessess[i].LogLikelihood;
                    result.LogLikelihood   = guessess[i].LogLikelihood;
                    result.Observation     = guessess[i].Observation;
                    result.PlaceInSequence = guessess[i].PlaceInSequence;
                    result.NumberOfGuesses = guessess.Count;
                }
            }

            Debug.Write(result.PlaceInSequence + " : " + (new Vector(result.Observation)) + " " + result.NumberOfGuesses + " " + Environment.NewLine);

            return(result);
        }
Beispiel #3
0
        public IPredictionResult Predict(IGaussianMixtureModelState model, IPredictionRequest request)
        {
            var trainingSet = (double[][])request.TrainingSet.Clone();
            var predictions = new PredictionResult {
                Predicted = new double[request.NumberOfDays][]
            };

            for (int i = 0; i < request.NumberOfDays; i++)
            {
                var prediction = PredictNextValue(model, request, trainingSet);
                if (request.NumberOfDays > 1)
                {
                    trainingSet = new double[request.TrainingSet.Length + i + 1][];
                    request.TrainingSet.CopyTo(trainingSet, 0);
                    for (var j = 0; j < i + 1; j++)
                    {
                        trainingSet[request.TrainingSet.Length + j] = request.TestSet[j];
                    }
                }
                predictions.Predicted[i] = prediction;
            }

            return(predictions);
        }
        /// <summary>
        ///     Viterbi based prediction algorithm. For each observation in test set we perform following action :
        ///     1. Run Viterbi and find best state that generated last day observation
        ///     2. Prediction for next day is weighted average of means of predicting state emissions
        ///     3. Re-Train the model with new value added from training set
        ///     4. Return to step 1
        /// </summary>
        /// <typeparam name="TDistribution"></typeparam>
        /// <param name="model"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public IPredictionResult Predict <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request) where TDistribution : IDistribution
        {
            NumberOfIterations  = request.NumberOfTrainingIterations;
            LikelihoodTolerance = request.TrainingLikelihoodTolerance;
            var predictions = new PredictionResult {
                Predicted = new double[request.NumberOfDays][]
            };
            var trainingSet = (double[][])request.TrainingSet.Clone();

            for (int i = 0; i < request.NumberOfDays; i++)
            {
                var prediction = PredictNextValue(model, trainingSet);
                if (request.NumberOfDays > 1)
                {
                    trainingSet = new double[request.TrainingSet.Length + i + 1][];
                    request.TrainingSet.CopyTo(trainingSet, 0);
                    for (var j = 0; j < i + 1; j++)
                    {
                        trainingSet[request.TrainingSet.Length + j] = request.TestSet[j];
                    }
                    var iterations = NumberOfIterations;
                    ((IMachineLearningMultivariateModel)model).Train(trainingSet, iterations, LikelihoodTolerance);
                }
                predictions.Predicted[i] = prediction;
            }

            return(predictions);
        }
Beispiel #5
0
        public IPredictionResult Predict <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request)
            where TDistribution : IDistribution
        {
            var trainingSet = (double[][])request.TrainingSet.Clone();
            var predictions = new PredictionResult {
                Predicted = new double[request.NumberOfDays][]
            };

            for (int i = 0; i < request.NumberOfDays; i++)
            {
                var prediction = PredictNextValue(model, request, trainingSet);
                if (request.NumberOfDays > 1)
                {
                    trainingSet = new double[request.TrainingSet.Length + i + 1][];
                    request.TrainingSet.CopyTo(trainingSet, 0);
                    for (var j = 0; j < i + 1; j++)
                    {
                        trainingSet[request.TrainingSet.Length + j] = request.TestSet[j];
                    }
                }
                predictions.Predicted[i] = prediction;
            }

            return(predictions);
        }
Beispiel #6
0
        private double[] PredictNextValue <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request, double[][] trainingSet)
            where TDistribution : IDistribution
        {
            var N      = trainingSet.Length;
            var K      = trainingSet[0].Length;
            var result = new double[K];

            var yesterday           = trainingSet[N - 1];
            var forwardBackward     = new ForwardBackward(model.Normalized);
            var yesterdayLikelihood = forwardBackward.RunForward(Helper.Convert(new[] { yesterday }), model);

            Debug.WriteLine("Yesterday Likelihood : " + new Vector(yesterday) + " : " + yesterdayLikelihood + " ");

            var guessess       = FindMostSimilarObservations(model, trainingSet, yesterdayLikelihood, request.Tolerance);
            var bestGuessPlace = FindBestGuess(request, guessess);
            var tomorrow       = trainingSet[bestGuessPlace.PlaceInSequence + 1];
            var mostSimilar    = trainingSet[bestGuessPlace.PlaceInSequence];

            for (var k = 0; k < K; k++)
            {
                if (bestGuessPlace.PlaceInSequence != trainingSet.Length)
                {
                    result[k] = yesterday[k] + (tomorrow[k] - mostSimilar[k]);
                }
            }

            Debug.WriteLine("Predicted (for day " + N + ") : " + new Vector(result) + " : " + forwardBackward.RunForward(Helper.Convert(result), model));

            return(result);
        }
Beispiel #7
0
        public IPredictionResult Predict <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request) where TDistribution : IDistribution
        {
            var selectionMethod    = new TournamentSelection(TournamentSize);
            var crossoverAlgorithm = new Crossover(CrossoverProbability);
            var mutationAlgorithm  = new Mutator(MutationProbability);
            var evaluator          = new HmmEvaluator <TDistribution>(model, new ForwardBackward(true));

            var parameters = new GeneticSolverParameters
            {
                CrossOverProbability = CrossoverProbability,
                MutationProbability  = MutationProbability,
                NumberOfGenerations  = NumberOfGenerations,
                PopulationSize       = request.NumberOfDays * 10,
                TournamentSize       = TournamentSize
            };
            var predictions = new PredictionResult {
                Predicted = new double[request.NumberOfDays][]
            };

            var solver = new GeneticSolver(parameters, mutationAlgorithm, crossoverAlgorithm, evaluator, selectionMethod);
            var populationInitializer = new FromTimeSeriesRandomInitializer(request.TrainingSet);
            var population            = populationInitializer.Initialize <decimal>(parameters.PopulationSize, request.NumberOfDays, MutationProbability);

            for (int i = 0; i < population.Count; i++)
            {
                var chromosome = population[i];
                population[i].FintnessValue = evaluator.Evaluate(chromosome);
            }

            var result = solver.Solve(population);
            // Get best fitted chromosome
            var maximumFitness = result[0].FintnessValue;
            var solution       = result[0];

            foreach (var chromosome in result)
            {
                if (maximumFitness <= chromosome.FintnessValue)
                {
                    solution       = chromosome;
                    maximumFitness = chromosome.FintnessValue;
                }
            }
            // Convert it to array
            for (int i = 0; i < solution.Representation.Length; i++)
            {
                predictions.Predicted[i] = Array.ConvertAll(solution.Representation[i].Representation, x => (double)Convert.ChangeType(x, typeof(double)));
            }

            return(predictions);
        }
Beispiel #8
0
        public IPredictionResult Predict <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request) where TDistribution : IDistribution
        {
            if (!request.ValidateAlgorithmSpecificParameters())
            {
                return(null);
            }

            var n = Convert.ToInt32(request.AlgorithmSpecificParameters["NumberOfSamplePoints"]);
            var k = Convert.ToInt32(request.AlgorithmSpecificParameters["NumberOfWinningPoints"]);

            //var ratios = GetMaximumChangeRatios(request.TrainingSet);

            //var candidates = CreateStartingPool(request.TestSet[0], n, ratios);

/*            do
 *          {
 *              //var winningCandidate = CalculateWinningCandidates(k, candidates, model);
 *              //candidates = CreateCandidatePoolFromArray(winningCandidate, n, ratios);
 *          } while (Continue(request, candidates));*/

            var result = new PredictionResult()
            {
                Predicted = new double[1][]
            };

            //result.Predicted[0] = GetBestProbabilityCandidate(candidates);

            return(result);
        }