public void ConvertibleBondPricerTest() { //hardcoding parameters below, please change if necessary double sigma = 0.2; // volatility double T = 5; // time to expiry (years) double kappa = 1.0; // conversion ratio double B = 100; // principal Func <double, double> r = (t) => 0.05; // risk-free interest rate Func <double, double> rg = (t) => 0.05; // growth rate of stock Func <double, double> rc = (t) => 0.02; // credit spread //call schedule OptionalityPeriodParameters[] callScheduleArray = new OptionalityPeriodParameters[1]; callScheduleArray[0] = new OptionalityPeriodParameters(2, 5, 1.1); System.Collections.Generic.IEnumerable <OptionalityPeriodParameters> CallSchedule = callScheduleArray; //put scheule OptionalityPeriodParameters[] putScheduleArray = new OptionalityPeriodParameters[1]; putScheduleArray[0] = new OptionalityPeriodParameters(3, 3, 1.05); System.Collections.Generic.IEnumerable <OptionalityPeriodParameters> PutSchedule = putScheduleArray; //coupon payments System.Collections.Generic.List <CouponParameters> Coupons = new System.Collections.Generic.List <CouponParameters>(); for (int i = 0; i < 10; ++i) { Coupons.Add(new CouponParameters(0, T, 0.5 + i * 0.5, 4 / B)); } //callable bond parameter Pricers.Convertible.ConvertibleBondParameters cparams = new Pricers.Convertible.ConvertibleBondParameters(); cparams.Maturity = T; cparams.FaceValue = B; cparams.CallSchedule = CallSchedule; cparams.PutSchedule = null;// PutSchedule; cparams.Coupons = Coupons; //measure excution time for (int it = 4; it <= 4; it++) { ConvertibleBondPDEPricer CBSolver = new TFPricer(cparams, kappa, sigma, r, rg, rc); /*ConvertibleBondPDEPricer CBSolver = new AFVPricer(cparams, * spotPrice : B, * conversionRatio: kappa, * volatility: sigma, * riskFree: r, * growthRate: rg, * creditSpread: rc, * hazardRateFunc: null, * hazardRate: 0.02, * defaultRate: 1, * recoveryFactor: 0, * isDirtyPrice: false);*/ CBSolver.print_time_interval = 0.1; CBSolver.OutDir = "D:"; CBSolver.GridSize = it * 200; System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); CBSolver.CreateAndSolvePDE(); stopwatch.Stop(); System.Diagnostics.Debug.WriteLine("Time cost: {0} ms", stopwatch.ElapsedMilliseconds); double ans = CBSolver.getPrice(B); System.Diagnostics.Debug.WriteLine("price(0, 100) = {0} at grid {1}", ans, CBSolver.GridSize); using (var stream = new System.IO.StreamWriter("D:/timecost.txt", true)) { stream.WriteLine("{0} {1}", CBSolver.GridSize, stopwatch.ElapsedMilliseconds); } using (var stream = new System.IO.StreamWriter("D:/value.txt", true)) { stream.WriteLine("{0} {1}", CBSolver.GridSize, ans); } } }
private static void ExcelConvertibleBondPricer( double sigma, double T, double kappa, double B, double risk_free, double credit_spread, double stock_growth, double[,] couponInfoMatrix, double[,] callInfoMatrix, double[,] putInfoMatrix) { Func <double, double> r = (t) => risk_free; // risk-free interest rate Func <double, double> rg = (t) => stock_growth; // growth rate of stock Func <double, double> rc = (t) => credit_spread; // credit spread //call schedule if (callInfoMatrix.GetLength(1) != 3) { throw new ArgumentException("Call information should have 3 columns"); } OptionalityPeriodParameters[] callScheduleArray = new OptionalityPeriodParameters[callInfoMatrix.GetLength(0)]; for (int i = 0; i < callInfoMatrix.GetLength(0); ++i) { callScheduleArray[i] = new OptionalityPeriodParameters(callInfoMatrix[i, 0], callInfoMatrix[i, 1], callInfoMatrix[i, 2]); } System.Collections.Generic.IEnumerable <OptionalityPeriodParameters> CallSchedule = callScheduleArray; //put scheule if (putInfoMatrix.GetLength(1) != 3) { throw new ArgumentException("Put information should have 3 columns"); } OptionalityPeriodParameters[] putScheduleArray = new OptionalityPeriodParameters[putInfoMatrix.GetLength(0)]; for (int i = 0; i < putInfoMatrix.GetLength(0); ++i) { putScheduleArray[i] = new OptionalityPeriodParameters(putInfoMatrix[i, 0], putInfoMatrix[i, 1], putInfoMatrix[i, 2]); } System.Collections.Generic.IEnumerable <OptionalityPeriodParameters> PutSchedule = putScheduleArray; //coupon payments System.Collections.Generic.List <CouponParameters> Coupons = new System.Collections.Generic.List <CouponParameters>(); for (int i = 0; i < couponInfoMatrix.GetLength(0); ++i) { Coupons.Add(new CouponParameters(0, T, couponInfoMatrix[i, 0], couponInfoMatrix[i, 1])); } //callable bond parameter ConvertibleBondParameters cparams = new ConvertibleBondParameters(); cparams.Maturity = T; cparams.FaceValue = B; cparams.CallSchedule = CallSchedule; cparams.PutSchedule = PutSchedule; cparams.Coupons = Coupons; CBSolver = new TFPricer(cparams, kappa, sigma, rc, r, rg); CBSolver.print_time_interval = 0.25; CBSolver.OutDir = "D:"; CBSolver.GridSize = 300; System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); CBSolver.CreateAndSolvePDE(); stopwatch.Stop(); System.Diagnostics.Debug.WriteLine("Time cost: {0} ms", stopwatch.ElapsedMilliseconds); using (var stream = new System.IO.StreamWriter("D:/timecost.txt", true)) { stream.WriteLine("{0} {1}", CBSolver.GridSize, stopwatch.ElapsedMilliseconds); } }