Skip to content

Commit

Permalink
Merge pull request #47 from ccjeremylo/16-improve-barrier-option-design
Browse files Browse the repository at this point in the history
Reformatting code with clang format
  • Loading branch information
ccjeremylo authored Oct 25, 2023
2 parents 7e1472f + ace0d4b commit 5140e6b
Show file tree
Hide file tree
Showing 24 changed files with 554 additions and 366 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.

25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ The current format/set-up is largely based on [this repo](https://github.com/KYL
## Features
* Python binding of c++ code via [```pybind11```](https://github.com/pybind/pybind11) (see this [readme file](https://github.com/ccjeremylo/FinEng-in-IRFX/blob/main/src/pybind_example/README.md) for more info)
* Unit test suite supported by [```googletest```](https://github.com/google/googletest)
* CI/CD via [Github Actions](https://github.com/features/actions)
* CI via [Github Actions](https://github.com/features/actions)
* Code review of jupyter notebooks via [ReviewNB App](https://github.com/apps/review-notebook-app)
* Auto formatting (tbd)

## Requirements
* CMake
* C++11 compliant compiler
* Python3
* Python3.7 or above
* Gtest

## Install, Build & Run
Expand All @@ -56,9 +56,9 @@ The following script only works for linux/macOS users:
### Set up & using ```pybind11```:
See this separate [readme file](https://github.com/ccjeremylo/FinEng-in-IRFX/blob/main/src/pybind_example/README.md) for details <br />

The following scripts only work for linux/macOS users (and assumes dependencies have been installed) - best to refer to this [readme file](https://github.com/ccjeremylo/FinEng-in-IRFX/blob/main/src/pybind_example/README.md) for setting up. <br />
The following scripts only work for linux/macOS users (and assumes dependencies have been installed) - best to refer to this [readme file](https://github.com/ccjeremylo/FinEng-in-IRFX/blob/main/src/pybind_example/README.md) for set up. <br />

Install our c++ package ```fineng_irfx``` (python binding) into the virtual env ```.venv```:
Install our c++ python binding ```fineng_irfx``` into the virtual env ```.venv```:
```
./deploy_cpp_module.sh
```
Expand All @@ -68,9 +68,10 @@ Now, you can also launch a jupyter lab session with the required virtual env:
```

## Project Structure
* The library can be found [here](https://github.com/ccjeremylo/FinEng-in-IRFX/tree/main/src).
* Example tests can be found [here](https://github.com/ccjeremylo/FinEng-in-IRFX/tree/main/tests).
* Project to-do list are [here](https://github.com/ccjeremylo/FinEng-in-IRFX/issues).
* The C++ can be found [here](https://github.com/ccjeremylo/FinEng-in-IRFX/tree/main/src)
* The Python can be found [here](https://github.com/ccjeremylo/FinEng-in-IRFX/tree/main/src/python)
* Example tests can be found [here](https://github.com/ccjeremylo/FinEng-in-IRFX/tree/main/tests)
* Project to-do list are [here](https://github.com/ccjeremylo/FinEng-in-IRFX/issues)


### Lecture 1
Expand Down Expand Up @@ -100,15 +101,13 @@ Now, you can also launch a jupyter lab session with the required virtual env:
- using Box-muller as RNG
- pricing path-dependent payoffs (barrier/Asian)
- discretisation error in long stepping (terminal correlation)
- Monte Carlo error analysis and convergence
- risks computation & error analysis

### Lecture 4
* Monte Carlo methods continued:
- Monte Carlo error analysis and convergence
- variance reduction techniques
- pricing arithmetic asian with geometric asian as control variate
- pricing barrier with vanilla option as control variate
- error analysis continued
- pricing early excerise payoffs (American/Bermudans)
- pricing basket options
- pricing path-dependent basket options


- risks computation & error analysis
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
Loading

0 comments on commit 5140e6b

Please sign in to comment.