# include <math.h>
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include "black_scholes.h"
/******************************************************************************/
double *asset_path ( double s0, double mu, double sigma, double t1, int n,
int *seed )
/******************************************************************************/
/*
Purpose:
ASSET_PATH simulates the behavior of an asset price over time.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
18 February 2012
Author:
Original MATLAB version by Desmond Higham.
C version by John Burkardt.
Reference:
Desmond Higham,
Black-Scholes for Scientific Computing Students,
Computing in Science and Engineering,
November/December 2004, Volume 6, Number 6, pages 72-79.
Parameters:
Input, double S0, the asset price at time 0.
Input, double MU, the expected growth rate.
Input, double SIGMA, the volatility of the asset.
Input, double T1, the expiry date.
Input, integer N, the number of steps to take between 0 and T1.
Input/output, int *SEED, a seed for the random number generator.
Output, double ASSET_PATH[N+1], the option values from time 0 to T1
in equal steps.
*/
{
double dt;
int i;
double p;
double *r;
double *s;
dt = t1 / ( double ) ( n );
r = r8vec_normal_01_new ( n, seed );
s = ( double * ) malloc ( ( n + 1 ) * sizeof ( double ) );
s[0] = s0;
p = s0;
for ( i = 1; i <= n; i++ )
{
p = p * exp ( ( mu - sigma * sigma ) * dt + sigma * sqrt ( dt ) * r[i-1] );
s[i] = p;
}
free ( r );
return s;
}
/******************************************************************************/
double binomial ( double s0, double e, double r, double sigma, double t1,
int m )
/******************************************************************************/
/*
Purpose:
BINOMIAL uses the binomial method for a European call.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
18 February 2012
Author:
Original MATLAB version by Desmond Higham.
C version by John Burkardt.
Reference:
Desmond Higham,
Black-Scholes for Scientific Computing Students,
Computing in Science and Engineering,
November/December 2004, Volume 6, Number 6, pages 72-79.
Parameters:
Input, double S0, the asset price at time 0.
Input, double E, the exercise price.
Input, double R, the interest rate.
Input, double SIGMA, the volatility of the asset.
Input, double T1, the expiry date.
Input, int M, the number of steps to take
between 0 and T1.
Output, double BIONOMIAL, the option value at time 0.
*/
{
double a;
double c;
double d;
double dt;
int i;
int n;
double p;
double u;
double *w;
/*
Time stepsize.
*/
dt = t1 / ( double ) ( m );
a = 0.5 * ( exp ( - r * dt ) + exp ( ( r + sigma * sigma ) * dt ) );
d = a - sqrt ( a * a - 1.0 );
u = a + sqrt ( a * a - 1.0 );
p = ( exp ( r * dt ) - d ) / ( u - d );
w = ( double * ) malloc ( ( m + 1 ) * sizeof ( double ) );
for ( i = 0; i <= m; i++ )
{
w[i] = fmax ( s0 * pow ( d, m - i ) * pow ( u, i ) - e, 0.0 );
}
/*
Trace backwards to get the option value at time 0.
*/
for ( n = m - 1; 0 <= n; n-- )
{
for ( i = 0; i <= n; i++ )
{
w[i] = ( 1.0 - p ) * w[i] + p * w[i+1];
}
}
for ( i = 0; i < m + 1; i++ )
{
w[i] = exp ( - r * t1 ) * w[i];
}
c = w[0];
free ( w );
return c;
}
/******************************************************************************/
double bsf ( double s0, double t0, double e, double r, double sigma, double t1 )
/******************************************************************************/
/*
Purpose:
BSF evaluates the Black-Scholes formula for a European call.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
18 February 2012
Author:
Original MATLAB version by Desmond Higham.
C version by John Burkardt.
Reference:
Desmond Higham,
Black-Scholes for Scientific Computing Students,
Computing in Science and Engineering,
November/December 2004, Volume 6, Number 6, pages 72-79.
Parameters:
Input, double S0, the asset price at time T0.
Input, double T0, the time at which the asset price is known.
Input, double E, the exercise price.
Input, double R, the interest rate.
Input, double SIGMA, the volatility of the asset.
Input, double T1, the expiry date.
Output, double BSF, the value of the call option.
*/
{
double c;
double d1;
double d2;
double n1;
double n2;
double tau;
tau = t1 - t0;
if ( 0.0 < tau )
{
d1 = ( log ( s0 / e ) + ( r + 0.5 * sigma * sigma ) * tau )
/ ( sigma * sqrt ( tau ) );
d2 = d1 - sigma * sqrt ( tau );
n1 = 0.5 * ( 1.0 + erf ( d1 / sqrt ( 2.0 ) ) );
n2 = 0.5 * ( 1.0 + erf ( d2 / sqrt ( 2.0 ) ) );
c = s0 * n1 - e * exp ( - r * tau ) * n2;
}
else
{
c = fmax ( s0 - e, 0.0 );
}
return c;
}
/******************************************************************************/
double *forward ( double e, double r, double sigma, double t1, int nx,
int nt, double smax )
/******************************************************************************/
/*
Purpose:
FORWARD uses the forward difference method to value a European call option.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
18 February 2012
Author:
Original MATLAB version by Desmond Higham.
C version by John Burkardt.
Reference:
Desmond Higham,
Black-Scholes for Scientific Computing Students,
Computing in Science and Engineering,
November/December 2004, Volume 6, Number 6, pages 72-79.
Parameters:
Input, double E, the exercise price.
Input, double R, the interest rate.
Input, double SIGMA, the volatility of the asset.
Input, double T1, the expiry date.
Input, int NX, the number of "space" steps used to
divide the interval [0,L].
Input, int NT, the number of time steps.
Input, double SMAX, the maximum value of S to consider.
Output, double U[(NX-1)*(NT+1)], the value of the European
call option.
*/
{
double *a;
double *b;
double *c;
double dt;
double dx;
int i;
int j;
double p;
double t;
double *u;
double u0;
dt = t1 / ( double ) ( nt );
dx = smax / ( double ) ( nx );
a = ( double * ) malloc ( ( nx - 1 ) * sizeof ( double ) );
b = ( double * ) malloc ( ( nx - 1 ) * sizeof ( double ) );
c = ( double * ) malloc ( ( nx - 1 ) * sizeof ( double ) );
for ( i = 0; i < nx - 1; i++ )
{
b[i] = 1.0 - r * dt - dt * pow ( sigma * ( i + 1 ), 2 );
}
for ( i = 0; i < nx - 2; i++ )
{
c[i] = 0.5 * dt * pow ( sigma * ( i + 1 ), 2 ) + 0.5 * dt * r * ( i + 1 );
}
for ( i = 1; i < nx - 1; i++ )
{
a[i] = 0.5 * dt * pow ( sigma * ( i + 1 ), 2 ) - 0.5 * dt * r * ( i + 1 );
}
u = ( double * ) malloc ( ( nx - 1 ) * ( nt + 1 ) * sizeof ( double ) );
u0 = 0.0;
for ( i = 0; i < nx - 1; i++ )
{
u0 = u0 + dx;
u[i+0*(nx-1)] = fmax ( u0 - e, 0.0 );
}
for ( j = 0; j < nt; j++ )
{
t = ( double ) ( j ) * t1 / ( double ) ( nt );
p = 0.5 * dt * ( nx - 1 ) * ( sigma * sigma * ( nx - 1 ) + r )
* ( smax - e * exp ( - r * t ) );
for ( i = 0; i < nx - 1; i++ )
{
u[i+(j+1)*(nx-1)] = b[i] * u[i+j*(nx-1)];
}
for ( i = 0; i < nx - 2; i++ )
{
u[i+(j+1)*(nx-1)] = u[i+(j+1)*(nx-1)] + c[i] * u[i+1+j*(nx-1)];
}
for ( i = 1; i < nx - 1; i++ )
{
u[i+(j+1)*(nx-1)] = u[i+(j+1)*(nx-1)] + a[i] * u[i-1+j*(nx-1)];
}
u[nx-2+(j+1)*(nx-1)] = u[nx-2+(j+1)*(nx-1)] + p;
}
free ( a );
free ( b );
free ( c );
return u;
}
/******************************************************************************/
double *mc ( double s0,
没有合适的资源?快使用搜索试试~ 我知道了~
C 代码 实现一些简单的方法来 布莱克-斯科尔斯期权估值理论.rar
共3个文件
h:1个
sh:1个
c:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 133 浏览量
2022-11-12
20:02:14
上传
评论
收藏 6KB RAR 举报
温馨提示
c++源代码,c源代码,测试可以
资源推荐
资源详情
资源评论
收起资源包目录
C 代码 实现一些简单的方法来 布莱克-斯科尔斯期权估值理论.rar (3个子文件)
black_scholes
black_scholes.sh 232B
black_scholes.c 20KB
black_scholes.h 705B
共 3 条
- 1
资源评论
卷积神经网络
- 粉丝: 342
- 资源: 8460
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功