public void pdf()
        {
            double mu    = 0.0;
            double sigma = 1;
            double x     = 1;
            double y     = 1;
            double t     = 1.0;
            double s     = 0.0;

            double actual = new LogNormalDensity(
                mu, sigma).density(s, t, x, y);
            double expected = Normal.PDF(
                0, 1, (Math.Log(y / x) - (mu - 0.5 * sigma * sigma) * (t - s)) / (sigma * Math.Sqrt(t - s)));

            Assert.AreEqual(expected, actual, 1e-8);

            mu    = 0.1;
            sigma = 0.43;
            x     = 1;
            y     = 2;
            t     = 4.0;
            s     = 2.0;

            actual = new LogNormalDensity(
                mu, sigma).density(s, t, x, y);
            expected = Normal.PDF(
                0, 1, (Math.Log(y / x) - (mu - 0.5 * sigma * sigma) * (t - s)) / (sigma * Math.Sqrt(t - s)))
                       / (y * sigma * Math.Sqrt(t - s));
            Assert.AreEqual(expected, actual, 1e-8);
        }
示例#2
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);
        }