예제 #1
0
파일: GARCH.cs 프로젝트: w1r2p1/LeanTrading
        public Dictionary <string, object> GetBestParameters(Dictionary <string, object> guess)
        {
            // Create a guess for the solver
            var guessOmega = 1.0;
            var guessAlpha = 0.2;
            var guessBeta  = 0.2;

            if (guess != null)
            {
                guessOmega = (double)guess["Omega"];
                guessAlpha = (double)guess["Alpha"];
                guessBeta  = (double)guess["Beta"];
            }

            // Constrain guess to admissible range
            guessOmega = RestrictToOpenSet(guessOmega, 0.0, double.MaxValue);
            guessAlpha = RestrictToOpenSet(guessAlpha, 0.0, 1.0);
            guessBeta  = RestrictToOpenSet(guessBeta, 0.0, 1.0 - guessAlpha);

            var guessMucorr = guessAlpha + guessBeta;
            var guessMuema  = guessBeta / (guessAlpha + guessBeta);

            // Transform guess to solver coordinates
            var guessParameters = new double[3];

            guessParameters[0] = Math.Log(guessOmega);
            guessParameters[1] = -Math.Log(-Math.Log(guessMucorr));
            guessParameters[2] = -Math.Log(-Math.Log(guessMuema));

            var f = new GARCHMaxLikelihoodFunction(this);

            var nm = new NelderMead(3);

            nm.Function = solution => f.Value(solution);
            nm.Convergence.MaximumEvaluations = _maxIterations;

            nm.Maximize(guessParameters);
            var bestParameters = nm.Solution;

            // Transform parameters to GARCH parameters
            var omega  = Math.Exp(bestParameters[0]);
            var mucorr = Math.Exp(-Math.Exp(-bestParameters[1]));
            var muema  = Math.Exp(-Math.Exp(-bestParameters[2]));
            var beta   = mucorr * muema;
            var alpha  = mucorr - beta;

            double[] quantiles      = { 0.01, 0.05, 0.5 };
            var      quantileValues = GetQuantilPredictionsForParameters(omega, alpha, beta, quantiles);

            var results = new Dictionary <string, object>();

            results.Add("Omega", omega);
            results.Add("Alpha", alpha);
            results.Add("Beta", beta);
            results.Add("Szenarios", GetSzenarios(omega, alpha, beta));
            //results.Add("Likelihood", GetLogLikelihoodForParameters(omega, alpha, beta));
            results.Add("Vol", Math.Sqrt(GetLastResidualForParameters(omega, alpha, beta)));
            results.Add("Quantile=1%", quantileValues[0]);
            results.Add("Quantile=5%", quantileValues[1]);
            results.Add("Quantile=50%", quantileValues[2]);

            return(results);
        }