### 练习题:10.6.1
A digital call option with maturity T and strike K. This has a payoff equal to 1 if the stock price at maturity is greater than K and 0 other wise. “Binary option” or even “Boolean option” would perhaps be more logical names, but digital is the standard term. You can get European,American, etc., versions of digital options.
A digital put option has payoff equal to 1 if the stock price at maturity is less than K and 0 otherwise.
Write classes DigitalCallOption and DigitalPutOption. Price them using the MonteCarloPricer. See Appendix A if you do not recall what a digital option is.
为了实现这个功能,写了4个文件:
总体上难度并不是很大,主要还是cpp语法上的问题导致实现耗费时间比较多,在使用vs2022的时候没有使用解决方案,并且添加具体的文件,也没有使用cMake去调整依赖,导致出现问题。
以下答案仅供参考,不保证完全正确。
全部代码上传到了:practice/FMLib10 · 云金杞/learn_cpp_series - 码云 - 开源中国 (gitee.com)
DigitalCallOption.h
#pragma once #include "stdafx.h" #include "BlackScholesModel.h" #include "PathIndependentOption.h" class DigitalCallOption : public PathIndependentOption { public: DigitalCallOption(); double strike; double maturity; double payoff(double stockAtMaturity) const; double getMaturity() const; }; void testDigitalCallOption();
DigitalCallOption.cpp
#include "DigitalCallOption.h" #include "MonteCarloPricer.h" #include "matlib.h" #include "testing.h" DigitalCallOption::DigitalCallOption() : strike(0.0), maturity(0.0) { } double DigitalCallOption::payoff(double stockAtMaturity) const { if (stockAtMaturity <= strike) { return 0.0; } else { return 1.0; } } double DigitalCallOption::getMaturity() const { return maturity; } ////////////////////////// // // Test the call option class // // ////////////////////////// static void testDigitalCallOptionPrice() { rng("default"); BlackScholesModel m; m.volatility = 0.1; m.riskFreeRate = 0.05; m.stockPrice = 100.0; m.drift = 0.1; DigitalCallOption dco; dco.strike = 1100000; dco.maturity = 2; DigitalCallOption dco1; dco1.strike = 0.1; dco1.maturity = dco.maturity; // our pricer can price puts and calls MonteCarloPricer pricer; double price0 = pricer.price(dco, m); ASSERT_APPROX_EQUAL(price0, 0.0, 0.1); double price1 = pricer.price(dco1, m); ASSERT_APPROX_EQUAL(price1, 1.0, 0.1); } static void testPayoff() { DigitalCallOption dco; dco.strike = 105.0; dco.maturity = 2.0; ASSERT_APPROX_EQUAL(dco.payoff(110.0), 1, 0.001); ASSERT_APPROX_EQUAL(dco.payoff(100.0), 0, 0.001); } void testDigitalCallOption() { TEST(testPayoff); TEST(testDigitalCallOptionPrice); }
DigitalPutOption.h
#include "stdafx.h" #include "BlackScholesModel.h" #include "PathIndependentOption.h" class DigitalPutOption : public PathIndependentOption { public: DigitalPutOption(); double strike; double maturity; double payoff(double stockAtMaturity) const; double getMaturity() const; }; void testDigitalPutOption();
DigitalPutOption.cpp
#include "DigitalPutOption.h" #include "MonteCarloPricer.h" #include "matlib.h" #include "testing.h" DigitalPutOption::DigitalPutOption() : strike(0.0), maturity(0.0) { } double DigitalPutOption::payoff(double stockAtMaturity) const { if (stockAtMaturity >= strike) { return 0.0; } else { return 1.0; } } double DigitalPutOption::getMaturity() const { return maturity; } ////////////////////////// // // Test the call option class // // ////////////////////////// static void testDigitalPutOptionPayoff() { DigitalPutOption dpo; dpo.strike = 105.0; dpo.maturity = 2.0; ASSERT_APPROX_EQUAL(dpo.payoff(110.0), 0, 0.001); ASSERT_APPROX_EQUAL(dpo.payoff(100.0), 1, 0.001); } static void testDigitalPutOptionPrice() { rng("default"); BlackScholesModel m; m.volatility = 0.1; m.riskFreeRate = 0.05; m.stockPrice = 100.0; m.drift = 0.1; DigitalPutOption dpo; dpo.strike = 1100000; dpo.maturity = 2; DigitalPutOption dpo1; dpo1.strike = 0.1; dpo1.maturity = dpo.maturity; // our pricer can price puts and calls MonteCarloPricer pricer; double price0 = pricer.price(dpo, m); ASSERT_APPROX_EQUAL(price0, 1.0, 0.1); double price1 = pricer.price(dpo1, m); ASSERT_APPROX_EQUAL(price1, 0.0, 0.1); } void testDigitalPutOption() { TEST(testDigitalPutOptionPayoff); TEST(testDigitalPutOptionPrice); }