面向过程的标准欧式看涨期权二叉树定价
本帖隐藏的内容
- // by fantuanxiaot
- // by fantuanxiaot
- // 面向过程的标准欧式看涨期权二叉树定价
- // 面向过程的标准欧式看涨期权二叉树定价
- // 头文件的准备
- #include <iostream>
- #include <cmath>
- using namespace std;
- // 定义最大值的函数
- double max(double x,double y)
- {
- return(x>y?x:y);
- }
- // 定义次方的函数
- double cifang(double x,int n)
- {
- double z=1;
- if (n==0)
- {
- return(z);
- } else
- {
- while (n>=1)
- {
- z=z*x;
- n=n-1;
- }
- return(z);
- }
-
- }
- // 主函数
- int main()
- {
- // 初始价格S=10
- double S=10;
- // 目标价格K=12
- double K=12;
- // 时间是一年
- double T=1;
- // 时间共分为5期
- double N=5;
- // 年波动率定义
- double sigma=0.3;
- // 年无风险收益率r
- double r=0.03;
- // 定义二叉树矩阵算法,因为是5期
- double Matrix[6][6];
- // 定义deltaT和概率
- double deltaT, Probability, u, d;
- deltaT=T/N;
- u=exp(sigma*sqrt(deltaT));
- d=exp(-sigma*sqrt(deltaT));
- Probability=(1+r*deltaT-d)/(u-d);
- // 以上的数据准备完毕
- int i,j;
- // 二叉树末端定价
- for(i=0;i<=5;i++)
- {
- Matrix[i][5]=max(0,S*cifang(u,N-i)*cifang(d,i)-K);
- }
- // 二叉树回馈
- for(i=4;i>=0;i--)
- {
- for(j=0;j<=i;j++)
- {
- Matrix[j][i]=exp(-r*deltaT)*((1-Probability)*(Matrix[j+1][i+1])+Probability*(Matrix[j][i+1]));
- }
- }
- // 输出期权价格
- cout<<"The Call Option Pricing is "<<Matrix[0][0]<<endl;
- return 0;
- }
面向对象的标准欧式看涨期权二叉树定价
本帖隐藏的内容
- // 基于面向对象的欧式看涨期权定价
- // 基于面向对象的欧式看涨期权定价
- // by fantuanxiaot
- // by fantuanxiaot
- #include <cmath>
- #include <iostream>
- using namespace std;
- // 定义一个CallOptionPricing的类
- class CallOptionPricing
- {
- private:
- double T; // 时间
- int N; // 分期
- double S; // 初始价格
- double K; // 目标价格
- double sigma; // 波动率
- double r; // 无风险利率
- public:
- // 参数初始化,构造默认参数
- CallOptionPricing(double T1=1, double N1=5, double S1=10,double K1=15,double sigma1=0.3, double r1=0.03)
- {
- T=T1;
- N=N1;
- S=S1;
- K=K1;
- sigma=sigma1;
- r=r1;
- }
- // 定义次方函数
- double cifang(double x,int n)
- {
- double z=1;
- if (n==0)
- {
- return(z);
- } else
- {
- while (n>=1)
- {
- z=z*x;
- n=n-1;
- }
- return(z);
- }
- }
- double max(double x, double y)
- {
- return(x>y?x:y);
- }
- // 定义求值函数
- void Price()
- {
- double Matrix[100][100];
- int i,j;
- double Probability,u,d,deltaT;
- deltaT=T/N;
- u=exp(sigma*sqrt(deltaT));
- d=exp(-sigma*sqrt(deltaT));
- Probability=(1+r*deltaT-d)/(u-d);
- for(i=0;i<=N;i++)
- {
- Matrix[i][N]=max(0,S*cifang(u,N-i)*cifang(d,i)-K);
- }
- for(i=N-1;i>=0;i--)
- {
- for(j=0;j<=i;j++)
- {
- Matrix[j][i]=exp(-r*deltaT)*((1-Probability)*(Matrix[j+1][i+1])+Probability*(Matrix[j][i+1]));
- }
- }
- cout<<"The Call Option Pricing is "<<Matrix[0][0]<<endl;
- }
- };
- // 主函数
- int main()
- {
- CallOptionPricing t1,t2(1,10,10,15,0.3,0.03),t3(1,5,10,12,0.3,0.03);
- t1.Price();
- t2.Price();
- t3.Price();
- return 0;
- }