楼主: WenshanQi07
12468 37

[学科前沿] 【求助】欧式期权定价之隐式有限差分法-为什么matlab做出的图那么诡异呀? [推广有奖]

11
xuruilong100 发表于 2012-7-30 10:18:48 |只看作者 |坛友微信交流群
别忘了加好评哦,亲

使用道具

12
WenshanQi07 发表于 2012-7-30 17:29:34 |只看作者 |坛友微信交流群
xuruilong100 发表于 2012-7-30 10:10
sptprice = zeros(100,1);
optprice = zeros(100,1);
for(i = 1:100)
太感谢了!!!
按你说的 我把上届改成300 图像果然变正常了
但是另外5张本来就正常的图 我当时写的上届就是100 是不是我也需要把这些的上届都改成300才好呢?

使用道具

13
WenshanQi07 发表于 2012-7-30 18:11:32 |只看作者 |坛友微信交流群
xuruilong100 发表于 2012-7-30 10:10
sptprice = zeros(100,1);
optprice = zeros(100,1);
for(i = 1:100)
我试着把6张图的上届都定为300,发现这回显式看涨和显式看跌的图像都出不来,但隐式看涨、隐式看跌、CN看涨、CN看跌的图像都出来了,而且这4张图中,看跌的2张图和上届定为100的几乎没区别,看涨的2张图的option price的最高点由原先的不到50变成了超过50.
为什么这次显式的会出问题呢?
是不是我不需要把所有的上届都改为300,只需把隐式改为300,显式和CN仍然定为100呢?

显式的程序   如下
function oPrice = finDiffExplicit(X,S0,r,sig,Svec,tvec,oType)
% Function to calculate the price of a vanilla European
% Put or Call option using the explicit finite difference method
%
% oPrice = finDiffExplicit(X,r,sig,Svec,tvec,oType)
%
% Inputs: X - strike
%       : S0 - stock price
%       : r - risk free interest rate
%       : sig - volatility
%       : Svec - Vector of stock prices (i.e. grid points)
%       : tvec - Vector of times (i.e. grid points)
%       : oType - must be 'PUT' or 'CALL'.
%
% Output: oPrice - the option price
%
% Notes: This code focuses on details of the implementation of the
%        explicit finite difference scheme.
%        It does not contain any programatic essentials such as error
%        checking.
%        It does not allow for optional/default input arguments.
%        It is not optimized for memory efficiency, speed or
%        use of sparse matrces.

% Author: Phil Goddard (phil@goddardconsulting.ca)
% Date  : Q4, 2007

% Get the number of grid points
M = length(Svec)-1;
N = length(tvec)-1;
% Get the grid sizes (assuming equi-spaced points)
dt = tvec(2)-tvec(1);

% Calculate the coefficients
% To do this we need a vector of j points
j = 1:M-1;
sig2 = sig*sig;
j2 = j.*j;
aj = 0.5*dt*(sig2*j2-r*j);
bj = 1-dt*(sig2*j2+r);
cj = 0.5*dt*(sig2*j2+r*j);

% Pre-allocate the output
price(1:M+1,1:N+1) = nan;

% Specify the boundary conditions
switch oType
    case 'CALL'
        % Specify the expiry time boundary condition
        price(:,end) = max(Svec-X,0);
        % Put in the minimum and maximum price boundary conditions
        % assuming that the largest value in the Svec is
        % chosen so that the following is true for all time
        price(1,:) = 0;
        price(end,:) = (Svec(end)-X)*exp(-r*tvec(end:-1:1));
    case 'PUT'
        % Specify the expiry time boundary condition
        price(:,end) = max(X-Svec,0);
        % Put in the minimum and maximum price boundary conditions
        % assuming that the largest value in the Svec is
        % chosen so that the following is true for all time
        price(1,:) = (X-Svec(end))*exp(-r*tvec(end:-1:1));
        price(end,:) = 0;
end

% Form the tridiagonal matrix
A = diag(bj);  % Diagonal terms
A(2:M:end) = aj(2:end); % terms below the diagonal
A(M:M:end) = cj(1:end-1); % terms above the diagonal

% Calculate the price at all interior nodes
offsetConstants = [aj(1); cj(end)];
for i = N:-1:1
    price(2:end-1,i) = A*price(2:end-1,i+1);
    % Offset the first and last terms
    price([2 end-1],i) = price([2 end-1],i) + ...
        offsetConstants.*price([1 end],i+1);
end

% Calculate the option price
oPrice = interp1(Svec,price(:,1),S0);

画图程序  如下
>> sptprice = zeros(100,1);
optprice = zeros(100,1);
for(i = 1:100)
     sptprice(i,1) = i;
     optprice(i,1) = finDiffExplicit(50,sptprice(i,1),0.05,0.3,0:1:300,0:0.001:1,'CALL');
end
plot(sptprice,optprice,'-')
grid on

>> sptprice = zeros(100,1);
optprice = zeros(100,1);
for(i = 1:100)
     sptprice(i,1) = i;
     optprice(i,1) = finDiffExplicit(50,sptprice(i,1),0.05,0.3,0:1:300,0:0.001:1,'PUT');
end
plot(sptprice,optprice,'-')
grid on

使用道具

14
WenshanQi07 发表于 2012-7-30 18:36:13 |只看作者 |坛友微信交流群
Chemist_MZ 发表于 2012-7-30 09:24
帮你修改好了,这回应该没问题了。你那个程序有问题,写得乱七八糟的。或者直接粘来的可能作者没有把输入 ...
真的非常感谢!!!
主要是我实在没本事自己写程序啊 所以只好粘现成的了

使用道具

15
WenshanQi07 发表于 2012-7-30 18:41:42 |只看作者 |坛友微信交流群
xuruilong100 发表于 2012-7-30 10:18
别忘了加好评哦,亲
恩 必须的亲

使用道具

16
xuruilong100 发表于 2012-7-30 21:13:36 |只看作者 |坛友微信交流群
WenshanQi07 发表于 2012-7-30 17:29
太感谢了!!!
按你说的 我把上届改成300 图像果然变正常了
但是另外5张本来就正常的图 我当时写的上届 ...
理论上是这样的

使用道具

17
xuruilong100 发表于 2012-7-30 21:28:57 |只看作者 |坛友微信交流群
WenshanQi07 发表于 2012-7-30 18:11
我试着把6张图的上届都定为300,发现这回显式看涨和显式看跌的图像都出不来,但隐式看涨、隐式看跌、CN看 ...
数值计算的内部运作本来就很微妙,我一时也想不起来原因。
出结果也许有巧合的成分。
“显式差分格式”不稳定,不一定收敛到真实解,不一定算出结果。

做期权计算,建议使用稳定的方法,比如“隐式差分”。如果有文献
说明了一种方法不稳定,最好不要用。

使用道具

18
WenshanQi07 发表于 2012-7-30 23:15:19 |只看作者 |坛友微信交流群
xuruilong100 发表于 2012-7-30 21:28
数值计算的内部运作本来就很微妙,我一时也想不起来原因。
出结果也许有巧合的成分。
“显式差分格式” ...
谢谢
那我还是把隐式和CN的上届都定位300,然后显示就还是100吧,至少这样图像都是正常的

使用道具

19
WenshanQi07 发表于 2012-7-30 23:23:19 |只看作者 |坛友微信交流群
xuruilong100 发表于 2012-7-30 10:10
sptprice = zeros(100,1);
optprice = zeros(100,1);
for(i = 1:100)
再弱弱的问一句, 此时 S_max=300,那M和N分别等于多少呢?

使用道具

20
Chemist_MZ 在职认证  发表于 2012-7-30 23:26:09 |只看作者 |坛友微信交流群
WenshanQi07 发表于 2012-7-30 23:23
再弱弱的问一句, 此时 S_max=300,那M和N分别等于多少呢?
和上界大小无关,只取决于你划分的格子的密度。这个你自己设定。N是时间维度的密度,跟股票就更加没有关系了。再说一遍,这个程序其实最要命的地方是他最后一部分写得有问题,画图的问题倒是其次,我用我自己的方法画出来也跟你差不多。所以你不用拼命去改S的上界,我帮你改的股价是10,上界是15都没有问题。楼主一直无视我的话,我很怀疑你有没有运行过我给你的东西。隐式方法是不可能画出不稳定的诡异图像的,所以不是画图的问题,是程序本来就写错了。
扫头像关注公众号“二点三西格玛”衍生品定价与风险管理

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jr
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 05:12