Skip to content

Commit

Permalink
Merge pull request #46 from ccjeremylo/39-add-digital-payoff-binomial
Browse files Browse the repository at this point in the history
39 add digital payoff binomial
  • Loading branch information
ccjeremylo authored Oct 24, 2023
2 parents 4c26a6c + 2d3868c commit 7e1472f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cpp-ubuntu.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: C++
name: CI

on:
push:
Expand Down
8 changes: 8 additions & 0 deletions src/Lecture2/Options05.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ lecture2::Put::Put(int N) : lecture2::EurOption(N) {
SetPayoff(PutPayoff);
}

lecture2::DigitalCall::DigitalCall(int N) : lecture2::EurOption(N) {
SetPayoff(DigitalCallPayoff);
}

lecture2::DigitalPut::DigitalPut(int N) : lecture2::EurOption(N) {
SetPayoff(DigitalPutPayoff);
}

lecture2::DoubleBarrierCall::DoubleBarrierCall(int N, double UpperB, double LowerB) : lecture2::EurOption(N), UpperB_(UpperB), LowerB_(LowerB) {
SetPayoff(CallPayoff);
}
Expand Down
20 changes: 20 additions & 0 deletions src/Lecture2/Options05.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ class Put: public EurOption
double K_;
};

class DigitalCall: public EurOption
{
public:
DigitalCall(int N);
double GetK(){return K_;}

private:
double K_;
};

// Poor design!! Using a payoff class would be better
class DigitalPut: public EurOption
{
public:
DigitalPut(int N);
double GetK(){return K_;}

private:
double K_;
};

// Poor design!! should be using a decorator...
class DoubleBarrierCall: public EurOption
Expand Down
10 changes: 10 additions & 0 deletions src/Lecture2/payoff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ double lecture2::PutPayoff(double S, double K) {
if (S < K) {return K - S;}
return 0.0;
}

double lecture2::DigitalCallPayoff(double S, double K) {
if (S > K) {return 1.0;}
return 0.0;
}

double lecture2::DigitalPutPayoff(double S, double K) {
if (S < K) {return 1.0;}
return 0.0;
}
3 changes: 3 additions & 0 deletions src/Lecture2/payoff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ namespace lecture2 {
double CallPayoff(double z, double K);
double PutPayoff(double z, double K);

double DigitalCallPayoff(double z, double K);
double DigitalPutPayoff(double z, double K);

}
14 changes: 14 additions & 0 deletions tests/lecture2_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ TEST(L2, vanillaPutPayOffs)
EXPECT_EQ(lecture2::PutPayoff(100.2, 100.2), 0.0) << "Vanilla put ATM case failed!";
}

TEST(L2,digitalCallPayOffs)
{
EXPECT_EQ(lecture2::DigitalCallPayoff(10.1, 2.1), 1.0) << "Digital call ITM case failed!";
EXPECT_EQ(lecture2::DigitalCallPayoff(2.3, 10.4), 0.0) << "Digital call OTM case failed!";
EXPECT_EQ(lecture2::DigitalCallPayoff(100.2, 100.2), 0.0) << "Digital call ATM case failed!";
}

TEST(L2, digitalPutPayOffs)
{
EXPECT_EQ(lecture2::DigitalPutPayoff(10.1, 2.1), 0.0) << "Digital put OTM case failed!";
EXPECT_EQ(lecture2::DigitalPutPayoff(2.3, 10.4), 1.0) << "Digital put ITM case failed!";
EXPECT_EQ(lecture2::DigitalPutPayoff(100.2, 100.2), 0.0) << "Digital put ATM case failed!";
}

TEST(L2, CRRBinomialTest)
{
double S0 = 110.0;
Expand Down
24 changes: 14 additions & 10 deletions tests/lecture3_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,22 @@ TEST(L3, americanOption)
lecture2::BinModel BinModel1 = lecture2::BinModel(S0, U, D, R);
lecture3::Call CallOption = lecture3::Call(K, N);
lecture3::Put PutOption = lecture3::Put(K, N);
lecture3::Call CallOption2N = lecture3::Call(K, 2*N);
lecture3::Put PutOption2N = lecture3::Put(K, 2*N);

// Vanilla Call
double am_call_price = CallOption.PriceBySnell(BinModel1);
EXPECT_TRUE(am_call_price > 0.0);
// American Call
double price_bs_call = lecture2::call_price(S0, K, r, sigma, T);
double abs_diff_call = am_call_price -price_bs_call;
EXPECT_TRUE(abs_diff_call > 0.0);
EXPECT_TRUE(price_bs_call > 0.0);
double am_call_price = CallOption.PriceBySnell(BinModel1);
EXPECT_TRUE(am_call_price > price_bs_call);
double am_call_price_2N = CallOption2N.PriceBySnell(BinModel1);
EXPECT_TRUE(am_call_price_2N > am_call_price);

// Vanilla Put
double am_put_price = PutOption.PriceBySnell(BinModel1);
EXPECT_TRUE(am_put_price > 0.0);
// American Put
double price_bs_put = lecture2::put_price(S0, K, r, sigma, T);
double abs_diff_put = am_put_price -price_bs_put;
EXPECT_TRUE(abs_diff_put > 0.0);
EXPECT_TRUE(price_bs_put > 0.0);
double am_put_price = PutOption.PriceBySnell(BinModel1);
EXPECT_TRUE(am_put_price > price_bs_put);
double am_put_price_2N = PutOption2N.PriceBySnell(BinModel1);
EXPECT_TRUE(am_put_price_2N > am_put_price);
}

0 comments on commit 7e1472f

Please sign in to comment.