-
Notifications
You must be signed in to change notification settings - Fork 1
/
PutOption.cpp
65 lines (52 loc) · 1.64 KB
/
PutOption.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "PutOption.h"
#include "matlib.h"
Matrix PutOption::payoffAtMaturity( const Matrix& stockAtMaturity ) const {
Matrix val = getStrike() - stockAtMaturity;
val.positivePart();
return val;
}
double PutOption::price(
const MultiStockModel& msm ) const {
BlackScholesModel bsm =
msm.getBlackScholesModel(getStock());
double S = bsm.stockPrice;
double K = getStrike();
double sigma = bsm.volatility;
double r = bsm.riskFreeRate;
double T = getMaturity() - bsm.date;
double numerator = log( S/K ) + ( r + sigma*sigma*0.5)*T;
double denominator = sigma * sqrt(T );
double d1 = numerator/denominator;
double d2 = d1 - denominator;
return -S*normcdf(-d1) + exp(-r*T)*K*normcdf(-d2);
}
//////////////////////////
//
// Test the call option class
//
//
//////////////////////////
static void testPayoff() {
PutOption putOption;
putOption.setStrike( 105.0) ;
putOption.setMaturity( 2.0 );
ASSERT_APPROX_EQUAL( putOption.payoffAtMaturity(Matrix(110.0)).asScalar(), 0.0, 0.001);
ASSERT_APPROX_EQUAL( putOption.payoffAtMaturity(Matrix(100.0)).asScalar(), 5.0, 0.001);
}
static void testPutOptionPrice() {
PutOption putOption;
putOption.setStrike( 105.0 );
putOption.setMaturity( 2.0 );
BlackScholesModel bsm;
bsm.date = 1.0;
bsm.volatility = 0.1;
bsm.riskFreeRate = 0.05;
bsm.stockPrice = 100.0;
MultiStockModel msm(bsm);
double price = putOption.price( msm );
ASSERT_APPROX_EQUAL( price, 3.925, 0.01);
}
void testPutOption() {
TEST( testPutOptionPrice );
TEST( testPayoff );
}