Skip to content

Commit

Permalink
reformatted code with updated .clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
ccjeremylo committed Oct 25, 2023
1 parent 17a2f7c commit ace0d4b
Show file tree
Hide file tree
Showing 22 changed files with 516 additions and 323 deletions.
38 changes: 38 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
BasedOnStyle: Google

# indentation:
IndentWidth: 4
AccessModifierOffset: -4
IndentWrappedFunctionNames: 'true'
IndentCaseLabels: 'true'
NamespaceIndentation: Inner

# sorting:
SortIncludes: 'true'
SortUsingDeclarations: 'true'

# Must be 80 characters or less!
ColumnLimit: 80

# use \n instead of \r\n
UseCRLF: false

# spaces, not tabs!
UseTab: Never

# if (x) doStuff() is not allowed, bad style
AllowShortIfStatementsOnASingleLine: false

# change the next line to All for Alistair's textbook style
AlwaysBreakAfterReturnType: TopLevelDefinitions

# others
DerivePointerAlignment: 'true'
FixNamespaceComments: 'true'
IncludeBlocks: Merge
PointerAlignment: Left
ReferenceAlignment: Left
SpaceAfterLogicalNot: 'true'
SpaceAfterTemplateKeyword: 'true'
Standard: Auto
19 changes: 0 additions & 19 deletions .clang-format.sh

This file was deleted.

29 changes: 16 additions & 13 deletions src/Lecture2/BinModel02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@

#include "BinModel02.hpp"

double lecture2::BinModel::RiskNeutralProb() {
double
lecture2::BinModel::RiskNeutralProb() {
return (R_ - D_) / (U_ - D_);
}

double lecture2::BinModel::S(int n, int i) {
return S0_ * pow(1+U_,i) * pow(1+D_, n-i);
double
lecture2::BinModel::S(int n, int i) {
return S0_ * pow(1 + U_, i) * pow(1 + D_, n - i);
}

double lecture2::BinModel::GetR() {
double
lecture2::BinModel::GetR() {
return R_;
}

lecture2::BinModel::BinModel(double S0, double U, double D, double R) : S0_(S0), U_(U), D_(D), R_(R) {
if (S0_ <= 0 || U_ <= -1.0 || D_ <= -1.0 || R_ <= -1.0 || U_ <= D)
{
throw std::invalid_argument( "Illegal data range!" );
lecture2::BinModel::BinModel(double S0, double U, double D, double R)
: S0_(S0), U_(U), D_(D), R_(R) {
if (S0_ <= 0 || U_ <= -1.0 || D_ <= -1.0 || R_ <= -1.0 || U_ <= D) {
throw std::invalid_argument("Illegal data range!");
}
// check for arb
if (R_ <= D || R_ >= U)
{
throw std::invalid_argument( "Arb opp exists!" );
if (R_ <= D || R_ >= U) {
throw std::invalid_argument("Arb opp exists!");
}

std::cout << "Input data checked - no arbitrage oppertunities detected" << std::endl;

std::cout << "Input data checked - no arbitrage oppertunities detected"
<< std::endl;
};
9 changes: 4 additions & 5 deletions src/Lecture2/BinModel02.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#pragma once

#include <math.h>
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <math.h>
#include <iostream>
#include <limits>

namespace lecture2 {
Expand All @@ -21,14 +21,13 @@ class BinModel {
double RiskNeutralProb();
double S(int n, int i);
double GetR();

private:
double S0_;
double U_;
double D_;
double R_;
double T_;

};

}
} // namespace lecture2
45 changes: 30 additions & 15 deletions src/Lecture2/BlackScholes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,55 @@
// Created by Jeremy Lo on 17/10/2023.
//


#include "BlackScholes.hpp"

namespace {
// Standard normal probability density function
double norm_pdf(const double& x) {
return (1.0/(pow(2*M_PI,0.5)))*exp(-0.5*x*x);
}
double
norm_pdf(const double& x) {
return (1.0 / (pow(2 * M_PI, 0.5))) * exp(-0.5 * x * x);
}
} // namespace

// An approximation to the CDF for the standard normal
// Note: This is a recursive function
double lecture2::norm_cdf(const double& x) {
double k = 1.0/(1.0 + 0.2316419*x);
double k_sum = k*(0.319381530 + k*(-0.356563782 + k*(1.781477937 + k*(-1.821255978 + 1.330274429*k))));
double
lecture2::norm_cdf(const double& x) {
double k = 1.0 / (1.0 + 0.2316419 * x);
double k_sum =
k * (0.319381530 +
k * (-0.356563782 +
k * (1.781477937 + k * (-1.821255978 + 1.330274429 * k))));

if (x >= 0.0) {
return (1.0 - (1.0/(pow(2*M_PI,0.5)))*exp(-0.5*x*x) * k_sum);
return (1.0 - (1.0 / (pow(2 * M_PI, 0.5))) * exp(-0.5 * x * x) * k_sum);
} else {
return 1.0 - lecture2::norm_cdf(-x);
}
}

// This calculates d_j, for j in {1,2}
double lecture2::d_j(const int& j, const double& S, const double& K, const double& r, const double& v, const double& T) {
return (log(S/K) + (r + (pow(-1,j-1))*0.5*v*v)*T)/(v*(pow(T,0.5)));
double
lecture2::d_j(const int& j, const double& S, const double& K,
const double& r, const double& v, const double& T) {
return (log(S / K) + (r + (pow(-1, j - 1)) * 0.5 * v * v) * T) /
(v * (pow(T, 0.5)));
}

// Calculate the European vanilla call price
double lecture2::call_price(const double& S, const double& K, const double& r, const double& v, const double& T) {
return S * lecture2::norm_cdf(d_j(1, S, K, r, v, T))-K*exp(-r*T) * lecture2::norm_cdf(lecture2::d_j(2, S, K, r, v, T));
// Calculate the European vanilla call price
double
lecture2::call_price(const double& S, const double& K, const double& r,
const double& v, const double& T) {
return S * lecture2::norm_cdf(d_j(1, S, K, r, v, T)) -
K * exp(-r * T) *
lecture2::norm_cdf(lecture2::d_j(2, S, K, r, v, T));
}

// Calculate the European vanilla put price
double lecture2::put_price(const double& S, const double& K, const double& r, const double& v, const double& T) {
return -S*lecture2::norm_cdf(-d_j(1, S, K, r, v, T))+K*exp(-r*T) * lecture2::norm_cdf(-lecture2::d_j(2, S, K, r, v, T));
double
lecture2::put_price(const double& S, const double& K, const double& r,
const double& v, const double& T) {
return -S * lecture2::norm_cdf(-d_j(1, S, K, r, v, T)) +
K * exp(-r * T) *
lecture2::norm_cdf(-lecture2::d_j(2, S, K, r, v, T));
}
16 changes: 9 additions & 7 deletions src/Lecture2/BlackScholes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
// Created by Jeremy Lo on 17/10/2023.
//


#pragma once

#include <math.h>
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <math.h>
#include <iostream>

namespace lecture2 {

double norm_cdf(const double& x);

double d_j(const int& j, const double& S, const double& K, const double& r, const double& v, const double& T);
double d_j(const int& j, const double& S, const double& K, const double& r,
const double& v, const double& T);

double call_price(const double& S, const double& K, const double& r, const double& v, const double& T);
double call_price(const double& S, const double& K, const double& r,
const double& v, const double& T);

double put_price(const double& S, const double& K, const double& r, const double& v, const double& T);
double put_price(const double& S, const double& K, const double& r,
const double& v, const double& T);

}
} // namespace lecture2
65 changes: 36 additions & 29 deletions src/Lecture2/Options04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,70 @@
#include "Options04.hpp"
#include <vector>


void lecture2::getOptionsInputData(int& N, double& K) {
void
lecture2::getOptionsInputData(int& N, double& K) {
std::cout << "Enter steps to expiry N: ";
std::cin >> N;

std::cout << "Enter strike price K: ";
std::cin >> K;
std::cout << std::endl;
}

double lecture2::PriceByCRR(lecture2::BinModel Model, int N, double K, double (*Payoff)(double z, double K)) {
double
lecture2::PriceByCRR(lecture2::BinModel Model, int N, double K,
double (*Payoff)(double z, double K)) {
double q = Model.RiskNeutralProb();
double Price[N+1];
for (int i = 0 ; i <= N; i++) { // payoff
*(Price+i) = Payoff( Model.S(N,i), K);
double Price[N + 1];
for (int i = 0; i <= N; i++) { // payoff
*(Price + i) = Payoff(Model.S(N, i), K);
}
for ( int n = N - 1 ; n >= 0 ; n-- ) { // fill backwards by expectation
for (int i = 0; i <= n; i++) { // requires double for loop to build entire tree
*(Price+i) = ( q * (*(Price+i+1)) + (1 - q) * (*(Price+i)) ) / (1.+ Model.GetR());
for (int n = N - 1; n >= 0; n--) { // fill backwards by expectation
for (int i = 0; i <= n;
i++) { // requires double for loop to build entire tree
*(Price + i) = (q * (*(Price + i + 1)) + (1 - q) * (*(Price + i))) /
(1. + Model.GetR());
}
}
return *Price;
}
}

// Binomial coefficient
double lecture2::NewtonSymb(int N, int i) {
if (i<0 || i>N) return 0;
double
lecture2::NewtonSymb(int N, int i) {
if (i < 0 || i > N)
return 0;
double result = 1;
for (int k=1; k<=i; k++) {
result = result * (N-i+k)/k;
for (int k = 1; k <= i; k++) {
result = result * (N - i + k) / k;
}
return result;
}

double lecture2::PriceAnalytic(lecture2::BinModel Model, int N, double K, double (*Payoff)(double z, double K)) {
double
lecture2::PriceAnalytic(lecture2::BinModel Model, int N, double K,
double (*Payoff)(double z, double K)) {
double q = Model.RiskNeutralProb();
std::vector<double> PDF(N + 1);
double PDF_Sum = 0.0;
double *Price = new double[N + 1];
double *S_T = new double[N + 1];
double* Price = new double[N + 1];
double* S_T = new double[N + 1];
double Sum = 0.0;

for (int i=0; i<=N; i++)
{

for (int i = 0; i <= N; i++) {
S_T[i] = Model.S(N, i);
Price[i] = Payoff(S_T[i], K);
}
for (int i=0; i<=N; i++)
{
PDF[i] = lecture2::NewtonSymb(N, i) * pow(q, i) * pow(1 - q, N - i); // binomial distribution
for (int i = 0; i <= N; i++) {
PDF[i] = lecture2::NewtonSymb(N, i) * pow(q, i) *
pow(1 - q, N - i); // binomial distribution
PDF_Sum += PDF[i];
Sum += PDF[i] * Price[i];
}
//std::cout << " PDF_Sum = " << PDF_Sum << std::endl ;
double result = Sum / pow(1.+ Model.GetR(),N); // expectation of discouted payoff
delete [] Price;
delete [] S_T;
// std::cout << " PDF_Sum = " << PDF_Sum << std::endl ;
double result =
Sum / pow(1. + Model.GetR(), N); // expectation of discouted payoff
delete[] Price;
delete[] S_T;
return result;
}

15 changes: 8 additions & 7 deletions src/Lecture2/Options04.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

#pragma once

#include <math.h>
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <math.h>

#include <iostream>
#include "BinModel02.hpp"
#include "payoff.hpp"

Expand All @@ -22,13 +21,15 @@ void getOptionsInputData(int& N, double& K);
double NewtonSymb(int N, int i);

// pricing European option
double PriceByCRR(lecture2::BinModel Model, int N, double K, double (*Payoff)(double z, double K));
double PriceByCRR(lecture2::BinModel Model, int N, double K,
double (*Payoff)(double z, double K));

//computing payoff
// computing payoff
double CallPayoff(double z, double K);
double PutPayoff(double z, double K);

// analytical pricer
double PriceAnalytic(lecture2::BinModel Model, int N, double K, double (*Payoff)(double z, double K));
double PriceAnalytic(lecture2::BinModel Model, int N, double K,
double (*Payoff)(double z, double K));

}
} // namespace lecture2
Loading

0 comments on commit ace0d4b

Please sign in to comment.