├── CppOptions ├── GericDerivative.cpp ├── MonteCarlo.hpp ├── blas_samp.hpp ├── boosttest.hpp ├── BlackScholesPricer.hpp ├── GericDerivative.h ├── qlibsample.hpp ├── stl_alg.hpp ├── LatticeModel.hpp ├── RungeKutta.hpp ├── Prototype.hpp ├── DataSource.hpp ├── Integration.hpp ├── BlackScholes.hpp ├── DesignPatterns.cpp ├── EulersMethod.hpp ├── RandomWalk.hpp ├── GenericOption.h ├── DesignPatterns.hpp ├── Functional.hpp ├── NewtonMethod.hpp ├── DateCompact.h ├── MathFunction.hpp ├── blas_samp.cpp ├── Matrix.h ├── DataSource.cpp ├── CDS.h ├── Prototype.cpp ├── RandomWalkGenerator.h ├── MathFunction.cpp ├── Observer.hpp ├── templates.hpp ├── LAVectors.hpp ├── EulersMethod.cpp ├── Date.h ├── OOExamples.cpp ├── RandomWalk.cpp ├── RungeKutta.cpp ├── templates.cpp ├── boosttest.cpp ├── Integration.cpp ├── OOExamples.h ├── RandomWalkGenerator.cpp ├── GenericOption.cpp ├── NewtonMethod.cpp ├── LAVectors.cpp ├── Observer.cpp ├── MonteCarlo.cpp ├── BlackScholesPricer.cpp ├── DateCompact.cpp ├── BlackScholes.cpp ├── Matrix.cpp ├── CDS.cpp ├── Functional.cpp ├── qlibsample.cpp ├── LatticeModel.cpp ├── Date.cpp └── stl_alg.cpp ├── LICENSE.txt ├── 9781484218136.jpg ├── README.md └── contributing.md /CppOptions/GericDerivative.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // GericDerivative.cpp 3 | 4 | #include "GericDerivative.h" 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/options-derivatives-programming-in-cpp/HEAD/LICENSE.txt -------------------------------------------------------------------------------- /9781484218136.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/options-derivatives-programming-in-cpp/HEAD/9781484218136.jpg -------------------------------------------------------------------------------- /CppOptions/MonteCarlo.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // MonteCarlo.hpp 3 | 4 | #ifndef MonteCarlo_hpp 5 | #define MonteCarlo_hpp 6 | 7 | 8 | 9 | 10 | #endif /* MonteCarlo_hpp */ 11 | -------------------------------------------------------------------------------- /CppOptions/blas_samp.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // blas_samp.hpp 3 | 4 | #ifndef blas_samp_hpp 5 | #define blas_samp_hpp 6 | 7 | #include 8 | 9 | #endif /* blas_samp_hpp */ 10 | -------------------------------------------------------------------------------- /CppOptions/boosttest.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // boosttest.hpp 3 | 4 | #ifndef boosttest_hpp 5 | #define boosttest_hpp 6 | 7 | #include 8 | 9 | #endif /* boosttest_hpp */ 10 | -------------------------------------------------------------------------------- /CppOptions/BlackScholesPricer.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // BlackScholesPricer.hpp 3 | 4 | #ifndef BlackScholesPricer_hpp 5 | #define BlackScholesPricer_hpp 6 | 7 | 8 | 9 | 10 | 11 | #endif /* BlackScholesPricer_hpp */ 12 | -------------------------------------------------------------------------------- /CppOptions/GericDerivative.h: -------------------------------------------------------------------------------- 1 | // 2 | // GericDerivative.h 3 | 4 | #ifndef __CppOptions__GericDerivative__ 5 | #define __CppOptions__GericDerivative__ 6 | 7 | #include 8 | 9 | #endif /* defined(__CppOptions__GericDerivative__) */ 10 | -------------------------------------------------------------------------------- /CppOptions/qlibsample.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // qlibsample.hpp 3 | 4 | #ifndef qlibsample_hpp 5 | #define qlibsample_hpp 6 | 7 | #include 8 | 9 | 10 | #include 11 | 12 | 13 | #include 14 | 15 | 16 | 17 | 18 | 19 | #endif /* qlibsample_hpp */ 20 | -------------------------------------------------------------------------------- /CppOptions/stl_alg.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // stl_alg.hpp 3 | // CppOptions 4 | // 5 | // Created by Carlos Oliveira on 1/10/16. 6 | // Copyright © 2016 Carlos Oliveira. All rights reserved. 7 | // 8 | 9 | #ifndef stl_alg_hpp 10 | #define stl_alg_hpp 11 | 12 | #include 13 | 14 | #endif /* stl_alg_hpp */ 15 | -------------------------------------------------------------------------------- /CppOptions/LatticeModel.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // LatticeModel.hpp 3 | // CppOptions 4 | // 5 | // Created by Carlos Oliveira on 3/13/16. 6 | // Copyright © 2016 Carlos Oliveira. All rights reserved. 7 | // 8 | 9 | #ifndef LatticeModel_hpp 10 | #define LatticeModel_hpp 11 | 12 | #include 13 | 14 | #endif /* LatticeModel_hpp */ 15 | -------------------------------------------------------------------------------- /CppOptions/RungeKutta.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // RungeKutta.hpp 3 | 4 | #ifndef RungeKutta_hpp 5 | #define RungeKutta_hpp 6 | 7 | #include "EulersMethod.hpp" 8 | 9 | class RungeKuttaMethod { 10 | public: 11 | RungeKuttaMethod(DEMathFunction &f); 12 | RungeKuttaMethod(const RungeKuttaMethod &p); 13 | ~RungeKuttaMethod(); 14 | RungeKuttaMethod &operator=(const RungeKuttaMethod &p); 15 | double solve(int n, double x0, double y0, double c); 16 | private: 17 | DEMathFunction &m_func; 18 | }; 19 | 20 | #endif /* RungeKutta_hpp */ 21 | -------------------------------------------------------------------------------- /CppOptions/Prototype.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Prototype.hpp 3 | 4 | #ifndef Prototype_hpp 5 | #define Prototype_hpp 6 | 7 | class Prototype { 8 | private: 9 | Prototype(); 10 | Prototype(const Prototype &p); 11 | Prototype &operator=(const Prototype &p); 12 | 13 | public: 14 | ~Prototype(); 15 | Prototype *clone() const; 16 | void setVar1(int v); 17 | static const Prototype &getPrototype(); 18 | 19 | private: 20 | int m_var1; 21 | static Prototype *s_prototype; 22 | 23 | }; 24 | 25 | 26 | 27 | #endif /* Prototype_hpp */ 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Options and Derivatives Programming in C++*](http://www.apress.com/9781484218136) by CARLOS OLIVEIRA (Apress, 2016). 4 | 5 | ![Cover image](9781484218136.jpg) 6 | 7 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 8 | 9 | ## Releases 10 | 11 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 12 | 13 | ## Contributions 14 | 15 | See the file Contributing.md for more information on how you can contribute to this repository. 16 | -------------------------------------------------------------------------------- /CppOptions/DataSource.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // DataSource.hpp 3 | 4 | #ifndef DataSource_hpp 5 | #define DataSource_hpp 6 | 7 | #include 8 | 9 | #include 10 | 11 | class DataSource { 12 | private: 13 | DataSource(const std::string &name); 14 | DataSource(const DataSource &p); 15 | DataSource &operator=(const DataSource &p); 16 | public: 17 | ~DataSource(); // must be public so clients can use delete 18 | 19 | static DataSource *createInstance(); 20 | 21 | void readData(); 22 | 23 | private: 24 | std::string m_dataName; 25 | }; 26 | 27 | 28 | 29 | 30 | #endif /* DataSource_hpp */ 31 | -------------------------------------------------------------------------------- /CppOptions/Integration.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Integration.hpp 3 | 4 | #ifndef Integration_hpp 5 | #define Integration_hpp 6 | 7 | #include "MathFunction.hpp" 8 | 9 | class SimpsonsIntegration { 10 | public: 11 | SimpsonsIntegration(MathFunction &f); 12 | SimpsonsIntegration(const SimpsonsIntegration &p); 13 | ~SimpsonsIntegration(); 14 | SimpsonsIntegration &operator=(const SimpsonsIntegration &p); 15 | 16 | double getIntegral(double a, double b); 17 | void setNumIntervals(int n); 18 | private: 19 | MathFunction &m_f; 20 | int m_numIntervals; 21 | }; 22 | 23 | 24 | #endif /* Integration_hpp */ 25 | -------------------------------------------------------------------------------- /CppOptions/BlackScholes.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // BlackScholes.hpp 3 | 4 | #ifndef BlackScholes_hpp 5 | #define BlackScholes_hpp 6 | 7 | #include 8 | 9 | class BlackScholesMethod { 10 | public: 11 | BlackScholesMethod(double expiration, double maxPrice, double strike, double intRate); 12 | BlackScholesMethod(const BlackScholesMethod &p); 13 | ~BlackScholesMethod(); 14 | BlackScholesMethod &operator=(const BlackScholesMethod &p); 15 | 16 | std::vector solve(double volatility, int nx, int timeSteps); 17 | private: 18 | double m_expiration; 19 | double m_maxPrice; 20 | double m_strike; 21 | double m_intRate; 22 | }; 23 | #endif /* BlackScholes_hpp */ 24 | -------------------------------------------------------------------------------- /CppOptions/DesignPatterns.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // DesignPatterns.cpp 3 | 4 | #include "DesignPatterns.hpp" 5 | 6 | ClearingHouse *ClearingHouse::s_clearingHouse = nullptr; 7 | 8 | 9 | ClearingHouse::ClearingHouse() 10 | { 11 | } 12 | 13 | ClearingHouse::~ClearingHouse() 14 | { 15 | 16 | } 17 | 18 | ClearingHouse &ClearingHouse::getClearingHouse() 19 | { 20 | if (!s_clearingHouse) 21 | { 22 | s_clearingHouse = new ClearingHouse(); 23 | } 24 | return *s_clearingHouse; 25 | } 26 | 27 | void ClearingHouse::clearTrade(const Trade &t) 28 | { 29 | } 30 | 31 | void useClearingHouse() 32 | { 33 | Trade trade; 34 | ClearingHouse &ch = ClearingHouse::getClearingHouse(); 35 | ch.clearTrade(trade); 36 | } -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /CppOptions/EulersMethod.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // EulersMethod.hpp 3 | 4 | #ifndef EulersMethod_hpp 5 | #define EulersMethod_hpp 6 | 7 | 8 | class DEMathFunction { 9 | public: 10 | 11 | virtual ~DEMathFunction() {} 12 | virtual double operator()(double x, double y) = 0; // version with two variables 13 | private: 14 | // this just an interface 15 | }; 16 | 17 | 18 | class EulersMethod { 19 | public: 20 | EulersMethod(DEMathFunction &f); 21 | EulersMethod(const EulersMethod &p); 22 | ~EulersMethod(); 23 | EulersMethod &operator=(const EulersMethod &p); 24 | 25 | double solve(int n, double x0, double y0, double c); 26 | private: 27 | DEMathFunction &m_f; 28 | }; 29 | 30 | 31 | #endif /* EulersMethod_hpp */ 32 | -------------------------------------------------------------------------------- /CppOptions/RandomWalk.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // RandomWalk.hpp 3 | 4 | #ifndef RandomWalk_hpp 5 | #define RandomWalk_hpp 6 | 7 | 8 | #include 9 | 10 | // Simple random walk for price simulation 11 | class RandomWalkModel { 12 | public: 13 | RandomWalkModel(int size, double start, double step); 14 | RandomWalkModel(const RandomWalkModel &p); 15 | ~RandomWalkModel(); 16 | RandomWalkModel &operator=(const RandomWalkModel &p); 17 | 18 | std::vector getWalk(); 19 | private: 20 | int random_integer(int max); 21 | 22 | int m_numSteps; // number of steps 23 | double m_stepSize; // size of each step (in percentage) 24 | double m_startPrice; // starting price 25 | }; 26 | 27 | 28 | #endif /* defined(__FinancialSamples__RandonWalk__) */ 29 | 30 | -------------------------------------------------------------------------------- /CppOptions/GenericOption.h: -------------------------------------------------------------------------------- 1 | // 2 | // GenericOption.h 3 | 4 | #ifndef __CppOptions__GenericOption__ 5 | #define __CppOptions__GenericOption__ 6 | 7 | #include 8 | 9 | enum OptionType { 10 | OptionType_Call, 11 | OptionType_Put 12 | }; 13 | 14 | 15 | class GenericOption { 16 | public: 17 | GenericOption(double strike, OptionType type, double cost); 18 | GenericOption(const GenericOption &p); 19 | ~GenericOption(); 20 | GenericOption &operator=(const GenericOption &p); 21 | 22 | double valueAtExpiration(double underlyingAtExpiration); 23 | double profitAtExpiration(double underlyingAtExpiration); 24 | private: 25 | double m_strike; 26 | OptionType m_type; 27 | double m_cost; 28 | }; 29 | 30 | #endif /* defined(__CppOptions__GenericOption__) */ 31 | -------------------------------------------------------------------------------- /CppOptions/DesignPatterns.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // DesignPatterns.hpp 3 | // CppOptions 4 | 5 | #ifndef DesignPatterns_hpp 6 | #define DesignPatterns_hpp 7 | 8 | class Trade { 9 | // .... 10 | }; 11 | 12 | class ClearingHouse { 13 | private: // these are all private because this is a singleton 14 | ClearingHouse(); 15 | // the copy constructor is not implemented 16 | ClearingHouse(const ClearingHouse &p); 17 | ~ClearingHouse(); 18 | // assignment operator is not implemented 19 | ClearingHouse &operator=(const ClearingHouse &p); 20 | 21 | public: 22 | static ClearingHouse &getClearingHouse(); 23 | 24 | void clearTrade(const Trade &); 25 | 26 | private: 27 | static ClearingHouse *s_clearingHouse; 28 | }; 29 | 30 | 31 | 32 | #endif /* DesignPatterns_hpp */ 33 | -------------------------------------------------------------------------------- /CppOptions/Functional.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Functional.hpp 3 | 4 | #ifndef Functional_hpp 5 | #define Functional_hpp 6 | 7 | class SimpleOption { 8 | public: 9 | // other definitions here 10 | int daysToExpiration() const { return m_daysToExpiration; } 11 | 12 | double getInTheMoneyProbability(int numDays, double currentUnderlyingPrice) const ; 13 | private: 14 | int m_daysToExpiration; 15 | }; 16 | 17 | 18 | class OptionComparison { 19 | public: 20 | OptionComparison(bool directionLess); 21 | OptionComparison(const OptionComparison &p); 22 | ~OptionComparison(); 23 | OptionComparison &operator=(const OptionComparison &p); 24 | 25 | bool operator()(const SimpleOption &o1, const SimpleOption &o2); 26 | private: 27 | bool m_directionLess; 28 | }; 29 | 30 | 31 | #endif /* Functional_hpp */ 32 | -------------------------------------------------------------------------------- /CppOptions/NewtonMethod.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // NewtonMethod.hpp 3 | 4 | #ifndef NewtonMethod_hpp 5 | #define NewtonMethod_hpp 6 | 7 | #include "MathFunction.hpp" 8 | 9 | // 10 | // a Newton method implementation. 11 | // 12 | class NewtonMethod { 13 | public: 14 | // Takes as parameter the function and its derivatives 15 | // 16 | NewtonMethod(MathFunction &f, MathFunction &derivative); 17 | NewtonMethod(MathFunction &f, MathFunction &derivative, double error); 18 | NewtonMethod(const NewtonMethod &p); 19 | virtual ~NewtonMethod(); 20 | NewtonMethod &operator=(const NewtonMethod &p); 21 | 22 | double getFunctionRoot(double initialValue); 23 | private: 24 | MathFunction &m_f; 25 | MathFunction &m_derivative; 26 | double m_error; 27 | }; 28 | 29 | 30 | #endif /* NewtonMethod_hpp */ 31 | -------------------------------------------------------------------------------- /CppOptions/DateCompact.h: -------------------------------------------------------------------------------- 1 | // 2 | // DateCompact.h 3 | 4 | #ifndef __CppOptions__DateCompact__ 5 | #define __CppOptions__DateCompact__ 6 | 7 | // 8 | // a compact representation for dates, using a character string 9 | // 10 | class DateCompact { 11 | public: 12 | DateCompact(int year, int month, int day); 13 | DateCompact(const DateCompact &p); 14 | ~DateCompact(); 15 | DateCompact &operator=(const DateCompact &p); 16 | 17 | void setYear(int y); 18 | void setMonth(int m); 19 | void setDay(int d); 20 | 21 | int year(); 22 | int month(); 23 | int day(); 24 | 25 | void print(); 26 | 27 | bool operator==(const DateCompact &d) const; 28 | bool operator<(const DateCompact &d) const; 29 | 30 | private: 31 | char m_date[8]; 32 | }; 33 | 34 | 35 | #endif /* defined(__CppOptions__DateCompact__) */ 36 | -------------------------------------------------------------------------------- /CppOptions/MathFunction.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // MathFunction.hpp 3 | 4 | #ifndef MathFunction_hpp 5 | #define MathFunction_hpp 6 | 7 | #include 8 | #include 9 | 10 | 11 | class MathFunction { 12 | public: 13 | 14 | virtual ~MathFunction() {} 15 | virtual double operator()(double x) = 0; 16 | private: 17 | // this just an interface 18 | }; 19 | 20 | 21 | // 22 | // Polynomial has the form c_1 x^n + c_2 x^n-1 + .... + c_n-1 x^1 + c_n 23 | class PolynomialFunction : public MathFunction { 24 | public: 25 | PolynomialFunction(const std::vector &coef); 26 | PolynomialFunction(const PolynomialFunction &p); 27 | virtual ~PolynomialFunction(); 28 | PolynomialFunction &operator=(const PolynomialFunction &p); 29 | 30 | virtual double operator()(double x) override; 31 | 32 | private: 33 | std::vector m_coeficients; 34 | }; 35 | 36 | 37 | #endif /* MathFunction_hpp */ 38 | -------------------------------------------------------------------------------- /CppOptions/blas_samp.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // blas_samp.cpp 3 | 4 | #include "blas_samp.hpp" 5 | 6 | #include "Matrix.h" 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | namespace ublas = boost::numeric::ublas; 14 | 15 | std::vector preMultiply(const std::vector &v, Matrix &m) 16 | { 17 | using namespace ublas; 18 | ublas::vector vec; 19 | std::copy(v.begin(), v.end(), vec.end()); 20 | 21 | int d1 = m.numRows(); 22 | int d2 = (int)m[0].size(); 23 | ublas::matrix M(d1, d2); 24 | 25 | for (int i = 0; i < d1; ++i) 26 | { 27 | for (int j = 0; j < d2; ++j) 28 | { 29 | M(i,j) = m[i][j]; 30 | } 31 | } 32 | 33 | vector pv = prod(vec, M); 34 | 35 | std::vector res; 36 | std::copy(pv.begin(), pv.end(), res.end()); 37 | return res; 38 | } -------------------------------------------------------------------------------- /CppOptions/Matrix.h: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix.h 3 | // 4 | 5 | #ifndef __FinancialSamples__Matrix__ 6 | #define __FinancialSamples__Matrix__ 7 | 8 | #include 9 | 10 | class Matrix { 11 | public: 12 | typedef std::vector Row; 13 | 14 | Matrix(int size); 15 | Matrix(int size1, int size2); 16 | Matrix(const Matrix &s); 17 | ~Matrix(); 18 | Matrix &operator=(const Matrix &s); 19 | 20 | void transpose(); 21 | double trace(); 22 | void add(const Matrix &s); 23 | void subtract(const Matrix &s); 24 | void multiply(const Matrix &s); 25 | void multiply(double num); 26 | 27 | Row & operator[](int pos); 28 | int numRows() const; 29 | private: 30 | std::vector m_rows; 31 | }; 32 | 33 | // free operators 34 | // 35 | Matrix operator+(const Matrix &s1, const Matrix &s2); 36 | Matrix operator-(const Matrix &s1, const Matrix &s2); 37 | Matrix operator*(const Matrix &s1, const Matrix &s2); 38 | 39 | #endif /* defined(__FinancialSamples__Matrix__) */ -------------------------------------------------------------------------------- /CppOptions/DataSource.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // DataSource.cpp 3 | 4 | #include "DataSource.hpp" 5 | 6 | 7 | DataSource::DataSource(const std::string &name) 8 | : m_dataName(name) 9 | { 10 | } 11 | 12 | DataSource::DataSource(const DataSource &p) 13 | : m_dataName(p.m_dataName) 14 | { 15 | } 16 | 17 | DataSource &DataSource::operator=(const DataSource &p) 18 | { 19 | if (this != &p) 20 | { 21 | m_dataName = p.m_dataName; 22 | } 23 | return *this; 24 | } 25 | 26 | DataSource::~DataSource() 27 | { 28 | } 29 | 30 | 31 | DataSource *DataSource::createInstance() 32 | { 33 | std::string sourceName; 34 | // complex method used here to find sourceName and other construction parameters .... 35 | DataSource *ds = new DataSource(sourceName); 36 | return ds; 37 | } 38 | 39 | void DataSource::readData() 40 | { 41 | // read data here ... 42 | } 43 | 44 | 45 | void useDataSource() 46 | { 47 | // DataSource *source = new DataSource(""); // this will not work! 48 | DataSource *source = DataSource::createInstance(); 49 | source->readData(); 50 | // do something else with data 51 | delete source; 52 | } -------------------------------------------------------------------------------- /CppOptions/CDS.h: -------------------------------------------------------------------------------- 1 | // 2 | // CDS.hpp 3 | // CppOptions 4 | 5 | #ifndef CDS_hpp 6 | #define CDS_hpp 7 | 8 | #include 9 | 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | // 17 | // CDSSolver class, incorporates the solution to Credit Default 18 | class CDSSolver : boost::noncopyable { 19 | public: 20 | 21 | // constructor 22 | CDSSolver(double val, double sigma, double divYield, 23 | double rf, double strike, double barrier, double rebate); 24 | 25 | // solve the model 26 | std::pair 27 | solve(QuantLib::Date maturity_date); 28 | 29 | // generate a grid 30 | void generateGrid(QuantLib::BarrierOption &option, 31 | QuantLib::BlackScholesMertonProcess &process, 32 | const std::vector &grid); 33 | 34 | private: 35 | 36 | double cur_val; 37 | double sigma; 38 | double divYield; 39 | double rf; 40 | double strike; 41 | double barrier; 42 | double rebate; 43 | }; 44 | 45 | 46 | #endif /* CDS_hpp */ 47 | -------------------------------------------------------------------------------- /CppOptions/Prototype.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Prototype.cpp 3 | 4 | #include "Prototype.hpp" 5 | 6 | Prototype *Prototype::s_prototype = nullptr; 7 | 8 | 9 | Prototype::Prototype() 10 | { 11 | } 12 | 13 | Prototype::Prototype(const Prototype &p) 14 | : m_var1(p.m_var1) 15 | { 16 | } 17 | 18 | Prototype::~Prototype() 19 | { 20 | 21 | } 22 | 23 | void Prototype::setVar1(int v) 24 | { 25 | m_var1 = v; 26 | } 27 | 28 | const Prototype &Prototype::getPrototype() 29 | { 30 | if (!s_prototype) 31 | { 32 | s_prototype = new Prototype(); 33 | } 34 | return *s_prototype; 35 | } 36 | 37 | Prototype &Prototype::operator=(const Prototype &p) 38 | { 39 | if (this != &p) 40 | { 41 | m_var1 = p.m_var1; 42 | } 43 | return *this; 44 | } 45 | 46 | Prototype *Prototype::clone() const 47 | { 48 | Prototype *a = new Prototype(*this); 49 | return a; 50 | } 51 | 52 | 53 | void usePrototype() 54 | { 55 | const Prototype &p = Prototype::getPrototype(); 56 | 57 | Prototype *pnew = p.clone(); 58 | pnew->setVar1(22); 59 | 60 | Prototype *pn2 = pnew->clone(); 61 | pn2->setVar1(43); 62 | 63 | delete pnew; 64 | delete pn2; 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- /CppOptions/RandomWalkGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // RandomWalkGenerator.h 3 | 4 | #ifndef __CppOptions__RandomWalkGenerator__ 5 | #define __CppOptions__RandomWalkGenerator__ 6 | 7 | // the class uses a vector to hold the elements 8 | // of the random walk, so they can be later plotted. 9 | #include 10 | 11 | // 12 | // Simple random walk generating class. This class can be 13 | // used for price simulation purposes. 14 | // 15 | class RandomWalkGenerator { 16 | public: 17 | // 18 | // class constructors 19 | RandomWalkGenerator(int size, double start, double step); 20 | RandomWalkGenerator(const RandomWalkGenerator &p); 21 | 22 | // destructor 23 | ~RandomWalkGenerator(); 24 | 25 | // assignment operator 26 | RandomWalkGenerator &operator=(const RandomWalkGenerator &p); 27 | 28 | // main method that returns a vector with values of the random walk 29 | std::vector generateWalk(); 30 | 31 | // returns a single step of the random walk 32 | double computeRandomStep(double currentPrice); 33 | 34 | private: 35 | int m_numSteps; // the number of steps 36 | double m_stepSize; // size of each step (in percentage points) 37 | double m_initialPrice; // starting price 38 | }; 39 | 40 | 41 | #endif /* defined(__CppOptions__RandomWalkGenerator__) */ 42 | -------------------------------------------------------------------------------- /CppOptions/MathFunction.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // MathFunction.cpp 3 | 4 | #include "MathFunction.hpp" 5 | 6 | #include 7 | 8 | using std::cout; 9 | using std::endl; 10 | 11 | 12 | PolynomialFunction::PolynomialFunction(const std::vector &coef) 13 | : m_coeficients(coef) 14 | { 15 | } 16 | 17 | PolynomialFunction::PolynomialFunction(const PolynomialFunction &p) 18 | : m_coeficients(p.m_coeficients) 19 | { 20 | } 21 | 22 | PolynomialFunction::~PolynomialFunction() 23 | { 24 | } 25 | 26 | PolynomialFunction &PolynomialFunction::operator=(const PolynomialFunction &p) 27 | { 28 | if (this != &p) 29 | { 30 | m_coeficients = p.m_coeficients; 31 | } 32 | return *this; 33 | } 34 | 35 | double PolynomialFunction::operator()(double x) 36 | { 37 | int n = (int)m_coeficients.size(); 38 | double y = 0; 39 | int i; 40 | for (i=0; i 8 | #include 9 | 10 | 11 | class Observer { 12 | public: 13 | Observer(); 14 | Observer(const Observer &p); 15 | ~Observer(); 16 | Observer &operator=(const Observer &p); // not implemented 17 | 18 | virtual void notify() = 0; 19 | 20 | private: 21 | 22 | }; 23 | 24 | class Trade { 25 | // .... 26 | }; 27 | 28 | class TradingLedger; 29 | 30 | class TradeObserver : public Observer { 31 | public: 32 | TradeObserver(TradingLedger *t); 33 | TradeObserver(const TradeObserver &p); 34 | ~TradeObserver(); 35 | TradeObserver &operator=(const TradeObserver &p); 36 | 37 | void notify(); 38 | void processNewTrade(); 39 | private: 40 | Trade m_trade; 41 | TradingLedger *m_ledger; 42 | }; 43 | 44 | 45 | class TradingLedger { 46 | public: 47 | TradingLedger(); 48 | TradingLedger(const TradingLedger &p); 49 | ~TradingLedger(); 50 | TradingLedger &operator=(const TradingLedger &p); 51 | 52 | void addObserver(std::shared_ptr observer); 53 | void removeObserver(std::shared_ptr observer); 54 | void triggerNotifications(); 55 | 56 | void addTrade(const Trade &t); 57 | const Trade &getLastTrade(); 58 | 59 | private: 60 | std::set> m_observers; 61 | Trade m_trade; 62 | }; 63 | 64 | 65 | 66 | 67 | #endif /* Observer_hpp */ 68 | -------------------------------------------------------------------------------- /CppOptions/templates.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // templates.hpp 3 | 4 | #ifndef templates_hpp 5 | #define templates_hpp 6 | 7 | #include 8 | #include 9 | 10 | 11 | template 12 | class Factorial { 13 | public: 14 | enum { 15 | Argument = N 16 | }; 17 | static long value(); 18 | }; 19 | 20 | template 21 | long Factorial::value() 22 | { 23 | return N * Factorial::value(); 24 | } 25 | 26 | 27 | template <> 28 | long Factorial<0>::value() 29 | { 30 | return 1; 31 | } 32 | 33 | void array_normalize(std::vector &array); 34 | 35 | void set_normalize(std::set &set); 36 | 37 | template 38 | class Normalization { 39 | public: 40 | typedef T Type; 41 | static void normalize(T &arg); 42 | }; 43 | 44 | template <> 45 | void Normalization>::normalize(std::vector &a) 46 | { 47 | array_normalize(a); 48 | } 49 | 50 | template <> 51 | void Normalization>::normalize(std::set &a) 52 | { 53 | set_normalize(a); 54 | } 55 | 56 | template 57 | void normalize(T &val) { 58 | Normalization::normalize(val); 59 | } 60 | 61 | void use_normalize() 62 | { 63 | std::set set; 64 | std::vector array; 65 | 66 | 67 | 68 | normalize(set); 69 | normalize(array); 70 | } 71 | 72 | void array_normalize(std::vector &array) 73 | { 74 | 75 | } 76 | 77 | void set_normalize(std::set &set) 78 | { 79 | 80 | } 81 | 82 | 83 | #endif /* templates_hpp */ 84 | -------------------------------------------------------------------------------- /CppOptions/LAVectors.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // LAVectors.hpp 3 | 4 | #ifndef LAVectors_hpp 5 | #define LAVectors_hpp 6 | 7 | 8 | #include 9 | 10 | typedef std::vector Vector; 11 | 12 | // scalar by vector operations 13 | 14 | Vector add(double num, const Vector &v); 15 | Vector multiply(double num, const Vector &v); 16 | 17 | void in_place_add(double num, Vector &v); 18 | void in_place_multiply(double num, Vector &v); 19 | 20 | inline Vector operator +(double num, const Vector &v) 21 | { 22 | return add(num, v); 23 | } 24 | 25 | inline Vector operator *(double num, const Vector &v) 26 | { 27 | return multiply(num, v); 28 | } 29 | 30 | inline void operator +=(double num, Vector &v) 31 | { 32 | in_place_add(num, v); 33 | } 34 | 35 | inline void operator *=(double num, Vector &v) 36 | { 37 | in_place_multiply(num, v); 38 | } 39 | 40 | // vector to vector operations 41 | 42 | Vector add(const Vector &v1, const Vector &v2); 43 | void in_place_add(Vector &v1, const Vector &v2); 44 | 45 | double product(const Vector &v1, const Vector &v2); 46 | void in_place_product(Vector &v1, const Vector &v2); 47 | 48 | 49 | inline Vector operator +(const Vector &v1, const Vector &v2) 50 | { 51 | return add(v1, v2); 52 | } 53 | 54 | inline void operator +=(Vector &v1, const Vector &v2) 55 | { 56 | in_place_add(v1, v2); 57 | } 58 | 59 | inline double operator *(const Vector &v1, const Vector &v2) 60 | { 61 | return product(v1, v2); 62 | } 63 | 64 | inline void operator *=(Vector &v1, const Vector &v2) 65 | { 66 | in_place_add(v1, v2); 67 | } 68 | 69 | double norm(const Vector &v); 70 | 71 | 72 | #include 73 | 74 | #endif /* LAVectors_hpp */ 75 | -------------------------------------------------------------------------------- /CppOptions/EulersMethod.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // EulersMethod.cpp 3 | 4 | #include "EulersMethod.hpp" 5 | 6 | 7 | #include 8 | 9 | using std::cout; 10 | using std::endl; 11 | 12 | 13 | EulersMethod::EulersMethod(DEMathFunction &f) 14 | : m_f(f) 15 | { 16 | } 17 | 18 | EulersMethod::EulersMethod(const EulersMethod &p) 19 | : m_f(p.m_f) 20 | { 21 | } 22 | 23 | EulersMethod::~EulersMethod() 24 | { 25 | } 26 | 27 | EulersMethod &EulersMethod::operator=(const EulersMethod &p) 28 | { 29 | if (this != &p) 30 | { 31 | m_f = p.m_f; 32 | } 33 | return *this; 34 | } 35 | 36 | double EulersMethod::solve(int n, double x0, double y0, double c) 37 | { 38 | // problem : y' = f(x,y) ; y(x0) = y0 39 | 40 | auto x = x0; 41 | auto y = y0; 42 | auto h = (c - x0)/n; 43 | 44 | cout << " h is " << h << endl; 45 | 46 | for (int i=0; i 8 | 9 | enum DayOfTheWeek { 10 | DayOfTheWeek_Sunday, 11 | DayOfTheWeek_Monday, 12 | DayOfTheWeek_Tuesday, 13 | DayOfTheWeek_Wednesday, 14 | DayOfTheWeek_Thursday, 15 | DayOfTheWeek_Friday, 16 | DayOfTheWeek_Saturday, 17 | DayOfTheWeek_UNKNOWN 18 | }; 19 | 20 | enum Month { 21 | Month_January = 1, 22 | Month_February, 23 | Month_March, 24 | Month_April, 25 | Month_May, 26 | Month_June, 27 | Month_July, 28 | Month_August, 29 | Month_September, 30 | Month_October, 31 | Month_November, 32 | Month_December, 33 | }; 34 | 35 | class Date { 36 | public: 37 | Date(int year, int month, int day); 38 | Date(const Date &p); 39 | ~Date(); 40 | Date &operator=(const Date &p); 41 | 42 | void setHolidays(const std::vector &days); 43 | std::string month(); 44 | std::string dayOfWeek(); 45 | 46 | void add(int numDays); 47 | void addTradingDays(int numDays); 48 | void subtract(int numDays); 49 | void subtractTradingDays(int numDays); 50 | int dateDifference(const Date &date); 51 | int tradingDateDifference(const Date &date); 52 | DayOfTheWeek dayOfTheWeek(); 53 | bool isHoliday(); 54 | bool isWeekDay(); 55 | Date nextTradingDay(); 56 | bool isLeapYear(); 57 | bool isTradingDay(); 58 | void print(); 59 | 60 | Date &operator ++(); 61 | Date &operator --(); 62 | bool operator<(const Date &d) const; 63 | bool operator==(const Date &d); 64 | private: 65 | int m_year; 66 | int m_month; 67 | int m_day; 68 | DayOfTheWeek m_weekDay; 69 | std::vector m_holidays; 70 | }; 71 | 72 | 73 | #endif /* defined(__CppOptions__Date__) */ 74 | -------------------------------------------------------------------------------- /CppOptions/OOExamples.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // OOExamples.cpp 3 | 4 | #include "OOExamples.h" 5 | 6 | 7 | double GeneralDerivative::strike() 8 | { 9 | return m_strike; 10 | } 11 | 12 | 13 | double CDSContract::kStandardPayoff = 1000.0; 14 | 15 | void CDSContract::setCounterpart(const std::string &s) 16 | { 17 | m_counterpart = s; 18 | setPayoff(kStandardPayoff); 19 | } 20 | 21 | double CDSContract::computeCurrentValue(const Date &d) 22 | { 23 | if (!counterpart().empty()) 24 | { 25 | processCreditEvent(); // make sure there is no credit event; 26 | } 27 | return 0; //calculateInternalValue(); 28 | } 29 | 30 | void LoanOnlyCDSContract::processCreditEvent() 31 | { 32 | } 33 | 34 | 35 | double LoanOnlyCDSContract::computeCurrentValue(const Date &d) 36 | { 37 | return 0; 38 | } 39 | 40 | CDSContract *createSimpeleContract() 41 | { 42 | CDSContract *contract = new LoanOnlyCDSContract(); 43 | contract->setCounterpart("IBM"); 44 | return contract; 45 | } 46 | 47 | 48 | void useContract(bool isLOContract, Date ¤tDate) 49 | { 50 | CDSContract *contract = nullptr; 51 | if (isLOContract) 52 | { 53 | contract = new LoanOnlyCDSContract(); 54 | } 55 | else 56 | { 57 | //contract = new CDSContract(); // normal CDS contract 58 | } 59 | 60 | contract->computeCurrentValue(currentDate); 61 | delete contract; 62 | } 63 | 64 | void useBasePtr(CDSContract *contract, Date ¤tDate) 65 | { 66 | contract->computeCurrentValue(currentDate); 67 | delete contract; 68 | } 69 | 70 | void callBasePtr() 71 | { 72 | Date date(1,1,2010); 73 | useBasePtr(new LoanOnlyCDSContract(), date); 74 | } 75 | 76 | int main_ooe() 77 | { 78 | Date date(1,1,2010); 79 | //useContract(true, date); 80 | useBasePtr(new LoanOnlyCDSContract(), date); 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /CppOptions/RandomWalk.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // RandomWalk.cpp 3 | 4 | #include "RandomWalk.hpp" 5 | 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using std::vector; 12 | using std::cout; 13 | using std::endl; 14 | 15 | 16 | std::default_random_engine engine; 17 | 18 | 19 | RandomWalkModel::RandomWalkModel(int size, double start, double step) 20 | : m_numSteps(size), 21 | m_stepSize(step), 22 | m_startPrice(start) 23 | { 24 | } 25 | 26 | RandomWalkModel::RandomWalkModel(const RandomWalkModel &p) 27 | : m_numSteps(p.m_numSteps), 28 | m_stepSize(p.m_stepSize), 29 | m_startPrice(p.m_startPrice) 30 | { 31 | } 32 | 33 | RandomWalkModel::~RandomWalkModel() 34 | { 35 | } 36 | 37 | RandomWalkModel &RandomWalkModel::operator=(const RandomWalkModel &p) 38 | { 39 | if (this != &p) 40 | { 41 | m_numSteps = p.m_numSteps; 42 | m_stepSize = p.m_stepSize; 43 | m_startPrice = p.m_startPrice; 44 | } 45 | return *this; 46 | } 47 | 48 | int RandomWalkModel::random_integer(int max) 49 | { 50 | 51 | std::uniform_int_distribution unif(0, max); 52 | return unif(engine); 53 | } 54 | 55 | std::vector RandomWalkModel::getWalk() 56 | { 57 | vector walk; 58 | double prev = m_startPrice; 59 | 60 | for (int i=0; i walk = rw.getWalk(); 77 | for (int i=0; i 8 | 9 | using std::cout; 10 | using std::endl; 11 | 12 | RungeKuttaMethod::RungeKuttaMethod(DEMathFunction &f) 13 | : m_func(f) 14 | { 15 | } 16 | 17 | RungeKuttaMethod::RungeKuttaMethod(const RungeKuttaMethod &p) 18 | : m_func(p.m_func) 19 | { 20 | } 21 | 22 | RungeKuttaMethod::~RungeKuttaMethod() 23 | { 24 | } 25 | 26 | RungeKuttaMethod &RungeKuttaMethod::operator=(const RungeKuttaMethod &p) 27 | { 28 | if (this != &p) 29 | { 30 | m_func = p.m_func; 31 | } 32 | return *this; 33 | } 34 | 35 | // Runge-Kutta method with fourth order approximation 36 | // 37 | double RungeKuttaMethod::solve(int n, double x0, double y0, double c) 38 | { 39 | // initial conditions 40 | auto x = x0; 41 | auto y = y0; 42 | auto h = (c - x0)/n; 43 | 44 | for (int i=0; i 7 | #include 8 | 9 | template 10 | T generic_max(T a, T b) 11 | { 12 | if (a > b) 13 | { 14 | return a; 15 | } 16 | else 17 | { 18 | return b; 19 | } 20 | } 21 | 22 | template <> 23 | const char * generic_max(const char *a, const char *b) 24 | { 25 | if (strcmp(a, b) > 0) 26 | { 27 | return a; 28 | } 29 | else 30 | { 31 | return b; 32 | } 33 | } 34 | 35 | 36 | template 37 | void printNumberPlusOne() 38 | { 39 | int a = N + 1; 40 | std::cout << a << std::endl; 41 | } 42 | 43 | void usePrintTemplate() 44 | { 45 | printNumberPlusOne<10>(); 46 | } 47 | 48 | template 49 | void printNumberRecursive() // general case 50 | { 51 | std::cout << N << " "; 52 | printNumberRecursive(); 53 | } 54 | 55 | template<> 56 | void printNumberRecursive<0>() // base case 57 | { 58 | std::cout << std::endl; 59 | } 60 | 61 | void usePrintRecursive() 62 | { 63 | printNumberRecursive<10>(); 64 | } 65 | 66 | template 67 | int intSum() 68 | { 69 | return N + intSum(); 70 | } 71 | 72 | template <> 73 | int intSum<0>() 74 | { 75 | return 0; 76 | } 77 | 78 | void useIntSum() 79 | { 80 | std::cout << intSum<20>() << std::endl; 81 | } 82 | 83 | void useFactorial() 84 | { 85 | Factorial<8> fact; 86 | std::cout << " factorial for argument " << fact.Argument << " is " << fact.value() << std::endl; 87 | } 88 | 89 | int int_max(int a, int b) 90 | { 91 | if (a > b) 92 | { 93 | return a; 94 | } 95 | else 96 | { 97 | return b; 98 | } 99 | } 100 | 101 | void use_template() 102 | { 103 | int res = 0; 104 | int a = 15, b = 32; 105 | 106 | res = generic_max(a, b); 107 | 108 | 109 | generic_max("test1", "test2"); 110 | } 111 | 112 | 113 | 114 | 115 | int main_templat() 116 | { 117 | usePrintRecursive(); 118 | useIntSum(); 119 | useFactorial(); 120 | return 0; 121 | } -------------------------------------------------------------------------------- /CppOptions/boosttest.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // boosttest.cpp 3 | 4 | 5 | 6 | #include "boosttest.hpp" 7 | 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | using namespace std; 15 | using namespace boost::numeric::odeint; 16 | 17 | const double sigma = 10.0; 18 | const double R = 28.0; 19 | const double b = 8.0 / 3.0; 20 | 21 | typedef boost::array< double , 3 > state_type; 22 | 23 | void lorenz( const state_type &x , state_type &dxdt , double t ) 24 | { 25 | dxdt[0] = sigma * ( x[1] - x[0] ); 26 | dxdt[1] = R * x[0] - x[1] - x[0] * x[2]; 27 | dxdt[2] = -b * x[2] + x[0] * x[1]; 28 | } 29 | 30 | void write_lorenz( const state_type &x , const double t ) 31 | { 32 | cout << t << '\t' << x[0] << '\t' << x[1] << '\t' << x[2] << endl; 33 | } 34 | 35 | void main2(int argc, char **argv) 36 | { 37 | state_type x = { 10.0 , 1.0 , 1.0 }; // initial conditions 38 | integrate(lorenz , x , 0.0 , 25.0 , 0.1 , write_lorenz ); 39 | } 40 | 41 | 42 | 43 | // 44 | // This is the equation at the right side of the ODE y' = f(x,y) 45 | // It is evaluated in the inner steps of the algorithm. 46 | // 47 | void right_side_equation(double y, double &dydx, double x) 48 | { 49 | dydx = 3.0/(2.5*x*x) + y/(1.5*x); 50 | } 51 | 52 | // this function simply prints the current value of the interactive 53 | // solution steps. 54 | void write_cout( const double &x , const double t ) 55 | { 56 | cout << t << '\t' << x << endl; 57 | } 58 | 59 | // A stepper based on Runge-Kutta algorithm. 60 | // the state_type use is 'double' 61 | typedef runge_kutta_dopri5 stepper_type; 62 | 63 | 64 | // This solves the ODE described above with initial condition x(1) = 0. 65 | 66 | 67 | int main_boost() 68 | { 69 | double x = 0.0; 70 | auto n = integrate_adaptive( 71 | make_controlled(1E-12, 1E-12, stepper_type()), // instantiate the stepper 72 | right_side_equation, // equation 73 | x, // initial state 74 | 1.0 , 10.0 , 0.1 , // start x, end x, and step size 75 | write_cout ); 76 | cout << " process completed after " << n << " steps \n"; 77 | return 0; 78 | } -------------------------------------------------------------------------------- /CppOptions/Integration.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Integration.cpp 3 | 4 | #include "Integration.hpp" 5 | 6 | 7 | #include "MathFunction.hpp" 8 | 9 | #include 10 | #include 11 | 12 | using std::cout; 13 | using std::endl; 14 | 15 | namespace { 16 | const int DEFAULT_NUM_INTERVALS = 100; 17 | } 18 | 19 | SimpsonsIntegration::SimpsonsIntegration(MathFunction &f) 20 | : m_f(f), 21 | m_numIntervals(DEFAULT_NUM_INTERVALS) 22 | { 23 | } 24 | 25 | SimpsonsIntegration::SimpsonsIntegration(const SimpsonsIntegration &p) 26 | : m_f(p.m_f), 27 | m_numIntervals(p.m_numIntervals) 28 | { 29 | } 30 | 31 | SimpsonsIntegration::~SimpsonsIntegration() 32 | { 33 | } 34 | 35 | SimpsonsIntegration &SimpsonsIntegration::operator=(const SimpsonsIntegration &p) 36 | { 37 | if (this != &p) 38 | { 39 | m_f = p.m_f; 40 | m_numIntervals = p.m_numIntervals; 41 | } 42 | return *this; 43 | } 44 | 45 | double SimpsonsIntegration::getIntegral(double a, double b) 46 | { 47 | double S = 0; 48 | double intSize = (b - a)/m_numIntervals; 49 | double x = a; 50 | 51 | for (int i=0; i 8 | #include 9 | #include "Date.h" 10 | 11 | class GeneralDerivative { 12 | 13 | virtual double strike(); 14 | private: 15 | double m_strike; 16 | }; 17 | 18 | 19 | /// abstract classes 20 | 21 | class AbstractDerivative { 22 | public: 23 | virtual double calculatePriceAtExpiration() = 0; 24 | virtual void setExpiration(int year, int month, int day); 25 | virtual double getcurrentPrice() = 0; 26 | }; 27 | 28 | 29 | class PartialDerivative { 30 | public: 31 | PartialDerivative(); 32 | virtual ~PartialDerivative(); 33 | PartialDerivative(const PartialDerivative &p); 34 | PartialDerivative &operator=(const PartialDerivative &p); 35 | 36 | private: 37 | 38 | }; 39 | 40 | enum CDSUnderlying { 41 | CDSUnderlying_Bond, 42 | CDSUnderlying_Cash 43 | }; 44 | 45 | class Date; 46 | class MathIntegration; 47 | 48 | class CDSContract { 49 | public: 50 | CDSContract() {} 51 | CDSContract(MathIntegration *mipt); 52 | CDSContract(const CDSContract &p); 53 | virtual ~CDSContract() { std::cout << " base delete " << std::endl; } 54 | CDSContract &operator=(const CDSContract &p); 55 | 56 | std::string counterpart() { return m_counterpart; } 57 | void setCounterpart(const std::string &s); 58 | double payoff() { return m_payoff; } 59 | void setPayoff(double payoff) { m_payoff = payoff; } 60 | virtual double computeCurrentValue(const Date &d); 61 | 62 | virtual void processCreditEvent() = 0; 63 | 64 | private: 65 | std::string m_counterpart; 66 | CDSUnderlying m_underlying; 67 | double m_payoff; 68 | int m_term; 69 | double m_spreadCost; 70 | MathIntegration *m_mipt; 71 | 72 | static double kStandardPayoff; 73 | }; 74 | 75 | 76 | class LoanOnlyCDSContract : public CDSContract { 77 | public: 78 | LoanOnlyCDSContract() { std::cout << " derived class delete " << std::endl; } 79 | // constructors go here 80 | void changeLoanSource(const std::string &s); 81 | virtual double computeCurrentValue(const Date &d); 82 | 83 | virtual void processCreditEvent(); 84 | 85 | private: 86 | std::string m_loanSource; 87 | }; 88 | 89 | 90 | 91 | 92 | 93 | #endif /* OOExamples_hpp */ 94 | -------------------------------------------------------------------------------- /CppOptions/RandomWalkGenerator.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // RandomWalkGenerator.cpp 3 | // 4 | // Simple random walk implementation. 5 | 6 | #include "RandomWalkGenerator.h" 7 | 8 | #include 9 | #include 10 | 11 | using std::vector; 12 | using std::cout; 13 | using std::endl; 14 | 15 | // 16 | // Constructor. The supplied parameters represent the number of elements in the 17 | // random walk, the initial price, and the step size for the random walk. 18 | // 19 | RandomWalkGenerator::RandomWalkGenerator(int size, double start, double step) 20 | : m_numSteps(size), 21 | m_stepSize(step), 22 | m_initialPrice(start) 23 | { 24 | } 25 | 26 | RandomWalkGenerator::RandomWalkGenerator(const RandomWalkGenerator &p) 27 | : m_numSteps(p.m_numSteps), 28 | m_stepSize(p.m_stepSize), 29 | m_initialPrice(p.m_initialPrice) 30 | { 31 | } 32 | 33 | RandomWalkGenerator::~RandomWalkGenerator() 34 | { 35 | } 36 | 37 | RandomWalkGenerator &RandomWalkGenerator::operator=(const RandomWalkGenerator &p) 38 | { 39 | if (this != &p) 40 | { 41 | m_numSteps = p.m_numSteps; 42 | m_stepSize = p.m_stepSize; 43 | m_initialPrice = p.m_initialPrice; 44 | } 45 | return *this; 46 | } 47 | 48 | // 49 | // returns a single step of the random walk 50 | // 51 | double RandomWalkGenerator::computeRandomStep(double currentPrice) 52 | { 53 | int r = rand() % 3; 54 | double val = currentPrice; 55 | if (r == 0) 56 | { 57 | val += (m_stepSize * val); 58 | } 59 | else if (r == 1) 60 | { 61 | val -= (m_stepSize * val); 62 | } 63 | return val; 64 | } 65 | 66 | 67 | // 68 | // This is the main method. It will generate random numbers within 69 | // the constraints set in the constructor. 70 | // 71 | std::vector RandomWalkGenerator::generateWalk() 72 | { 73 | vector walk; 74 | double prev = m_initialPrice; 75 | 76 | for (int i=0; i walk = rw.generateWalk(); 92 | for (int i=0; i 7 | 8 | using std::cout; 9 | using std::endl; 10 | 11 | GenericOption::GenericOption(double strike, OptionType type, double cost) 12 | : m_strike(strike), 13 | m_type(type), 14 | m_cost(cost) 15 | { 16 | 17 | } 18 | 19 | GenericOption::GenericOption(const GenericOption &p) 20 | : m_strike(p.m_strike), 21 | m_type(p.m_type), 22 | m_cost(p.m_cost) 23 | { 24 | 25 | } 26 | 27 | GenericOption::~GenericOption() 28 | { 29 | } 30 | 31 | GenericOption &GenericOption::operator=(const GenericOption &p) 32 | { 33 | if (this != &p) 34 | { 35 | m_type = p.m_type; 36 | m_strike = p.m_strike; 37 | m_cost = p.m_cost; 38 | } 39 | return *this; 40 | } 41 | 42 | // 43 | // Computes the value of the option at expiration date. 44 | // Value depends on the type of option (CALL or PUT) and strike. 45 | // 46 | double GenericOption::valueAtExpiration(double underlyingAtExpiration) 47 | { 48 | double value = 0.0; 49 | 50 | if (m_type == OptionType_Call) 51 | { 52 | if (m_strike < underlyingAtExpiration) 53 | { 54 | value = underlyingAtExpiration - m_strike; 55 | } 56 | } 57 | else // it is an OptionType_Put 58 | { 59 | if (m_strike > underlyingAtExpiration) 60 | { 61 | value = m_strike - underlyingAtExpiration; 62 | } 63 | } 64 | return value; 65 | } 66 | 67 | // 68 | // return the profit (value at expiration minus option cost) 69 | // 70 | double GenericOption::profitAtExpiration(double underlyingAtExpiration) 71 | { 72 | 73 | double finalValue = valueAtExpiration(underlyingAtExpiration); 74 | 75 | return finalValue - m_cost; 76 | } 77 | 78 | int main_go() 79 | { 80 | GenericOption option(100.0, OptionType_Put, 1.1); 81 | double price1 = 120.0; 82 | double value = option.valueAtExpiration(price1); 83 | cout << " For 100PUT, value at expiration for price " << price1 << " is " << value << endl; 84 | double price2 = 85.0; 85 | value = option.valueAtExpiration(85.0); 86 | cout << " For 100PUT, value at expiration for price " << price2 << " is " << value << endl; 87 | 88 | auto limit = 120.0; 89 | for (auto price = 80.0; price <= limit; price += 0.1) 90 | { 91 | value = option.profitAtExpiration(price); 92 | cout << price << ", " << value << endl; 93 | } 94 | 95 | 96 | 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /CppOptions/NewtonMethod.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // NewtonMethod.cpp 3 | 4 | #include "NewtonMethod.hpp" 5 | 6 | 7 | #include 8 | #include 9 | 10 | using std::endl; 11 | using std::cout; 12 | 13 | namespace { 14 | const double DEFAULT_ERROR = 0.0001; 15 | } 16 | 17 | NewtonMethod::NewtonMethod(MathFunction &f, MathFunction &derivative) 18 | : m_f(f), 19 | m_derivative(derivative), 20 | m_error(DEFAULT_ERROR) 21 | { 22 | } 23 | 24 | NewtonMethod::NewtonMethod(MathFunction &f, MathFunction &derivative, double error) 25 | : m_f(f), 26 | m_derivative(derivative), 27 | m_error(error) 28 | { 29 | } 30 | 31 | NewtonMethod::NewtonMethod(const NewtonMethod &p) 32 | : m_f(p.m_f), 33 | m_derivative(p.m_derivative), 34 | m_error(p.m_error) 35 | { 36 | } 37 | 38 | NewtonMethod::~NewtonMethod() 39 | { 40 | } 41 | 42 | NewtonMethod &NewtonMethod::operator=(const NewtonMethod &p) 43 | { 44 | if (this != &p) 45 | { 46 | m_f = p.m_f; 47 | m_derivative = p.m_derivative; 48 | m_error = p.m_error; 49 | } 50 | return *this; 51 | } 52 | 53 | double NewtonMethod::getFunctionRoot(double x0) 54 | { 55 | double x1 = x0; 56 | do 57 | { 58 | x0 = x1; 59 | cout << " x0 is " << x0 << endl; // this line just for demonstration 60 | double d = m_derivative(x0); 61 | double y = m_f(x0); 62 | x1 = x0 - y / d; 63 | } 64 | while (std::abs(x0 - x1) > m_error); 65 | return x1; 66 | } 67 | 68 | // ---- A function used as example 69 | 70 | namespace { 71 | 72 | class SampleFunction : public MathFunction { 73 | public: 74 | virtual ~SampleFunction(); 75 | virtual double operator()(double value); 76 | }; 77 | 78 | SampleFunction::~SampleFunction() 79 | { 80 | } 81 | 82 | double SampleFunction::operator ()(double x) 83 | { 84 | return (x-1)*(x-1)*(x-1); 85 | } 86 | 87 | class Derivative : public MathFunction { 88 | public: 89 | virtual ~Derivative(); 90 | virtual double operator()(double value); 91 | }; 92 | 93 | // represents the derivative of the sample function 94 | Derivative::~Derivative() 95 | { 96 | } 97 | 98 | double Derivative::operator ()(double x) 99 | { 100 | return 3*(x-1)*(x-1); 101 | } 102 | 103 | } 104 | 105 | int main_newton() 106 | { 107 | SampleFunction f; 108 | Derivative df; 109 | NewtonMethod nm(f, df); 110 | cout << " the root of the function is " << nm.getFunctionRoot(100) << endl; 111 | return 0; 112 | } 113 | 114 | -------------------------------------------------------------------------------- /CppOptions/LAVectors.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // LAVectors.cpp 3 | 4 | #include "LAVectors.hpp" 5 | 6 | #include 7 | 8 | // 9 | // adds a scalar number to a vector "v" 10 | // 11 | Vector add(double num, const Vector &v) 12 | { 13 | int n = (int)v.size(); 14 | Vector result(n); 15 | for (int i=0; i PObserver; 9 | typedef shared_ptr PTradeObserver; 10 | 11 | Observer::Observer() 12 | { 13 | } 14 | 15 | Observer::Observer(const Observer &p) 16 | { 17 | } 18 | 19 | Observer::~Observer() 20 | { 21 | } 22 | 23 | void Observer::notify() 24 | { 25 | } 26 | 27 | TradeObserver::TradeObserver(TradingLedger *t) 28 | : m_ledger(t) 29 | { 30 | } 31 | 32 | TradeObserver::TradeObserver(const TradeObserver &p) 33 | : m_trade(p.m_trade), 34 | m_ledger(p.m_ledger) 35 | { 36 | } 37 | 38 | TradeObserver::~TradeObserver() 39 | { 40 | } 41 | 42 | TradeObserver &TradeObserver::operator=(const TradeObserver &p) 43 | { 44 | if (this != &p) 45 | { 46 | m_trade = p.m_trade; 47 | m_ledger = p.m_ledger; 48 | } 49 | return *this; 50 | } 51 | 52 | void TradeObserver::notify() 53 | { 54 | this->processNewTrade(); 55 | } 56 | 57 | void TradeObserver::processNewTrade() 58 | { 59 | m_trade = m_ledger->getLastTrade(); 60 | // do trading processing here 61 | } 62 | 63 | 64 | // 65 | 66 | TradingLedger::TradingLedger() 67 | { 68 | } 69 | 70 | TradingLedger::TradingLedger(const TradingLedger &p) 71 | : m_observers(p.m_observers), 72 | m_trade(p.m_trade) 73 | { 74 | } 75 | 76 | TradingLedger::~TradingLedger() 77 | { 78 | } 79 | 80 | TradingLedger &TradingLedger::operator=(const TradingLedger &p) 81 | { 82 | if (this != &p) 83 | { 84 | m_observers = p.m_observers; 85 | m_trade = p.m_trade; 86 | } 87 | return *this; 88 | } 89 | 90 | void TradingLedger::addObserver(PObserver observer) 91 | { 92 | m_observers.insert(observer); 93 | } 94 | 95 | void TradingLedger::removeObserver(PObserver observer) 96 | { 97 | if (m_observers.find(observer) != m_observers.end()) 98 | { 99 | m_observers.erase(observer); 100 | } 101 | } 102 | 103 | void TradingLedger::triggerNotifications() 104 | { 105 | for (auto i : m_observers) 106 | { 107 | i->notify(); 108 | } 109 | } 110 | 111 | void TradingLedger::addTrade(const Trade &t) 112 | { 113 | m_trade = t; 114 | this->triggerNotifications(); 115 | } 116 | 117 | const Trade &TradingLedger::getLastTrade() 118 | { 119 | return m_trade; 120 | } 121 | 122 | int main_obs() 123 | { 124 | TradingLedger tl; 125 | PTradeObserver observer1 = PTradeObserver(new TradeObserver(&tl)); 126 | PTradeObserver observer2 = PTradeObserver(new TradeObserver(&tl)); 127 | tl.addObserver(observer1); 128 | tl.addObserver(observer2); 129 | 130 | // perform trading system here 131 | 132 | Trade aTrade; 133 | tl.addTrade(aTrade); 134 | 135 | // observers should receive a notification at this point 136 | return 0; 137 | } 138 | -------------------------------------------------------------------------------- /CppOptions/MonteCarlo.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // MonteCarlo.cpp 3 | 4 | #include "MonteCarlo.hpp" 5 | 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using std::cout; 13 | using std::endl; 14 | using std::vector; 15 | 16 | std::default_random_engine generator; 17 | 18 | 19 | int get_uniform_int(int max) 20 | { 21 | if (max < 1) 22 | { 23 | cout << "invalid parameter max " << max << endl; 24 | throw std::runtime_error("invalid parameter max"); 25 | } 26 | std::uniform_int_distribution uint(0,max); 27 | 28 | return uint(generator); 29 | } 30 | 31 | 32 | vector coin_toss_experiment(int num_experiments) 33 | { 34 | if (num_experiments < 1) 35 | { 36 | cout << "invalid number of experiments " << num_experiments << endl; 37 | throw std::runtime_error("invalid number of experiments"); 38 | } 39 | 40 | std::bernoulli_distribution bernoulli(0.5); 41 | 42 | vector results; 43 | for (int i=0; i num_customers_experiment(double mean, int max, int ntries) 51 | { 52 | std::default_random_engine generator; 53 | 54 | vector occurrences(max, 0); 55 | std::poisson_distribution poisson(mean); 56 | 57 | for (int i=0; i get_normal_observations(int n, double mean, double stdev) 70 | { 71 | std::default_random_engine generator; 72 | 73 | 74 | vector values; 75 | std::normal_distribution normaldist(mean, stdev); 76 | 77 | for (int i=0; i nv = get_normal_observations(1000, 8, 2); 97 | 98 | auto res = std::minmax_element(nv.begin(), nv.end()); 99 | double min = *(res.first); 100 | double max = *(res.second); 101 | 102 | int N = 100; 103 | double h = (max - min)/double(N); 104 | vector values(N, 0); 105 | 106 | for (int i=0; i 8 | 9 | #include 10 | 11 | 12 | // 13 | // The BlackScholesPricer class provides an interface to the ODEINT 14 | // pricer component 15 | // 16 | class BlackScholesPricer { 17 | public: 18 | BlackScholesPricer(bool call, double price, double strike, double tau, double r, double fr, double vol); 19 | BlackScholesPricer(const BlackScholesPricer &p); 20 | ~BlackScholesPricer(); 21 | BlackScholesPricer &operator=(const BlackScholesPricer &p); 22 | 23 | double value(); 24 | 25 | double delta(); 26 | double gamma(); 27 | double theta(); 28 | double vega(); 29 | private: 30 | double m_price; 31 | double m_strike; 32 | double m_tau; 33 | double m_rate; 34 | double m_frate; 35 | double m_vol; 36 | double m_isCall; 37 | 38 | boost::shared_ptr m_calc; 39 | }; 40 | 41 | 42 | BlackScholesPricer::BlackScholesPricer(bool call, double price, double strike, double tau, double r, double fr, double vol) 43 | :m_price(price), 44 | m_strike(strike), 45 | m_tau(tau), 46 | m_rate(r), 47 | m_frate(fr), 48 | m_vol(vol), 49 | m_isCall(call) 50 | { 51 | boost::shared_ptr 52 | m_option (new QuantLib::PlainVanillaPayoff(call ? QuantLib::Option::Call : QuantLib::Option::Put, strike)); 53 | 54 | // compute discount rates 55 | double cur_disc = std::exp(-m_rate * m_tau); // current discount rate 56 | double for_disc = std::exp(-m_frate * m_tau); // foward discount rate 57 | double stdev = m_vol * std::sqrt(m_tau); // standard deviation 58 | 59 | m_calc.reset(new QuantLib::BlackScholesCalculator(m_option, m_price, for_disc, stdev, cur_disc)); 60 | } 61 | 62 | BlackScholesPricer::BlackScholesPricer(const BlackScholesPricer &p) 63 | :m_price(p.m_price), 64 | m_strike(p.m_strike), 65 | m_tau(p.m_tau), 66 | m_rate(p.m_rate), 67 | m_frate(p.m_frate), 68 | m_vol(p.m_vol), 69 | m_isCall(p.m_isCall), 70 | m_calc(p.m_calc) 71 | {} 72 | 73 | BlackScholesPricer::~BlackScholesPricer() {} 74 | 75 | BlackScholesPricer &BlackScholesPricer::operator=(const BlackScholesPricer &p) 76 | { 77 | if (this != &p) 78 | { 79 | m_price = p.m_price; 80 | m_strike = p.m_strike; 81 | m_tau = p.m_tau; 82 | m_rate = p.m_rate; 83 | m_frate = p.m_frate; 84 | m_vol = p.m_vol; 85 | m_isCall = p.m_isCall; 86 | m_calc = p.m_calc; 87 | } 88 | return *this; 89 | } 90 | 91 | double BlackScholesPricer::value() 92 | { 93 | return m_calc->value(); 94 | } 95 | 96 | double BlackScholesPricer::delta() 97 | { 98 | return m_calc->delta(); 99 | } 100 | 101 | double BlackScholesPricer::gamma() 102 | { 103 | return m_calc->gamma(); 104 | } 105 | 106 | double BlackScholesPricer::theta() 107 | { 108 | return m_calc->theta(m_tau); 109 | } 110 | 111 | double BlackScholesPricer::vega() 112 | { 113 | return m_calc->vega(m_tau); 114 | } 115 | 116 | 117 | 118 | #endif -------------------------------------------------------------------------------- /CppOptions/DateCompact.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // DateCompact.cpp 3 | // 4 | // Implementation for the DateCompact class 5 | 6 | #include "DateCompact.h" 7 | 8 | #include 9 | #include 10 | 11 | using std::cout; 12 | using std::endl; 13 | 14 | DateCompact::DateCompact(int year, int month, int day) 15 | { 16 | setYear(year); 17 | setMonth(month); 18 | setDay(day); 19 | } 20 | 21 | DateCompact::DateCompact(const DateCompact &p) 22 | { 23 | strcpy(m_date, p.m_date); 24 | } 25 | 26 | DateCompact::~DateCompact() 27 | { 28 | } 29 | 30 | DateCompact &DateCompact::operator=(const DateCompact &p) 31 | { 32 | if (&p != this) 33 | { 34 | strcpy(m_date, p.m_date); 35 | } 36 | return *this; 37 | } 38 | 39 | // 40 | // Use string comparison to determine if the dates are equal 41 | // 42 | bool DateCompact::operator==(const DateCompact &d) const 43 | { 44 | return strncmp(m_date, d.m_date, 8) == 0; 45 | } 46 | 47 | // Use the strncmp function to determine if a date is less than the other. 48 | bool DateCompact::operator<(const DateCompact &d) const 49 | { 50 | // strcmp returns negative values if the first argument is less than the second. 51 | return strncmp(m_date, d.m_date, 8) < 0; 52 | } 53 | 54 | // 55 | // Functions to calculate the year, month, and days as an integers, 56 | // based on the characters contained in the string 'm_date'. 57 | // 58 | 59 | int DateCompact::year() 60 | { 61 | // (x - '0') computes the numeric value corresponding to the each character. 62 | return 1000 * (m_date[0] - '0') + 100 * (m_date[1] - '0') + 10 * (m_date[2] - '0') + (m_date[3] - '0'); 63 | } 64 | 65 | int DateCompact::month() 66 | { 67 | return 10 * (m_date[4] - '0') + (m_date[5] - '0'); 68 | } 69 | 70 | int DateCompact::day() 71 | { 72 | return 10 * (m_date[6] - '0') + (m_date[7] - '0'); 73 | } 74 | 75 | void DateCompact::print() 76 | { 77 | // copy the m_date string into a NULL terminated string (with 9 characters). 78 | char s[9]; 79 | strncpy(s, m_date, 8); 80 | s[8] = '\0'; // properly terminate the string 81 | cout << s << endl; 82 | } 83 | 84 | // 85 | // calculate the string corresponding to the given numeric parameter. 86 | // 87 | 88 | void DateCompact::setYear(int year) 89 | { 90 | m_date[3] = '0' + (year % 10); 91 | year /= 10; 92 | m_date[2] = '0' + (year % 10); 93 | year /= 10; 94 | m_date[1] = '0' + (year % 10); 95 | year /= 10; 96 | m_date[0] = '0' + (year % 10); 97 | } 98 | 99 | void DateCompact::setMonth(int month) 100 | { 101 | m_date[5] = '0' + (month % 10); month /= 10; 102 | m_date[4] = '0' + (month % 10); 103 | 104 | } 105 | 106 | void DateCompact::setDay(int day) 107 | { 108 | m_date[7] = '0' + (day % 10); day /= 10; 109 | m_date[6] = '0' + (day % 10); 110 | } 111 | 112 | 113 | #include "Date.h" 114 | 115 | 116 | int main_dtc() 117 | //int main() 118 | { 119 | DateCompact d(2008, 3, 17); 120 | DateCompact e(2008, 5, 11); 121 | cout << " size of DateCompact: " << sizeof(DateCompact) << endl; 122 | 123 | d.print(); 124 | e.print(); 125 | 126 | if (d < e) 127 | { 128 | cout << " d is less than e " << endl; 129 | } 130 | else 131 | { 132 | cout << " d is not less than e " << endl; 133 | } 134 | 135 | Date date(2008, 3, 17); 136 | cout << " size of Date: " << sizeof(Date) << endl; 137 | 138 | return 0; 139 | } 140 | 141 | -------------------------------------------------------------------------------- /CppOptions/BlackScholes.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // BlackScholes.cpp 3 | 4 | 5 | #include "BlackScholes.hpp" 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using std::vector; 15 | using std::cout; 16 | using std::endl; 17 | using std::setw; 18 | 19 | BlackScholesMethod::BlackScholesMethod(double expiration, double maxPrice, 20 | double strike, double intRate) 21 | : m_expiration(expiration), 22 | m_maxPrice(maxPrice), 23 | m_strike(strike), 24 | m_intRate(intRate) 25 | { 26 | } 27 | 28 | BlackScholesMethod::BlackScholesMethod(const BlackScholesMethod &p) 29 | : m_expiration(p.m_expiration), 30 | m_maxPrice(p.m_maxPrice), 31 | m_strike(p.m_strike), 32 | m_intRate(p.m_intRate) 33 | { 34 | } 35 | 36 | BlackScholesMethod::~BlackScholesMethod() 37 | { 38 | } 39 | 40 | BlackScholesMethod &BlackScholesMethod::operator=(const BlackScholesMethod &p) 41 | { 42 | if (this != &p) 43 | { 44 | m_expiration = p.m_expiration; 45 | m_maxPrice = p.m_maxPrice; 46 | m_strike = p.m_strike; 47 | m_intRate = p.m_intRate; 48 | } 49 | return *this; 50 | } 51 | 52 | vector BlackScholesMethod::solve(double volatility, int nx, int timeSteps) 53 | { 54 | double dt = m_expiration /(double)timeSteps; 55 | double dx = m_maxPrice /(double)nx; 56 | 57 | vector a(nx-1); 58 | vector b(nx-1); 59 | vector c(nx-1); 60 | 61 | int i; 62 | for (i = 0; i < nx - 1; i++) 63 | { 64 | b[i] = 1.0 - m_intRate * dt - dt * pow(volatility * (i+1), 2); 65 | } 66 | 67 | for (i = 0; i < nx - 2; i++) 68 | { 69 | c[i] = 0.5 * dt * pow(volatility * (i+1), 2) + 0.5 * dt * m_intRate * (i+1); 70 | } 71 | 72 | for (i = 1; i < nx - 1; i++) 73 | { 74 | a[i] = 0.5 * dt * pow(volatility * (i+1), 2) - 0.5 * dt * m_intRate * (i+1); 75 | } 76 | 77 | vector u((nx-1)*(timeSteps+1)); 78 | 79 | double u0 = 0.0; 80 | for (i = 0; i < nx - 1; i++) 81 | { 82 | u0 += dx; 83 | u[i+0*(nx-1)] = std::max(u0 - m_strike, 0.0); 84 | } 85 | 86 | for (int j = 0; j < timeSteps; j++) 87 | { 88 | double t = (double)(j) * m_expiration /(double)timeSteps; 89 | 90 | double p = 0.5 * dt * (nx - 1) * (volatility*volatility * (nx-1) + m_intRate) 91 | * (m_maxPrice-m_strike * exp(-m_intRate*t ) ); 92 | 93 | for (i = 0; i < nx - 1; i++) 94 | { 95 | u[i+(j+1)*(nx-1)] = b[i] * u[i+j*(nx-1)]; 96 | } 97 | for (i = 0; i < nx - 2; i++) 98 | { 99 | u[i+(j+1)*(nx-1)] += c[i] * u[i+1+j*(nx-1)]; 100 | } 101 | for (i = 1; i < nx - 1; i++) 102 | { 103 | u[i+(j+1)*(nx-1)] += a[i] * u[i-1+j*(nx-1)]; 104 | } 105 | u[nx-2+(j+1)*(nx-1)] += p; 106 | } 107 | 108 | return u; 109 | } 110 | 111 | int main_bsmeth() 112 | { 113 | auto strike = 5.0; 114 | auto intRate = 0.03; 115 | auto sigma = 0.50; 116 | auto t1 = 1.0; 117 | auto numSteps = 11; 118 | auto numDays = 29; 119 | auto maxPrice = 10.0; 120 | 121 | 122 | BlackScholesMethod blackScholes(t1, maxPrice, strike, intRate); 123 | vector u = blackScholes.solve(sigma, numSteps, numDays); 124 | 125 | double minPrice = .0; 126 | for (int i=0; i < numSteps-1; i++) 127 | { 128 | double s = ((numSteps-i-2) * minPrice+(i+1)*maxPrice)/ (double)(numSteps-1); 129 | cout << " " << s << " " << u[i+numDays*(numSteps-1)] << endl; 130 | } 131 | return 0; 132 | } -------------------------------------------------------------------------------- /CppOptions/Matrix.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix.cpp 3 | // 4 | 5 | #include "Matrix.h" 6 | 7 | #include 8 | 9 | Matrix::Matrix(int size) 10 | { 11 | for (int i=0; i row(size, 0); 14 | m_rows.push_back(row); 15 | } 16 | } 17 | 18 | Matrix::Matrix(int size, int size2) 19 | { 20 | for (int i=0; i row(size2, 0); 23 | m_rows.push_back(row); 24 | } 25 | } 26 | 27 | Matrix::Matrix(const Matrix &s) 28 | : m_rows(s.m_rows) 29 | { 30 | } 31 | 32 | Matrix::~Matrix() 33 | { 34 | } 35 | 36 | Matrix &Matrix::operator=(const Matrix &s) 37 | { 38 | if (this != &s) 39 | { 40 | m_rows = s.m_rows; 41 | } 42 | return *this; 43 | } 44 | 45 | Matrix::Row &Matrix::operator[](int pos) 46 | { 47 | return m_rows[pos]; 48 | } 49 | 50 | void Matrix::transpose() 51 | { 52 | std::vector rows; 53 | for (unsigned i=0;i row; 56 | for (unsigned j=0; j rows; 118 | for (unsigned i=0; i row; 121 | for (unsigned j=0; j 9 | 10 | // include classes from QuantLib 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | using namespace QuantLib; 23 | 24 | using std::cout; 25 | using std::vector; 26 | using std::pair; 27 | 28 | using boost::shared_ptr; 29 | 30 | 31 | 32 | CDSSolver::CDSSolver(double val, double sigma, double divYield, double rf, 33 | double strike, double barrier, double rebate) 34 | : 35 | cur_val(val), 36 | sigma(sigma), 37 | divYield(divYield), 38 | rf(rf), 39 | strike(strike), 40 | barrier(barrier), 41 | rebate(rebate) 42 | { 43 | } 44 | 45 | // solve the valuation problem using the barrier technique, from today to the maturity date 46 | pair 47 | CDSSolver::solve(Date maturity_date) 48 | { 49 | Handle quote(boost::shared_ptr(new SimpleQuote(cur_val))); 50 | Date today = Date::todaysDate(); 51 | 52 | 53 | shared_ptr ts1(new FlatForward(today, divYield, Thirty360())); 54 | shared_ptr ts2(new FlatForward(today, rf, Thirty360())); 55 | shared_ptr vs(new BlackConstantVol(today, NullCalendar(),sigma, Thirty360())); 56 | 57 | auto process = BlackScholesMertonProcess(quote, 58 | Handle(ts1), 59 | Handle(ts2), 60 | Handle(vs)); 61 | 62 | 63 | shared_ptr payoff(new PlainVanillaPayoff(Option::Type::Call, strike)); 64 | shared_ptr exercise(new EuropeanExercise(maturity_date)); 65 | 66 | auto option = BarrierOption(Barrier::Type::UpIn, 67 | barrier, rebate, 68 | payoff, 69 | exercise); 70 | 71 | auto pproc = shared_ptr(&process); 72 | 73 | option.setPricingEngine(shared_ptr(new AnalyticBarrierEngine(pproc))); 74 | 75 | return std::make_pair(option, process); 76 | } 77 | 78 | void CDSSolver::generateGrid(BarrierOption &option, BlackScholesMertonProcess &process, const vector &grid) 79 | { 80 | double value = option.NPV(); 81 | Size maxG = grid[grid.size()-1]; // find maximum grid value 82 | 83 | for (auto g : grid) 84 | { 85 | FdBlackScholesBarrierEngine be(shared_ptr(&process), maxG, g); 86 | option.setPricingEngine(shared_ptr(&be)); 87 | 88 | cout << std::abs(option.NPV()/value -1); 89 | 90 | FdBlackScholesBarrierEngine be1(shared_ptr(&process), g, maxG); 91 | option.setPricingEngine(shared_ptr(&be1)); 92 | 93 | cout << std::abs(option.NPV()/value -1); 94 | } 95 | } 96 | 97 | void test_CDSSolver() 98 | { 99 | // use a few test values 100 | 101 | double cur_val = 50.0; 102 | double sigma = 0.2; 103 | double divYield = 0.01; 104 | double rf = 0.05; 105 | double strike = 104.0; 106 | double barrier = 85.0; 107 | double rebate = 0.0; 108 | 109 | CDSSolver solver(cur_val, sigma, divYield, rf, strike, barrier, rebate); 110 | 111 | Date date(10, Month::August, 2016); 112 | 113 | auto result = solver.solve(date); 114 | 115 | std::vector grid = { 5, 10, 25, 50, 100, 1000, 2000 }; 116 | solver.generateGrid(result.first, result.second, grid); 117 | } 118 | 119 | 120 | int main_cds() 121 | { 122 | test_CDSSolver(); 123 | return 0; 124 | } 125 | 126 | 127 | 128 | 129 | class CDSContract { 130 | public: 131 | CDSContract(); 132 | CDSContract(const CDSContract &p); 133 | ~CDSContract(); 134 | CDSContract &operator=(const CDSContract &p); 135 | 136 | double notional() { return m_notional; } 137 | double spread() { return m_spread; } 138 | int timePeriod() { return m_timePeriod; } 139 | bool payAtDefault() { return m_payAtDefault; } 140 | bool isLong() { return m_isLong; } 141 | 142 | private: 143 | 144 | double m_notional; 145 | double m_spread; 146 | int m_timePeriod; 147 | bool m_payAtDefault; 148 | bool m_isLong; 149 | 150 | }; 151 | 152 | #endif -------------------------------------------------------------------------------- /CppOptions/Functional.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Functional.cpp 3 | 4 | #include "Functional.hpp" 5 | 6 | #include 7 | #include 8 | 9 | #include // for functional STL code 10 | 11 | 12 | // 13 | // Class SimpleOption 14 | // 15 | 16 | double SimpleOption::getInTheMoneyProbability(int numDays, double currentUnderlyingPrice) const 17 | { 18 | return 0; // implementation here 19 | } 20 | 21 | 22 | // 23 | // Class OptionComparison 24 | // 25 | 26 | OptionComparison::OptionComparison(bool directionLess) 27 | : m_directionLess(directionLess) 28 | { 29 | } 30 | 31 | OptionComparison::OptionComparison(const OptionComparison &p) 32 | : m_directionLess(p.m_directionLess) 33 | { 34 | } 35 | 36 | OptionComparison::~OptionComparison() 37 | { 38 | } 39 | 40 | OptionComparison &OptionComparison::operator=(const OptionComparison &p) 41 | { 42 | if (this != &p) 43 | { 44 | m_directionLess = p.m_directionLess; 45 | } 46 | return *this; 47 | } 48 | 49 | bool OptionComparison::operator()(const SimpleOption &o1, const SimpleOption &o2) 50 | { 51 | bool result = false; 52 | 53 | // check components of opt1 and opt2. In practice this could be more complex. 54 | if (m_directionLess) 55 | { 56 | result = o1.daysToExpiration() < o2.daysToExpiration(); 57 | } 58 | else 59 | { 60 | result = o1.daysToExpiration() > o2.daysToExpiration(); 61 | } 62 | return result; 63 | } 64 | 65 | 66 | void test_compare() 67 | { 68 | OptionComparison comparison(true); 69 | 70 | SimpleOption a, b; 71 | // ... 72 | 73 | if (comparison(a, b)) 74 | { 75 | std::cout << " a is less than b " << std::endl; 76 | } 77 | else 78 | { 79 | std::cout << " b is less than a " << std::endl; 80 | } 81 | } 82 | 83 | 84 | void test_operator() 85 | { 86 | using namespace std; 87 | 88 | vector numbers = { 3, 4, 2, 1, 6 }; 89 | 90 | sort(numbers.begin(), numbers.end(), greater() ); 91 | 92 | } 93 | 94 | 95 | 96 | void test_transform() 97 | { 98 | using namespace std; 99 | 100 | auto list1 = { 3, 4, 2, 1, 6 }; 101 | auto list2 = { 4, 1, 5, 3, 2 }; 102 | 103 | vector result(list1.size()); 104 | 105 | transform(list1.begin(), list1.end(), list2.begin(), result.begin(), plus() ); 106 | 107 | // use transformed list here... 108 | 109 | copy(result.begin(), result.end(), std::ostream_iterator(cout, ", ")); 110 | 111 | // prints 7, 5, 7, 4, 8, 112 | } 113 | 114 | void use_bind() 115 | { 116 | using namespace std; 117 | using namespace std::placeholders; 118 | 119 | auto list1 = { 3, 4, 2, 1, 6 }; 120 | 121 | vector result(list1.size()); 122 | 123 | // add 3 to each element of the list 124 | transform(list1.begin(), list1.end(), result.begin(), bind(plus(), _1, 3)); 125 | 126 | copy(result.begin(), result.end(), std::ostream_iterator(cout, ", ")); 127 | 128 | // prints 6, 7, 5, 4, 9, 129 | } 130 | 131 | 132 | auto computeInTheMoneyProblExample(const std::vector &options) -> std::vector 133 | { 134 | using namespace std; 135 | using namespace std::placeholders; 136 | 137 | double currentPrice = 100.0; 138 | 139 | vector probabilities(options.size()); 140 | 141 | auto inTheMoneyCalc = bind(&SimpleOption::getInTheMoneyProbability, _1, 2, currentPrice); 142 | 143 | 144 | transform(options.begin(), options.end(), probabilities.begin(), inTheMoneyCalc); 145 | 146 | return probabilities; 147 | } 148 | 149 | 150 | void use_lambda() 151 | { 152 | auto fun = [](double x, double y) -> double { return x + y; }; 153 | 154 | double res = fun(4, 5); 155 | 156 | std::cout << " result is " << res << std::endl; 157 | } 158 | 159 | void use_lambda2() 160 | { 161 | int offset = 5; 162 | 163 | auto fun1 = [ offset](double x, double y) -> double { return x + y + offset; }; 164 | auto fun2 = [&offset](double x, double y) -> double { return x + y + offset; }; 165 | 166 | double res = fun1(4, 5); 167 | 168 | std::cout << " result is " << res << std::endl; 169 | 170 | offset = 10; 171 | std::cout << " result of fun1 is " << fun1(4, 5) << std::endl; 172 | std::cout << " result of fun2 is " << fun2(4, 5) << std::endl; 173 | } 174 | 175 | 176 | void use_function(std::function f) 177 | { 178 | auto res = f(2,3); 179 | 180 | std::cout << " the function returns the value " << res << std::endl; 181 | } 182 | 183 | void test_use_function() 184 | { 185 | auto f1 = [] (int a, int b) { return a + b; }; 186 | auto f2 = [] (int a, int b) { return a * b; }; 187 | 188 | use_function(f1); 189 | use_function(f2); 190 | } 191 | 192 | int main() 193 | { 194 | test_use_function(); 195 | return 0; 196 | } 197 | 198 | 199 | 200 | -------------------------------------------------------------------------------- /CppOptions/qlibsample.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // qlibsample.cpp 3 | 4 | #if 0 5 | 6 | #include "qlibsample.hpp" 7 | 8 | typedef double Real; 9 | 10 | using std::cout; 11 | using std::endl; 12 | 13 | using namespace QuantLib; 14 | 15 | 16 | 17 | void testingBlackScholesCalculator () 18 | { 19 | Real S0=100.0, K=105.0; 20 | Real rd =0.034 , rf =0.021 , tau =0.5 , vol =0.177; 21 | Real domDisc=std::exp(-rd*tau), forDisc=std::exp(-rf*tau); Real stdDev=vol*std::sqrt(tau); 22 | boost::shared_ptr vanillaPayoffPut( 23 | new QuantLib::PlainVanillaPayoff(Option::Put,K)); 24 | boost::shared_ptr aonPayoffCall( 25 | new AssetOrNothingPayoff(QuantLib::Option::Call,K)); 26 | QuantLib::BlackScholesCalculator vanillaPutPricer(vanillaPayoffPut,S0,forDisc,stdDev,domDisc); 27 | QuantLib::BlackScholesCalculator aonCallPricer(aonPayoffCall,S0,forDisc,stdDev,domDisc); 28 | std::cout << "--------------Vanilla Values -------------" << std::endl; 29 | std::cout << "Value:" << vanillaPutPricer.value() << std::endl; 30 | std::cout << "Delta:" << vanillaPutPricer.delta() << std::endl; 31 | std::cout << "Gamma:" << vanillaPutPricer.gamma() << std::endl; 32 | std::cout << "Vega:" << vanillaPutPricer.vega(tau) << std::endl; 33 | std::cout << "Theta:" << vanillaPutPricer.theta(tau) << std::endl; 34 | std::cout << "Delta Fwd:" << vanillaPutPricer.deltaForward() << std::endl; 35 | std::cout << "Gamma Fwd:" << vanillaPutPricer.gammaForward() << std::endl; 36 | std::cout << "-------------- AON Values-------------" << std::endl; 37 | std::cout << "Value:" << aonCallPricer.value() << std::endl; 38 | std::cout << "Delta:" << aonCallPricer.delta() << std::endl; 39 | std::cout << "Gamma:" << aonCallPricer.gamma() << std::endl; 40 | std::cout << "Vega:" << aonCallPricer.vega(tau) << std::endl; 41 | std::cout << "Theta:" << aonCallPricer.theta(tau) << std::endl; 42 | std::cout << "Delta Fwd:" << aonCallPricer.deltaForward() << std::endl; 43 | std::cout << "Gamma Fwd:" << aonCallPricer.gammaForward() << std::endl; 44 | 45 | 46 | } 47 | 48 | struct BlackScholesPrameters { 49 | double S0; 50 | double K; 51 | double rd; 52 | double rf; 53 | double tau; 54 | double vol; 55 | }; 56 | 57 | 58 | void callBlackScholes(BlackScholesPrameters &bp) 59 | { 60 | // create a vanilla option (standard option type) 61 | boost::shared_ptr 62 | vanillaPut(new QuantLib::PlainVanillaPayoff(Option::Put,bp.K)); 63 | 64 | // compute discount rates 65 | double cur_disc = std::exp(-bp.rd * bp.tau); // current discount rate 66 | double for_disc = std::exp(-bp.rf * bp.tau); // foward discount rate 67 | double stdev = bp.vol * std::sqrt(bp.tau); // standard deviation 68 | 69 | BlackScholesCalculator putPricer(vanillaPut, bp.S0, for_disc, stdev, cur_disc); 70 | 71 | // Print options greeks 72 | cout << "value:" << putPricer.value() << endl; 73 | cout << "delta:" << putPricer.delta() << endl; 74 | cout << "gamma:" << putPricer.gamma() << endl; 75 | cout << "vega:" << putPricer.vega(bp.tau) << endl; 76 | cout << "theta:" << putPricer.theta(bp.tau) << endl; 77 | cout << "delta Fwd:" << putPricer.deltaForward() << endl; 78 | cout << "gamma Fwd:" << putPricer.gammaForward() << endl; 79 | 80 | } 81 | 82 | void testBlackScholes() 83 | { 84 | BlackScholesPrameters bp; 85 | 86 | bp.S0 = 95.0; // current price 87 | bp.K = 100.0; // strike 88 | bp.rd = 0.026; // current rate of return 89 | bp.rf = 0.017; // forward rate of return 90 | bp.tau= 0.62; // tau (time greek) 91 | bp.vol= 0.193; // volatility 92 | 93 | callBlackScholes(bp); 94 | } 95 | 96 | 97 | 98 | void testDates() 99 | { 100 | Date date(10, Month::April, 2010); 101 | cout << "original date: " << date << endl; 102 | 103 | date += 2 * Days; 104 | cout << "after 2 days: " << date << endl; 105 | 106 | date += 3 * Months; 107 | cout << "after 3 months: " << date << endl; 108 | } 109 | 110 | void nextAndPreviousDay() 111 | { 112 | Date date(28, Month::February, 2010); 113 | cout << "original date: " << date << endl; 114 | 115 | date++; 116 | cout << "next day: " << date << endl; 117 | 118 | date--; 119 | cout << "previous day: " << date << endl; 120 | } 121 | 122 | void dateQuestions() 123 | { 124 | Date date(20, Month::February, 2010); 125 | 126 | 127 | cout << "weekday: " << date.weekday() << endl; 128 | 129 | cout << "is leap year? " << Date::isLeap(date.year()) << endl; 130 | 131 | cout << "is end of month? " << Date::isEndOfMonth(date) << endl; 132 | } 133 | 134 | void useCalendar() 135 | { 136 | Calendar cal = UnitedStates(UnitedStates::NYSE); 137 | 138 | cout << " list of holidays " << endl; 139 | for (auto date : Calendar::holidayList(cal, Date(1, Month::Jan, 2010), Date(1, Month::Jan, 2012))) 140 | { 141 | cout << " " << date; 142 | } 143 | 144 | cout << " is Jan 1 2010 a business day? " 145 | << cal.isBusinessDay(Date(1, Month::Jan, 2010)) << endl; 146 | cout << " is Jan 1 2010 a holiday? " 147 | << cal.isHoliday(Date(1, Month::Jan, 2010)) << endl; 148 | cout << " is Jan 1 2010 end of month? " 149 | << cal.isEndOfMonth(Date(1, Month::Jan, 2010)) << endl; 150 | } 151 | 152 | Calendar createNewCalendar() 153 | { 154 | Calendar newCal = UnitedStates(UnitedStates::NYSE); 155 | 156 | // Remove winter holiday 157 | newCal.removeHoliday(Date(25, Month::December, 2016)); 158 | 159 | // Add international worker's day 160 | newCal.addHoliday (Date(1, Month::May, 2016)); 161 | 162 | cout << " list of holidays " << endl; 163 | for (auto date : Calendar::holidayList(newCal, Date(1, Month::Jan, 2016), Date(1, Month::Jan, 2016))) 164 | { 165 | cout << " " << date; 166 | } 167 | 168 | return newCal; 169 | } 170 | 171 | int getNumberOfDays(Date d1, Date d2) 172 | { 173 | Calendar usCal = UnitedStates(UnitedStates::NYSE); 174 | 175 | int nDays = usCal.businessDaysBetween(d1, d2); 176 | 177 | cout << " the interval size is " << nDays << endl; 178 | 179 | return nDays; 180 | } 181 | 182 | 183 | int main_qlib() 184 | { 185 | 186 | cout << " hello \n" ; 187 | return 0; 188 | //testingBlackScholesCalculator(); 189 | } 190 | 191 | #endif -------------------------------------------------------------------------------- /CppOptions/LatticeModel.cpp: -------------------------------------------------------------------------------- 1 | // 2 | 3 | #include "LatticeModel.hpp" 4 | 5 | #include 6 | #include 7 | 8 | using vec = std::vector; 9 | 10 | 11 | double computePO(int ptype, double S, double X) 12 | { 13 | if (ptype == 0) 14 | { 15 | return std::max(0.0, S - X); 16 | } 17 | else 18 | { 19 | return std::max(0.0, X - S); 20 | } 21 | } 22 | 23 | 24 | //void dolatice(int N, 25 | // double S0, 26 | // double rr, 27 | // double sigma, 28 | // int k, int T, double X, int ptype) 29 | //{ 30 | // vec this_s(2*N); 31 | // vec this_o(2*N); 32 | // vec next_s(2*N); 33 | // vec next_o(2*N); 34 | // 35 | // double dt = T / N; 36 | // double drift = (rr - 0.5 * sigma * sigma) * T; 37 | // double vol = sigma * sqrt(k * dt); 38 | // double disc_dt = exp(-rr * dt); 39 | // double scale_s = exp(-drift / N); 40 | // 41 | // for (int i=0; i<2*N; ++i) 42 | // { 43 | // this_s[i] = S0 * exp(drift + (i-N) * vol); 44 | // this_o[i] = computePO(ptype, this_s[i], X); 45 | // } 46 | // 47 | // vec probs(3); 48 | // probs[0] = 0.5 / k; 49 | // probs[1] = (k-1)/k; 50 | // probs[2] = probs[0]; 51 | // 52 | // int interval = 10; 53 | // 54 | // for (int i=N-1; i>=0; --i) 55 | // { 56 | // if (i % interval == 0) 57 | // { 58 | // 59 | // } 60 | // for (int j=0; j<2*N; ++j) 61 | // { 62 | // double v =0; 63 | // v += probs[0] * this_o[j]; 64 | // v += probs[1] * this_o[j+1]; 65 | // v += probs[2] * this_o[j+2]; 66 | // 67 | // next_o[i] = disc_dt * v; // discounted value 68 | // } 69 | // 70 | // this_o = next_o; 71 | // 72 | // if (1) 73 | // { // american option only 74 | // for (int j=0; j<2*N; ++j) 75 | // { 76 | // next_s[j] = scale_s * this_s[j]; 77 | // } 78 | // 79 | // this_s = next_s; 80 | // 81 | // double payoff; 82 | // for (int j=0; j<2*N; ++j) 83 | // { 84 | // payoff = computePO(ptype, this_s[j], X); 85 | //// this_o = 86 | // } 87 | // } 88 | // } 89 | // 90 | //} 91 | 92 | 93 | class BinomialModel { 94 | public: 95 | BinomialModel(const BinomialModel &p); 96 | ~BinomialModel(); 97 | BinomialModel &operator=(const BinomialModel &p); 98 | 99 | BinomialModel(double T, // expiration time 100 | double S, // stock price 101 | double r, // interest rate 102 | double sigma, 103 | double q, // dividend yield 104 | int n, // numer of steps 105 | bool call 106 | ); 107 | 108 | double optionPriceForStrike(double K); 109 | virtual void computePriceStep(int i, int j, double K, vec &prices, 110 | double p_u, double p_d, double u); 111 | 112 | 113 | protected: 114 | double getStockPrice() { return m_S; } 115 | private: 116 | double m_T; // expiration time 117 | double m_S; // stock price 118 | double m_r; // interest rate 119 | double m_sigma; // volatility 120 | double m_q; // dividend yield 121 | int m_n; // numer of steps 122 | bool m_call; // true = call, false = put 123 | 124 | }; 125 | 126 | 127 | class AmericanBinomialModel : public BinomialModel { 128 | AmericanBinomialModel(const BinomialModel &p); 129 | ~AmericanBinomialModel(); 130 | AmericanBinomialModel &operator=(const BinomialModel &p); 131 | 132 | AmericanBinomialModel(double T, // expiration time 133 | double S, // stock price 134 | double r, // interest rate 135 | double sigma, 136 | double q, // dividend yield 137 | int n, // numer of steps 138 | bool call 139 | ); 140 | 141 | virtual void computePriceStep(int i, int j, double K, vec &prices, 142 | double p_u, double p_d, double u); 143 | }; 144 | 145 | 146 | 147 | AmericanBinomialModel::AmericanBinomialModel(const BinomialModel &p) 148 | : BinomialModel(p) 149 | { 150 | } 151 | 152 | AmericanBinomialModel::~AmericanBinomialModel() 153 | { 154 | } 155 | 156 | AmericanBinomialModel &AmericanBinomialModel::operator=(const BinomialModel &p) 157 | { 158 | BinomialModel::operator=(p); // no new data members in this calss 159 | return *this; 160 | } 161 | 162 | AmericanBinomialModel::AmericanBinomialModel(double T, // expiration time 163 | double S, // stock price 164 | double r, // interest rate 165 | double sigma, 166 | double q, // dividend yield 167 | int n, // numer of steps 168 | bool call) 169 | : BinomialModel(T, S, r, sigma, q, n, call) 170 | { 171 | } 172 | 173 | void AmericanBinomialModel::computePriceStep(int i, int j, double K, vec &prices, double p_u, double p_d, double u) 174 | { 175 | BinomialModel::computePriceStep(i, j, K, prices, p_u, p_d, u); 176 | 177 | // compute exercise price for American option 178 | // 179 | double exercise = K - getStockPrice() * pow(u, 2*i - j); 180 | if (prices[i] < exercise) 181 | { 182 | prices[i] = exercise; 183 | } 184 | } 185 | 186 | 187 | // ------------ 188 | 189 | BinomialModel::BinomialModel(double T, double S, double r, 190 | double sigma, 191 | double q, 192 | int n, bool call) 193 | : m_T(T), 194 | m_S(S), 195 | m_r(r), 196 | m_sigma(sigma), 197 | m_n(n), 198 | m_q(q), 199 | m_call(call) 200 | { 201 | } 202 | 203 | BinomialModel::BinomialModel(const BinomialModel &p) 204 | : m_T(p.m_T), 205 | m_S(p.m_S), 206 | m_r(p.m_r), 207 | m_sigma(p.m_sigma), 208 | m_n(p.m_n), 209 | m_q(p.m_q), 210 | m_call(p.m_call) 211 | { 212 | } 213 | 214 | BinomialModel::~BinomialModel() 215 | { 216 | } 217 | 218 | BinomialModel &BinomialModel::operator=(const BinomialModel &p) 219 | { 220 | if (this != &p) 221 | { 222 | m_T = p.m_T; 223 | m_S = p.m_S; 224 | m_r = p.m_r; 225 | m_sigma = p.m_sigma; 226 | m_n = p.m_n; 227 | m_q = p.m_q; 228 | m_call = p.m_call; 229 | } 230 | return *this; 231 | } 232 | 233 | void BinomialModel::computePriceStep(int i, int j, double K, 234 | vec &prices, double p_u, double p_d, double u) 235 | { 236 | prices[i] = p_u * prices[i] + p_d * prices[i+1]; 237 | } 238 | 239 | double BinomialModel::optionPriceForStrike(double K) 240 | { 241 | 242 | double delta = m_T / m_n; // size of each step 243 | double u = exp(m_sigma * sqrt(delta)); 244 | 245 | double p_u = (u * exp(-m_r * delta) - exp(-m_q * delta)) * u / (u*u - 1); 246 | double p_d = exp(-m_r * delta) - p_u; 247 | 248 | vec prices(m_n); 249 | 250 | // compute last day values (leafs of the tree) 251 | for (int i= 0; i=0; --j) 264 | { 265 | for (int i = 0; i 8 | #include 9 | 10 | using std::cout; 11 | using std::endl; 12 | 13 | using std::string; 14 | 15 | Date::Date(int year, int month, int day) 16 | : m_year(year), 17 | m_month(month), 18 | m_day(day), 19 | m_weekDay(DayOfTheWeek_UNKNOWN) 20 | { 21 | } 22 | 23 | Date::~Date() 24 | { 25 | } 26 | 27 | Date::Date(const Date &p) 28 | : m_year(p.m_year), 29 | m_month(p.m_month), 30 | m_day(p.m_day), 31 | m_weekDay(p.m_weekDay), 32 | m_holidays(p.m_holidays) 33 | { 34 | } 35 | 36 | Date &Date::operator=(const Date &p) 37 | { 38 | if (&p != this) 39 | { 40 | m_day = p.m_day; 41 | m_month = p.m_month; 42 | m_year = p.m_year; 43 | m_weekDay = p.m_weekDay; 44 | m_holidays = p.m_holidays; 45 | } 46 | return *this; 47 | } 48 | 49 | bool Date::operator<(const Date &d) const 50 | { 51 | if (m_year < d.m_year) return true; 52 | if (m_year == d.m_year && m_month < d.m_month) return true; 53 | if (m_year == d.m_year && m_month == d.m_month && m_day < d.m_day) return true; 54 | return false; 55 | } 56 | 57 | bool Date::operator==(const Date &d) 58 | { 59 | return d.m_day == m_day && d.m_month == m_month && d.m_year == m_year; 60 | } 61 | 62 | void Date::setHolidays(const std::vector &days) 63 | { 64 | m_holidays = days; 65 | } 66 | 67 | bool Date::isHoliday() 68 | { 69 | return std::find(m_holidays.begin(), m_holidays.end(), *this) != m_holidays.end(); 70 | } 71 | 72 | std::string Date::month() 73 | { 74 | switch (m_month) { 75 | case Month_January: return "January"; 76 | case Month_February: return "February"; 77 | case Month_March: return "March"; 78 | case Month_April: return "April"; 79 | case Month_May: return "May"; 80 | case Month_June: return "June"; 81 | case Month_July: return "July"; 82 | case Month_August: return "August"; 83 | case Month_September: return "September"; 84 | case Month_October: return "October"; 85 | case Month_November: return "November"; 86 | case Month_December: return "December"; 87 | default: throw std::runtime_error("unknown month"); 88 | } 89 | return ""; 90 | } 91 | 92 | #define self this 93 | 94 | std::string Date::dayOfWeek() 95 | { 96 | switch (this->dayOfTheWeek()) { 97 | case DayOfTheWeek_Sunday: return "Sunday"; 98 | case DayOfTheWeek_Monday: return "Monday"; 99 | case DayOfTheWeek_Tuesday: return "Tuesday"; 100 | case DayOfTheWeek_Wednesday: return "Wednesday"; 101 | case DayOfTheWeek_Thursday: return "Thursday"; 102 | case DayOfTheWeek_Friday: return "Friday"; 103 | case DayOfTheWeek_Saturday: return "Saturday"; 104 | default: throw std::runtime_error("unknown day of week"); 105 | } 106 | } 107 | 108 | void Date::add(int numDays) 109 | { 110 | for (int i=0; i(day); 219 | return m_weekDay; 220 | } 221 | 222 | bool Date::isWeekDay() 223 | { 224 | DayOfTheWeek dayOfWeek = dayOfTheWeek(); 225 | if (dayOfWeek == DayOfTheWeek_Sunday || dayOfWeek == DayOfTheWeek_Saturday) 226 | { 227 | return false; 228 | } 229 | return true; 230 | } 231 | 232 | bool Date::isTradingDay() 233 | { 234 | if (!isWeekDay()) return false; 235 | if (m_holidays.size() == 0) return true; 236 | if (isHoliday()) return false; 237 | return true; 238 | } 239 | 240 | Date Date::nextTradingDay() 241 | { 242 | Date d = *this; 243 | if (d.isTradingDay()) 244 | { 245 | return ++d; 246 | } 247 | while (!d.isTradingDay()) 248 | { 249 | ++d; 250 | } 251 | return d; 252 | } 253 | 254 | bool Date::isLeapYear() 255 | { 256 | if (m_year % 4 != 0) return false; 257 | if (m_year % 100 != 0) return true; 258 | if (m_year % 400 != 0) return false; 259 | return true; 260 | } 261 | 262 | Date &Date::operator--() 263 | { 264 | if (m_weekDay != DayOfTheWeek_UNKNOWN) // update weekday 265 | { 266 | if (m_weekDay == DayOfTheWeek_Sunday) 267 | m_weekDay = DayOfTheWeek_Saturday; 268 | else 269 | m_weekDay = static_cast(m_weekDay - 1); 270 | } 271 | 272 | if (m_day > 1) 273 | { 274 | m_day--; 275 | return *this; 276 | } 277 | 278 | if (m_month == Month_January) 279 | { 280 | m_month = Month_December; 281 | m_day = 31; 282 | m_year--; 283 | return *this; 284 | } 285 | 286 | m_month--; 287 | 288 | if (m_month == Month_February) 289 | { 290 | m_day = isLeapYear() ? 29 : 28; 291 | return *this; 292 | } 293 | 294 | // list of months with 31 days 295 | std::vector monthsWithThirtyOneDays = { 1, 3, 5, 7, 8, 10, 12 }; 296 | if (std::find(monthsWithThirtyOneDays.begin(), 297 | monthsWithThirtyOneDays.end(), m_month) 298 | != monthsWithThirtyOneDays.end()) 299 | { 300 | m_day = 31; 301 | } 302 | else 303 | { 304 | m_day = 30; 305 | } 306 | return *this; 307 | } 308 | 309 | Date &Date::operator++() 310 | { 311 | // list of months with 31 days 312 | std::vector monthsWithThirtyOneDays = { 1, 3, 5, 7, 8, 10, 12 }; 313 | 314 | if (m_day == 31) 315 | { 316 | m_day = 1; 317 | m_month++; 318 | } 319 | else if (m_day == 30 && 320 | std::find(monthsWithThirtyOneDays.begin(), 321 | monthsWithThirtyOneDays.end(), m_month) 322 | == monthsWithThirtyOneDays.end()) 323 | { 324 | m_day = 1; 325 | m_month++; 326 | } 327 | else if (m_day == 29 && m_month == 2) 328 | { 329 | m_day = 1; 330 | m_month++; 331 | } 332 | else if (m_day == 28 && m_month == 2 && !isLeapYear()) 333 | { 334 | m_day = 1; 335 | m_month++; 336 | } 337 | else 338 | { 339 | m_day++; 340 | } 341 | 342 | if (m_month > 12) 343 | { 344 | m_month = 1; 345 | m_year++; 346 | } 347 | 348 | if (m_weekDay != DayOfTheWeek_UNKNOWN) // update weekday 349 | { 350 | if (m_weekDay == DayOfTheWeek_Saturday) 351 | m_weekDay = DayOfTheWeek_Sunday; 352 | else 353 | m_weekDay = static_cast(m_weekDay + 1); 354 | } 355 | return *this; 356 | } 357 | 358 | void Date::print() 359 | { 360 | cout << m_year << "/" << m_month << "/" << m_day << endl; 361 | } 362 | 363 | 364 | //int main_dat() 365 | int main_date() 366 | { 367 | Date d(2015, 9, 12); 368 | DayOfTheWeek wd = d.dayOfTheWeek(); 369 | cout << " day of the week: " << wd << " " << d.dayOfWeek() << endl; 370 | d.print(); 371 | 372 | d.add(25); 373 | d.print(); 374 | 375 | d.addTradingDays(120); 376 | d.print(); 377 | cout << " day of the week: " << d.dayOfTheWeek() << " " << d.dayOfWeek() << endl; 378 | 379 | 380 | return 0; 381 | } -------------------------------------------------------------------------------- /CppOptions/stl_alg.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // stl_alg.cpp 3 | 4 | #include "stl_alg.hpp" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using std::vector; 13 | using std::cout; 14 | using std::endl; 15 | using std::pair; 16 | using std::list; 17 | 18 | class Date { 19 | public: 20 | // other public methods here 21 | bool operator<(const Date &d); 22 | 23 | int year() const { return m_year; } 24 | int month() const { return m_month; } 25 | int day() const { return m_day; } 26 | private: 27 | int m_day; 28 | int m_month; 29 | int m_year; 30 | }; 31 | 32 | bool Date::operator<(const Date &d) 33 | { 34 | if (m_year < d.m_year) 35 | { 36 | return true; 37 | } 38 | if (m_year == d.m_year and m_month < d.m_month) 39 | { 40 | return true; 41 | } 42 | if (m_year == d.m_year and m_month == d.m_month and m_day < d.m_day) 43 | { 44 | return true; 45 | } 46 | return false; 47 | } 48 | 49 | bool operator<(const Date &a, const Date &b) 50 | { 51 | return a < b; 52 | } 53 | 54 | bool year_comparison(const Date &a, const Date &b) 55 | { 56 | return a.year() < b.year(); 57 | } 58 | 59 | void sort_dates() 60 | { 61 | vector dates; 62 | // .... 63 | 64 | 65 | std::sort(dates.begin(), dates.end()); // normal comparison 66 | 67 | std::sort(dates.begin(), dates.end(), year_comparison); // comparison by year 68 | } 69 | 70 | void compute_frequency(vector &prices, double start, double end, double step) 71 | { 72 | 73 | int nbins = int(std::abs(end-start)/step); 74 | 75 | vector> count(nbins, std::make_pair(0,0)); 76 | for (int i=0; i ivector(100, 1); 106 | vector dvector(100); 107 | 108 | std::copy(ivector.begin(), ivector.end(), dvector.begin()); 109 | } 110 | 111 | void print_prices() 112 | { 113 | vector prices(100); 114 | 115 | // initialize prices here 116 | 117 | std::copy(prices.begin(), prices.end(), std::ostream_iterator(cout)); 118 | } 119 | 120 | void from_list_to_vector(const list &l) 121 | { 122 | vector values; 123 | 124 | // copy contents to destination array values 125 | std::copy(l.begin(), l.end(), values.begin()); 126 | } 127 | 128 | template 129 | typename T::const_iterator find(const T &a, S val) { 130 | return std::find (a.begin(), a.end(), val); 131 | } 132 | 133 | void find_value() 134 | { 135 | vector values; 136 | // ... initialize the vector 137 | 138 | 139 | vector::const_iterator aresult = find (values, 42); 140 | if (aresult == values.end()) 141 | { 142 | return; 143 | } 144 | 145 | vector::iterator result = std::find(values.begin(), values.end(), 42); 146 | if (result == values.end()) 147 | { 148 | cout << " the value was not found " << endl; 149 | } 150 | else 151 | { 152 | cout << " the value found is " << *result << endl; 153 | } 154 | } 155 | 156 | bool greater_than_100(int num) 157 | { 158 | return num > 100; 159 | } 160 | 161 | 162 | void conditional_find() 163 | { 164 | vector values; 165 | // ... initialize the vector 166 | 167 | vector::iterator result = std::find_if(values.begin(), values.end(), greater_than_100); 168 | if (result == values.end()) 169 | { 170 | cout << " the value was not found " << endl; 171 | } 172 | else 173 | { 174 | cout << " the value found is " << *result << endl; 175 | } 176 | } 177 | 178 | 179 | class StandardOption { 180 | public: 181 | StandardOption() : m_daysToExpiration() {} 182 | StandardOption(int days); 183 | StandardOption(const StandardOption &p); 184 | ~StandardOption(); 185 | StandardOption &operator=(const StandardOption &p); 186 | 187 | int daysToExpiration() const { return m_daysToExpiration; } 188 | 189 | // other function members here ... 190 | private: 191 | int m_daysToExpiration; 192 | // other data members here ... 193 | }; 194 | 195 | StandardOption::StandardOption(int days) 196 | : m_daysToExpiration(days) 197 | { 198 | } 199 | 200 | StandardOption::StandardOption(const StandardOption &p) 201 | : m_daysToExpiration(p.m_daysToExpiration) 202 | { 203 | } 204 | 205 | StandardOption::~StandardOption() 206 | { 207 | } 208 | 209 | StandardOption &StandardOption::operator=(const StandardOption &p) 210 | { 211 | if (this != &p) 212 | { 213 | m_daysToExpiration = p.m_daysToExpiration; 214 | } 215 | return *this; 216 | } 217 | 218 | 219 | 220 | bool is_expiring(const StandardOption &opt) 221 | { 222 | return opt.daysToExpiration() < 10; 223 | } 224 | 225 | vector find_expiring_options(vector &options) 226 | { 227 | vector result(options.size()); 228 | std::copy_if(options.begin(), options.end(), result.begin(), is_expiring); 229 | if (result.size()) 230 | { 231 | cout << " no expiring option was found " << endl; 232 | } 233 | return result; 234 | } 235 | 236 | 237 | int main_stlalg() 238 | { 239 | vector prices = //{32.3, 34, 35.6, 39.2, 38.7, 31.17, 33.14 }; 240 | { 42.78184445930497,43.67760039111022,42.03826375399276,47.91999821950942,49.93858797561979,42.13542043645291, 241 | 38.30163313879867,38.50027414977689,37.7250636004348,43.70213348996104,36.38765415703016,47.25755355070849, 242 | 35.56734542552674,35.99168991528921,37.50304207091226,38.51564887862568,44.87803633063664,39.85416161118745, 243 | 47.44804469670797,48.02670819457711,37.28933459695028,40.41342291119492,38.2241431256981,44.62217947447723, 244 | 41.03013931222591,35.26208364477185,39.29846553276695,40.65633985086487,47.0228452753842,45.34835153322644, 245 | 39.39700907758517,52.27811213309643,42.63383425974795,40.89249700685397,41.10156246193978,39.47450552642528, 246 | 45.59502602477541,37.32130193926878,36.10652743201731,36.83751340467555,38.22808121445735,40.7856881669989, 247 | 31.36116897916119,33.11108500567489,38.86526465282272,42.33702645101733,39.2742471198834,41.61902173680947, 248 | 32.49702576258007,40.02264414256337,34.85704183624394,35.15089912484926,36.77881561140303,45.72989880475985, 249 | 44.43533847264153,29.27318209468997,44.97598111118879,35.97246951040285,41.73849359918691,30.68554286551367, 250 | 34.66388957398927,43.80717757989846,39.7612215330862,47.03770824841743,44.92093808726787,48.11052662262456, 251 | 36.4622802991489,31.3763438464966,35.09788668863187,31.09792864705171,42.1708919897512,40.02911731364738, 252 | 39.46342929025483,45.81695148690243,43.63955234200387,42.17909414196498,37.60123847103494,43.25668427477181, 253 | 41.03317096943389,46.97461970715167,44.91857530250557,37.0296330565591,36.09364032256888,37.33186444898434, 254 | 38.37820863425179,42.128977451259,33.12287928040585,28.13889923699566,46.93552821004177,39.08490400296763, 255 | 41.40965593696255,40.39683039643357,48.26282345158097,41.38851709709769,47.37773643923619,39.93781367520791, 256 | 39.11047037239822,43.87488862041591,41.84229452562683,39.11307544315284,37.12839597652669,44.45813941412536, 257 | 46.73377993985634,40.11521346072037,34.02015829573072,39.21151096244717,42.5561533675194,36.66359384121515, 258 | 41.03770604517,37.58841778747188,37.36556871510344,30.77799770440181,42.64301727420439,46.26442977570423, 259 | 35.33601025545288,46.31322886198627,37.01799127095718,38.99354811415017,32.79446899107094,41.70857533970244, 260 | 40.87126425385425,32.85178742653525,37.55174479960902,47.18768755976919,42.21866580998352,41.63945505098134, 261 | 36.61444201381163,29.64780175729441,40.77940730811751,39.46542059833772,42.89910948035585,40.12317371206829, 262 | 42.43411461262851,35.88329424968147,29.01277874836401,38.56417020122005,36.50605380753814,40.55350429022879, 263 | 45.36041676143943,38.87427624108659,36.75057400908258,34.80509783081789,46.38341144819815,30.97062663171186, 264 | 39.46841998395084,26.10220881020765,34.20563388133634,40.71062702993218,40.12931797110947,31.38512872543786, 265 | 33.13674967978706,39.62942685602963,46.36085067393131,39.80501033808399,38.15726700221849,44.39924363525626, 266 | 33.2093433391518,31.69765959365117,42.33574480376637,37.33957414264603,45.33505715441738,47.15451393845362, 267 | 34.26571594660756,39.26995986487044,40.48923994719119,28.03197363217081,42.03042938796098,48.29611813824376, 268 | 32.65568118134725,33.03599145413803,38.77497405042885,37.54218522530394,36.73257681618899,31.74650042342772, 269 | 43.70466470964404,43.95509640314371,40.47615133493548,46.43657359714938,42.15964554414583,35.68711681489874, 270 | 42.41889757076655,44.52894333095128,38.23053461983582,33.54328535346639,39.20584181138072,38.46221246681366, 271 | 40.16257848097431,34.87088841002296,44.46483772617439,42.24668745434034,47.84207775896748,44.97672876224185, 272 | 34.50511961621996,36.25259304044854,48.70433158530048,38.65071818129881,41.12872526425293,36.8081868413574, 273 | 39.16674403418639,42.36081001508734,29.91648554931484,40.79507104027888,35.21155198669553,39.86467636572654, 274 | 34.16312678619537,44.28402670706689,32.2972327479956,49.71775839494127,31.85655354085528,42.69305364709465, 275 | 35.31978746186365,38.39450080730782,51.25056893970791,34.72778805729362,36.32614446260754,33.43739285911308, 276 | 38.54791259888807,34.49410615577555,44.75187351018732,45.39390925523594,35.96500752668668,41.54084212999258, 277 | 47.06467445316652,38.39843558787609,34.89666625244398,47.16228016827771,32.04148221250703,35.31171527834899, 278 | 40.42768570708395,42.98450221591088,34.95534551402104,45.56143682645318,43.67081394265119,37.30970243365253, 279 | 47.33704387524431,40.49347008302541,46.93380934436018,41.85656677613047,43.79909661106802,34.70490035774502, 280 | 36.56227975761775,35.99756822956058,40.31782436791381,41.47620622591621,47.39499139910249,29.76361427786139, 281 | 47.51448580510061,52.90916098669709,40.90020357863639,37.43710357613011,37.90639383683421,29.5225427825027, 282 | 45.10107112650064,42.81735023814488,38.56173403457683,28.71518973969216,35.4745320400253,38.1960900110268, 283 | 43.08875563412099,42.65915013188986,40.20701390247016,47.34133201355534,36.32572334393589,35.01705113251843, 284 | 35.5712158922518,38.39474522298454,28.81977157422747,45.55283128685715,37.67622910978037,41.46910993158829, 285 | 37.12918278792814,37.97333496653032,43.36143751335894,33.18434874172782,45.11338835476703,43.06015372494461, 286 | 30.80180131883006,34.50602074468794,45.17317489203142,33.7276490034627,49.53753463483577,45.85841071421091, 287 | 43.02365361972821,46.58265545026941,31.32267887691327,30.84752111873338,48.31335443087461,44.45394196493817, 288 | 42.76587932441861,44.4780979288028,42.26632945954473,33.50331196186566,38.79403203262196,38.69960822121119, 289 | 36.32280301539119,32.40982007652114,44.75746173057647,40.45497946850615,44.69217800086803,41.36346719520907, 290 | 36.85109237756015,39.96877678729342,40.35461240877066,40.91148989776364,35.26240177760784,35.48287092375762, 291 | 38.58465950042369,43.84253921028062,34.51879591594815,40.64679267671642,40.53928342449839,39.18602482572086, 292 | 36.01912495449452,35.34399985569668,38.96216402722161,36.43618650109931,38.52661462890424,38.95256437398413, 293 | 39.70645200370718,40.89035907475167,30.35549814699446,40.41779590913442,40.15272957970845,33.07259054852572, 294 | 38.87556911017958,38.34240699043298,37.90850080011717,36.89488047726263,53.22072610927503,42.67238777483797, 295 | 41.0412322196039,44.5538879373734,32.71162481918658,37.3528249148507,41.79667763835875,42.8961194793476, 296 | 43.8709213919598,39.58930822003997,54.1117190718699,49.5526479474614,38.10265766021563,38.3328362883291, 297 | 43.16194670143282,48.32865145554413,49.77219291224301,37.3563694900509,46.25511476472802,42.16008139722462, 298 | 40.13524190936413,38.98346514030116,34.35142142925718,30.01386924975923,37.89573116126832,43.70972077192636, 299 | 37.27034973834047,39.31443471387625,44.07161877381326,45.80342255388998,35.87713938023972,39.91225256774336, 300 | 41.3959892114833,46.0725782312095,42.77872322109898,31.11185961679133,40.45032983311206,43.9421772825232, 301 | 40.15968817379245,38.16683344854705,49.55977034103568,43.45626420932511,37.55532667654142,41.3907819459214, 302 | 39.90979226272548,35.60428341330544,45.35212021797298,42.52938644602645,38.55952972080569,40.29431115502922, 303 | 40.41342092411278,50.52770393302511,41.2319887087997,44.91666684917468,37.13675226533199,35.3562573099391, 304 | 32.84725721979154,35.29073977284445,42.34288305769034,25.84026261785755,39.62245050708788,44.84604915217371, 305 | 40.11578341866193,39.09919559200785,42.06175050871919,36.02303037427221,37.82331197692943,35.05942720028779, 306 | 36.88813138017978,36.03711817827941,43.98757052666639,35.77717057739348 }; 307 | compute_frequency(prices, 30.0, 50.0, 0.8); 308 | return 0; 309 | } 310 | 311 | --------------------------------------------------------------------------------