楼主: oliviawan
8911 4

[问答] 求助:利用牛顿法和二分法求解B-S期权定价模型里的隐含波动率 [推广有奖]

  • 0关注
  • 0粉丝

高中生

5%

还不是VIP/贵宾

-

威望
0
论坛币
29704 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
54 点
帖子
9
精华
0
在线时间
35 小时
注册时间
2013-9-14
最后登录
2018-4-18

20论坛币
谁能帮帮忙利用牛顿法和二分法编写个在matlab里的M文件,求算隐含波动率。给跪了。S,K,r,T,Option price 已知。

关键词:B-S期权定价模型 期权定价模型 定价模型 期权定价 二分法 matlab 二分法 模型
沙发
danielruc91 在职认证  发表于 2013-10-28 08:30:39 |只看作者 |坛友微信交流群
为啥要自己写呢?matlab自带函数,blsimpv...

使用道具

藤椅
louislau2010 发表于 2014-2-21 10:26:18 |只看作者 |坛友微信交流群
Function ImpliedCallVolatility(UnderlyingPrice, ExercisePrice, Time, Interest, Target, Dividend)
High = 5
Low = 0
Do While (High - Low) > 0.0001
If CallOption(UnderlyingPrice, ExercisePrice, Time, Interest, (High + Low) / 2, Dividend) > Target Then
High = (High + Low) / 2
Else: Low = (High + Low) / 2
End If
Loop
ImpliedCallVolatility = (High + Low) / 2
End Function
Function ImpliedPutVolatility(UnderlyingPrice, ExercisePrice, Time, Interest, Target, Dividend)
High = 5
Low = 0
Do While (High - Low) > 0.0001
If PutOption(UnderlyingPrice, ExercisePrice, Time, Interest, (High + Low) / 2, Dividend) > Target Then
High = (High + Low) / 2
Else: Low = (High + Low) / 2
End If
Loop
ImpliedPutVolatility = (High + Low) / 2
End Function
看涨期权,看跌期权你自己根据公式写函数就可,这个用的是牛顿迭代法

使用道具

板凳
Afgr4289ch 学生认证  发表于 2017-12-25 13:59:35 |只看作者 |坛友微信交流群
function volatility = blsimpv(S, X, r, T, value, varargin)
%BLSIMPV Black-Scholes implied volatility.
%   Compute the implied volatility of an underlying asset from the market
%   value of European call and put options using a Black-Scholes model.
%
%   Volatility = blsimpv(Price, Strike, Rate, Time, Value)
%   Volatility = blsimpv(Price, Strike, Rate, Time, Value, Limit, ...
%     Yield, Tolerance, Class)
%
% Optional Inputs: Limit, Yield, Tolerance, Class.
%
% Inputs:
%   Price - Current price of the underlying asset.
%
%   Strike - Strike (i.e., exercise) price of the option.
%
%   Rate - Annualized continuously compounded risk-free rate of return over
%     the life of the option, expressed as a positive decimal number.
%
%   Time - Time to expiration of the option, expressed in years.
%
%   Value - Price (i.e., value) of a European option from which the implied
%     volatility of the underlying asset is derived.
%
% Optional Inputs:
%   Limit - Positive scalar representing the upper bound of the implied
%     volatility search interval. If empty or missing, the default is 10,
%     or 1000% per annum.
%
%   Yield - Annualized continuously compounded yield of the underlying asset
%     over the life of the option, expressed as a decimal number. For example,
%     this could represent the dividend yield and foreign risk-free interest
%     rate for options written on stock indices and currencies, respectively.
%     If empty or missing, the default is zero.
%
%   Tolerance - Positive scalar implied volatility termination tolerance.
%     If empty or missing, the default is 1e-6.
%
%   Class - Option class (i.e., whether a call or put) indicating the
%     option type from which the implied volatility is derived. This may
%     be either a logical indicator or a cell array of characters. To
%     specify call options, set Class = true or Class = {'call'}; to specify
%     put options, set Class = false or Class = {'put'}. If empty or missing,
%     the default is a call option.
%
% Output:
%   Volatility - Implied volatility of the underlying asset derived from
%     European option prices, expressed as a decimal number. If no solution
%     can be found, a NaN (i.e., Not-a-Number) is returned.
%
% Example:
%   Consider a European call option trading at $10 with an exercise price
%   of $95 and 3 months until expiration. Assume the underlying stock pays
%   no dividends, is trading at $100, and the risk-free rate is 7.5% per
%   annum. Furthermore, assume we are interested in implied volatilities
%   no greater than 0.5 (i.e., 50% per annum). Under these conditions, any
%   of the following commands
%
%   Volatility = blsimpv(100, 95, 0.075, 0.25, 10, 0.5)
%   Volatility = blsimpv(100, 95, 0.075, 0.25, 10, 0.5, 0, [], {'Call'})
%   Volatility = blsimpv(100, 95, 0.075, 0.25, 10, 0.5, 0, [], true)
%
%   return an implied volatility of 0.3130, or 31.30%, per annum.
%
% Notes:
% (1) The input arguments Price, Strike, Rate, Time, Value, Yield, and
%     Class may be scalars, vectors, or matrices. If scalars, then that
%     value is used to compute the implied volatility from all options. If
%     more than one of these inputs is a vector or matrix, then the
%     dimensions of all non-scalar inputs must be the same.
% (2) Ensure that Rate, Time, and Yield are expressed in consistent units
%     of time.
%
% See also BLSPRICE, BLSDELTA, BLSGAMMA, BLSLAMBDA, BLSTHETA, BLSRHO.

% Copyright 1995-2010 The MathWorks, Inc.

%
% References:
%   Hull, J.C., "Options, Futures, and Other Derivatives", Prentice Hall,
%     5th edition, 2003.
%   Luenberger, D.G., "Investment Science", Oxford Press, 1998.
%

%
% Input argument checking & default assignment.
%

if nargin < 5
   error(message('finance:blsimpv:TooFewInputs'))
end

if nargin > 9
   error(message('finance:blsimpv:TooManyInputs'))
end


if any(value(:) < 0)
   error(message('finance:blsimpv:NegativeValue'))
end

if (nargin < 6) || isempty(varargin{1})
   limit = 10;
else
   if varargin{1}(1) <= 0
      error(message('finance:blsimpv:NonPositiveVolatility'))
   end
   limit = varargin{1}(1);
end

if (nargin < 7) || isempty(varargin{2})
   q = zeros(size(S));
else
   q = varargin{2};
end

blscheck(S, X, r, T, [], q);

if (nargin < 8) || isempty(varargin{3})
    tol = 1e-6;
else
    if varargin{3}(1) <= 0
       error(message('finance:blsimpv:NonPositiveTolerance'))
    end
    tol = varargin{3}(1);
end

if (nargin < 9) || isempty(varargin{4})
   optionClass = true(size(S));  % Denote TRUE = 1 ==> Call Options
else
%
%  Allow some flexibility for the option class input specification (i.e.,
%  whether the implied volatility is derived from the Black-Scholes
%  pricing formula of call options or put options). For backward
%  compatibility, the default still derives the implied volatility of
%  the underlyer from the prices of CALL options.
%
%  However, the following code allows users to indicate the option class
%  by any of the following:
%
%     (1) Logical values: 0 = Put, 1 = Call.
%     (2) Cell arrays of character strings, in which a case-insensitive
%         comparison is made of the first character of each element of the
%         cell array with 'P' and 'C' for puts and calls, respectively.
%     (3) Numeric values: 0 = Put, anything else = Call.
%
%  The preferred methods are (1) and (2).
%
   switch class(varargin{4})
     case {'double' , 'logical' , 'single'}
          optionClass = logical(varargin{4});
     case 'cell'
          optionClass = ~strncmpi(varargin{4}, 'P', 1);
     otherwise
          error(message('finance:blsimpv:InvalidOptionClass'))
   end
end

%
% Perform scalar expansion & guarantee conforming arrays.
%

try
   [S, X, r, T, value, q, optionClass] = finargsz('scalar', S, X, r, T, value, q, optionClass);
catch
   error(message('finance:blsimpv:InconsistentDimensions'))
end

%
% Record array dimensions for output argument formatting.
%

[nRows, nCols] = size(S);

volatility = nan(nRows * nCols, 1);

%
% Convert to column vectors for intermediate processing.
%

[S, X, r, T, value, q, optionClass] = deal(S(:),     X(:), r(:), ...
                                                  T(:), value(:), q(:), optionClass(:));

%
% Now estimate the implied volatility for each option.
%

options = optimset('fzero');
options = optimset(options, 'TolX', tol(1), 'Display', 'off');

for i = 1:length(volatility)
%
% Compute the implied volatility from option prices ONLY if the
% price of the underlying asset AND the option strike price AND
% the time to expiry of the option are greater than zero.
%
% Otherwise, return the implied volatility as a NaN, indicating
% that there is insufficient information to determine a unique
% solution. Note that these boundary conditions do NOT mean that
% the true volatility of the underlying asset is non-existent,
% only that we cannot determine a unique solution solely from
% option prices.
%
    if (S(i) > 0) && (X(i) > 0) && (T(i) > 0)

       try
         [volatility(i), ~, exitFlag] = fzero(@objfcn, [0 limit], options, ...
                   S(i), X(i), r(i), T(i), value(i), q(i), optionClass(i));

         if exitFlag < 0
            volatility(i) = NaN;
         end

       catch
         volatility(i) = NaN;
       end

    end

end

%
% Reshape the outputs for the user.
%

volatility = reshape(volatility, nRows, nCols);

%
% * * * * * * * Implied Volatility Objective Function * * * * * * *
%

function delta = objfcn(volatility, S, X, r, T, value, q, optionClass)
%OBJFCN Implied volatility objective function.
% The objective function is simply the difference between the specified
% market value, or price, of the option and the theoretical value derived
% from the Black-Scholes model.
%

[callValue, putValue] = blsprice(S, X, r, T, volatility, q);

if optionClass
   delta = value - callValue;
else
   delta = value - putValue;
end
MATLAB里面的函数blsimpv。。

使用道具

报纸
开朗的波 发表于 2020-2-13 22:39:49 |只看作者 |坛友微信交流群
louislau2010 发表于 2014-2-21 10:26
Function ImpliedCallVolatility(UnderlyingPrice, ExercisePrice, Time, Interest, Target, Dividend)
Hi ...
你好,请问,函数里的参数 Target, Dividend指什么?


使用道具

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

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

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

GMT+8, 2024-9-20 21:54