Example #1
0
        public double[,] Price(double faceValue, int periods)
        {
            double[,] shortRateLattice = LatticeBuilder.ConstructLattice(this.rate, this.u, this.d, periods);

            double[,] bondPriceLattice = new double[periods + 1, periods + 1];

            // Initialize the face values
            for (int j = 0; j <= periods; j++)
            {
                bondPriceLattice[periods, j] = faceValue;
            }

            // Fill the rest of the matrix
            double[,] r = shortRateLattice;
            double[,] z = bondPriceLattice;

            for (int i = periods - 1; i >= 0; i--)
            {
                for (int j = i; j >= 0; j--)
                {
                    z[i, j] = 1 / (1 + r[i, j]) * (qu * z[i + 1, j + 1] + qd * z[i + 1, j]);
                }
            }

            return(z);
        }
Example #2
0
        public double[,] Price(double faceValue, int periods)
        {
            double[,] shortRateLattice = LatticeBuilder.ConstructLattice(this.rate, this.u, this.d, periods);

            var zcbPrice = this.zcb.Price(faceValue, periods);

            var optionPrice = new double[expirationPeriods + 1, expirationPeriods + 1];

            Func <double, double, double> putCallFunction = delegate(double strike, double price)
            {
                if (putCallType == PutCallType.Call)
                {
                    return(Math.Max(0, price - strike));
                }

                return(Math.Max(0, strike - price));
            };

            Func <double, double, double> americanEuropeanFunction = delegate(double optionValue, double earlyExercizeValue)
            {
                if (americanEuropeanType == AmericanEuropeanType.European)
                {
                    return(optionValue);
                }

                return(Math.Max(optionValue, earlyExercizeValue)); // American type
            };

            // Initialize the expiry
            for (int j = 0; j <= expirationPeriods; j++)
            {
                optionPrice[expirationPeriods, j] = putCallFunction(strike, zcbPrice[expirationPeriods, j]);
            }

            double[,] r = shortRateLattice;
            double[,] z = optionPrice;

            // Fill the rest of the matrix
            for (int i = expirationPeriods - 1; i >= 0; i--)
            {
                for (int j = i; j >= 0; j--)
                {
                    var optionValue        = 1 / (1 + r[i, j]) * (qu * z[i + 1, j + 1] + qd * z[i + 1, j]);
                    var earlyExercizeValue = putCallFunction(this.strike, zcbPrice[i, j]);
                    z[i, j] = americanEuropeanFunction(optionValue, earlyExercizeValue);
                }
            }

            return(z);
        }
        public double[,] Price(double strike)
        {
            double[,] stockPrice = LatticeBuilder.ConstructLattice(this.currentPrice, this.u, this.d, this.expirationPeriods);

            var optionPrice = new double[expirationPeriods + 1, expirationPeriods + 1];

            Func <double, double, double> putCallFunction = delegate(double _strike, double _price)
            {
                if (putCallType == PutCallType.Call)
                {
                    return(Math.Max(0, _price - _strike));
                }

                return(Math.Max(0, _strike - _price));
            };

            Func <double, double, double> americanEuropeanFunction = delegate(double _optionValue, double _earlyExerciseValue)
            {
                if (americanEuropeanType == AmericanEuropeanType.European)
                {
                    return(_optionValue);
                }

                return(Math.Max(_optionValue, _earlyExerciseValue)); // American type
            };

            // Initialize the expiry
            for (int j = 0; j <= expirationPeriods; j++)
            {
                optionPrice[expirationPeriods, j] = putCallFunction(strike, stockPrice[expirationPeriods, j]);
            }

            double[,] c = optionPrice;

            // Fill the rest of the matrix
            for (int i = expirationPeriods - 1; i >= 0; i--)
            {
                for (int j = i; j >= 0; j--)
                {
                    var optionValue        = (1 / riskFreeRate) * (qu * c[i + 1, j + 1] + qd * c[i + 1, j]);
                    var earlyExerciseValue = putCallFunction(strike, stockPrice[i, j]);
                    c[i, j] = americanEuropeanFunction(optionValue, earlyExerciseValue);
                }
            }

            return(c);
        }