public ActionResult MeanDesvSimulation(double mean, double std_dev, int numberOfEvents)
        {
            // TODO: Replace harcoded values by import values of excel.
            // We need a file with the ranks and cumulative frequency
            var rankCount = 12;

            //double[] limits = new double[rankCount];
            Rank[] ranks = new Rank[rankCount];

            for (int i = 0; i < rankCount; i++)
            {
                //limits[i] = i * 10;
                ranks[i] = new Rank(i * 10, i * 10 + 10);
            }

            // Obtain Model
            var myModel = new MonteCarloModel(rankCount, ranks, mean, std_dev);

            ViewBag.ValuesInOrder = myModel.ValuesInOrderOfAppearance.Take(numberOfEvents).ToList();
            values = myModel.ValuesInOrderOfAppearance;

            return(View(myModel.MyDistribution));
        }
Exemple #2
0
        public async Task <IActionResult> CalcMonteCarloAsync(MonteCarloModel model)
        {
            try
            {
                double S0        = model.S0;
                double K         = model.K;
                double T         = model.T;
                double sigma     = model.sigma / 100;
                double r         = model.r / 100;
                double timestamp = model.timestamp;
                double samples   = model.samples;

                string json = $"{{\"S0\": {S0},\"K\": {K},\"T\": {T},\"sigma\": {sigma},\"r\": {r},\"timestamp\": {timestamp},\"samples\": {samples}}}";

                var content = new StringContent(json, Encoding.UTF8, "application/json");

                var response = await client.PostAsync("http://localhost:8888/api/v1/montecarlo", content);

                var responseString = await response.Content.ReadAsStringAsync();

                var result = double.TryParse(responseString, out _);

                if (!result)
                {
                    responseString = "ERROR";
                }

                TempData["value"] = "Predicted Price is: " + responseString;
                return(RedirectToAction("MonteCarlo"));
            }
            catch
            {
                TempData["value"] = "The error was occure";
                return(RedirectToAction("MonteCarlo"));
            }
        }
Exemple #3
0
        public IActionResult MonteCarlo()
        {
            var model = new MonteCarloModel();

            return(View(model));
        }
        /// <summary>
        /// Gets the price.
        /// </summary>
        /// <returns></returns>
        public double[,] GetPriceAndGreeks()
        {
            var n       = _resetAmounts.Length;
            var tList   = new List <double>();
            var volList = new List <double>();

            for (int i = 0; i < n; i++)
            {
                if (_resetDays[i] > 0)
                {
                    tList.Add(Convert.ToDouble(_resetDays[i]) / daybasis);
                    volList.Add(_volsToResets[i]);
                }
            }
            IVector t     = new DoubleVector(tList.ToArray());
            var     times = new SparseVector(t);

            // Vols
            var v  = new double[n, 1];
            int n1 = volList.Count;

            for (int idx = 0; idx < n1; idx++)
            {
                v[idx, 0] = volList[idx];
            }
            //Matrix vols = new Matrix(v);
            var variances = CalcForwardVariance(times.Data, volList.ToArray());
            var sigmas    = Sqrt(variances);
            var _vf       = new DoubleVector(variances);
            var vf        = new SparseVector(_vf, _vf.Length);
            //Drift
            var rates      = GetForwardRates(times.Data);
            var yields     = GetForwardYields(times.Data);
            var nrates     = times.Length;
            var _drifts    = new double[nrates];
            var _logdrifts = new double[nrates];

            //IVector driftsVector = new DoubleVector(_drifts);
            //SparseVector drifts = new SparseVector(driftsVector, driftsVector.Length);
            for (int idx = 0; idx < nrates; idx++)
            {
                _drifts[idx]    = rates[idx] - yields[idx];
                _logdrifts[idx] = _drifts[idx] - _vf[idx] * 0.5;
            }
            var ld        = new DoubleVector(_logdrifts);
            var logdrifts = new SparseVector(ld, ld.Length);
            // Discount
            var df = System.Math.Exp(-ForwardRate(0, _timeToExpiry) * _timeToExpiry);
            // 6 identical paths, price, delta, gamma, vega, theta, rho
            var pathGen = new SinglePathGenerator(Rng(_seed), logdrifts, vf, times);
            var pathPr  = new CliquetPathPricer(_payoffFunc, df, _useAntithetic, _spot, _strike, _floor, _cap, _nobsin, _nobsout, _resetDays, _resetAmounts, rates, yields, sigmas);
            //PathPricer deltaPr = PathFactory.GetPath("Delta", df, true, _spot, _strike, _floor, _cap, _resetDays, _resetAmounts, sigmas, _nobsin, _nobsout);
            var accumulatorList = new List <RiskStatistics>();

            for (int i = 0; i < 6; i++)
            {
                accumulatorList.Add(new RiskStatistics());
            }
            // Price path
            MonteCarloModel mcm = new MonteCarloModel(pathGen, pathPr, accumulatorList);

            mcm.AddMultiVarSamples(_numSimulations);
            var price = accumulatorList[0].Mean;
            var delta = accumulatorList[1].Mean;
            var gamma = accumulatorList[2].Mean;
            var vega  = accumulatorList[3].Mean;
            var theta = accumulatorList[4].Mean;
            var rho   = accumulatorList[5].Mean;
            var res   = new[, ] {
                { price, accumulatorList[0].ErrorEstimate },
                { delta, accumulatorList[1].ErrorEstimate },
                { gamma, accumulatorList[2].ErrorEstimate },
                { vega, accumulatorList[3].ErrorEstimate },
                { theta, accumulatorList[4].ErrorEstimate },
                { rho, accumulatorList[5].ErrorEstimate }
            };

            return(res);
        }