1## Copyright (C) 2016 Parsiad Azimzadeh <parsiad.azimzadeh@gmail.com> 2## 3## This program is free software; you can redistribute it and/or modify it under 4## the terms of the GNU Lesser General Public License as published by the Free 5## Software Foundation; either version 3 of the License, or (at your option) any 6## later version. 7## 8## This program is distributed in the hope that it will be useful, but WITHOUT 9## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10## FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11## for more details. 12## 13## You should have received a copy of the GNU Lesser General Public License 14## along with this program; if not, see <http://www.gnu.org/licenses/>. 15 16## -*- texinfo -*- 17## @deftypefn {Function File} {@var{heston} =} heston (@var{Return}, @var{Speed}, @var{Level}, @var{Volatility}) 18## @deftypefnx {Function File} {@var{heston} =} heston (@var{Return}, @var{Speed}, @var{Level}, @var{Volatility}, @var{OptionName}, @var{OptionValue}, @dots{}) 19## Creates an object to represent a Heston stochastic volatility model. 20## 21## @center dX_1 = (@var{Return}(t) * X_1)dt + (sqrt (X_2) * X_1)dW_1; 22## @center dX_2 = (@var{Speed}(t) * (@var{Level}(t) - X_2))dt + (sqrt (X_2) * @var{Volatility}(t))dW_2. 23## 24## See the @@sde documentation for a list of optional arguments. 25## 26## @seealso{sde} 27## @end deftypefn 28 29function heston = heston (Return, Speed, Level, Volatility, varargin) 30 31 if (nargin < 4) 32 print_usage (); 33 endif 34 35 if (isscalar (Return) && isreal (Return)) 36 ReturnFunction = @(t, X) Return; 37 elseif (isa (Return, "function_handle") && nargin (Return) == 1) 38 ReturnFunction = @(t, X) Return (t); 39 elseif (isa (Return, "function_handle") && nargin (Return) == 2) 40 ReturnFunction = Return; 41 else 42 error ("heston: RETURN must either be a real scalar or a function of one or two arguments returning such a scalar"); 43 endif 44 45 if (isscalar (Speed) && isreal (Speed)) 46 SpeedFunction = @(t, X) Speed; 47 elseif (isa (Speed, "function_handle") && nargin (Speed) == 1) 48 SpeedFunction = @(t, X) Speed (t); 49 elseif (isa (Speed, "function_handle") && nargin (Speed) == 2) 50 SpeedFunction = Speed; 51 else 52 error ("heston: SPEED must either be a real scalar or a function of one or two arguments returning such a scalar"); 53 endif 54 55 if (isscalar (Level) && isreal (Level)) 56 LevelFunction = @(t, X) Level; 57 elseif (isa (Level, "function_handle") && nargin (Level) == 1) 58 LevelFunction = @(t, X) Level (t); 59 elseif (isa (Level, "function_handle") && nargin (Level) == 2) 60 LevelFunction = Level; 61 else 62 error ("heston: LEVEL must either be a real scalar or a function of one or two arguments returning such a scalar"); 63 endif 64 65 if (isscalar (Volatility) && isreal (Volatility)) 66 VolatilityFunction = @(t, X) Volatility; 67 elseif (isa (Volatility, "function_handle") && nargin (Volatility) == 1) 68 VolatilityFunction = @(t, X) Volatility (t); 69 elseif (isa (Volatility, "function_handle") && nargin (Volatility) == 2) 70 VolatilityFunction = Volatility; 71 else 72 error ("heston: VOLATILITY must either be a real scalar or a function of one or two arguments returning such a scalar"); 73 endif 74 75 ## TODO: Optimize 76 Drift = @(t, X) [ReturnFunction(t, X) * X(1); SpeedFunction(t, X) * (LevelFunction(t, X) - X(2))]; 77 Diffusion = @(t, X) [sqrt(X(2)) * X(1) 0.;0. VolatilityFunction(t, X) * sqrt(X(2))]; 78 79 heston = sde (Drift, Diffusion, varargin{:}); 80 81endfunction 82