Esempio n. 1
0
        public void GetState()
        {
            double mu             = 0.1;
            double sigma          = 0.3;
            double dt             = 0.1;
            int    maturityInyear = 10;
            int    numOfPath      = 100;
            double initialValue   = 100;
            int    seed           = 1;
            var    rand           = new GaussianRandomNumber(
                seed);
            var und = new BsUnderlying(mu: mu, sigma: sigma, initialValue: initialValue,
                                       dt: dt, maturityInYear: maturityInyear, numOfPath: numOfPath, random: rand);

            double expected = initialValue;

            Assert.AreEqual(und.GetState(pathNo: 0, grid: 0), expected);

            expected = expected * Math.Exp((mu - 0.5 * sigma * sigma) * dt +
                                           sigma * und.BrownianMotion[0][1]);
            Assert.AreEqual(und.GetState(pathNo: 0, grid: 1), expected);

            expected = expected * Math.Exp((mu - 0.5 * sigma * sigma) * dt +
                                           sigma * und.BrownianMotion[0][2]);
            Assert.AreEqual(und.GetState(pathNo: 0, grid: 2), expected);
        }
 public void XvaPricer()
 {
     double dt             = 1;
     int    maturityInYear = 10;
     int    numOfPath      = 100;
     var    random         = new GaussianRandomNumber(seed: 1);
     var    underlying     = new BsUnderlying(
         initialValue: 100, sigma: 0.3, mu: 0.0, dt: dt,
         maturityInYear: maturityInYear,
         numOfPath: numOfPath,
         random: random);
     var payoff = new European(strike: 105, type: European.PayOffType.Call);
     //ContinuationValue
     //Integrate()
 }
Esempio n. 3
0
        public void GaussianRandom()
        {
            int size   = 1000;
            var rand   = new GaussianRandomNumber(0);
            var values = new List <double>();

            for (int i = 0; i < size; ++i)
            {
                values.Add(rand.Next());
            }
            double mean     = values.Mean();
            double variance = values.Variance();
            double delta    = 1 / Math.Sqrt(size);

            Assert.AreEqual(mean, 0.0, delta);
            Assert.AreEqual(variance, 1.0, 0.1);
        }
Esempio n. 4
0
        public void meshTest()
        {
            Func <double, double> f = (double x) => { return(x * x); };
            var    random           = new GaussianRandomNumber(seed: 1);
            int    numOfPath        = 100;
            int    numOfMesh        = 100;
            double dt           = 0.1;
            int    maturityYear = 1;
            double mu           = 0.1;
            double sigma        = 0.3;
            var    und          = new BsUnderlying(initialValue: 100, sigma: sigma, mu: mu,
                                                   dt: dt, maturityInYear: maturityYear, numOfPath: numOfPath, random: random);
            LogNormalDensity density = new LogNormalDensity(mu, sigma);
            var stochasticMesh       = new StochasticMeshOperator(numOfMesh,
                                                                  und, density);
            int startGrid = 3;
            int endGrid   = 10;

            double state    = 0.1;
            double actual   = stochasticMesh.Get(f, startGrid, endGrid)(state);
            double expected = 0.0;

            for (int i = 0; i < numOfPath; ++i)
            {
                double xEnd = und.GetLastState(i);
                IEnumerable <double> mesh
                    = from index in Enumerable.Range(0, numOfMesh)
                      select und.GetState(index, startGrid);

                double denominator = mesh.Select(
                    x => density.density(dt * startGrid,
                                         maturityYear, x, xEnd)).Average();
                expected += f(xEnd) * density.density(startGrid * dt, endGrid * dt,
                                                      state, xEnd);
            }
            Assert.AreEqual(expected, actual, 1e-10);
        }