1## Copyright (C) 2013 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{Call}, @var{Put}] =} blsprice (@var{Price}, @var{Strike}, @var{Rate}, @var{Time}, @var{Volatility})
18## @deftypefnx {Function File} {[@var{Call}, @var{Put}] =} blsprice (@var{Price}, @var{Strike}, @var{Rate}, @var{Time}, @var{Volatility}, @var{Yield})
19## Compute European call and put option prices.
20##
21## @itemize
22## @item
23## Variable: @var{Price} The current price of the underlying asset.
24## @item
25## Variable: @var{Strike} The strike price the option is written on.
26## @item
27## Variable: @var{Rate} The risk-free interest rate.
28## @item
29## Variable: @var{Time} The time-to-expiry.
30## @item
31## Variable: @var{Volatility} The volatility of the underlying asset.
32## @item
33## Variable: @var{Yield} (Optional, default = 0) Annualized, continuously
34## compounded rate of dividends of the underlying asset.
35## @end itemize
36##
37## Computes the European call and put option prices using the Black-Scholes
38## model.
39##
40## @seealso{blskprice, blsdelta, blsgamma, blsimpv, blslambda, blsrho, blstheta, blsvega}
41## @end deftypefn
42
43function [Call, Put] = blsprice (Price, Strike, Rate, Time, Volatility, ...
44                                 Yield = 0)
45
46  if (nargin < 5 || nargin > 6)
47    print_usage ();
48  endif
49
50  blscheck ("blsprice", Price, Strike, Rate, Time, Volatility, Yield);
51
52  sigma_sqrtT = Volatility .* sqrt (Time);
53
54  d1 = 1 ./ sigma_sqrtT .* (log (Price ./ Strike) + (Rate - Yield + ...
55       Volatility.^2 / 2) .* Time);
56  d2 = d1 - sigma_sqrtT;
57
58  phi1 = normcdf (d1);
59  phi2 = normcdf (d2);
60  disc = exp (-Rate .* Time);
61  F    = Price .* exp ((Rate - Yield) .* Time);
62
63  Call = disc .* (F .*      phi1  - Strike .*  phi2     );
64  Put  = disc .* (Strike .* (1 - phi2) + F .* (phi1 - 1));
65
66endfunction
67
68## Tests
69%!test
70%! [Call, Put] = blsprice (90:10:110, 100, 0.04, 1, 0.2, 0.01);
71%! assert (Call, [4.4037 9.3197 16.1217], 1e-4)
72%! assert (Put, [11.3781 6.3937 3.2952], 1e-4)
73
74## Test input validation
75%!error blsprice ()
76%!error blsprice (1)
77%!error blsprice (1, 2)
78%!error blsprice (1, 2, 3)
79%!error blsprice (1, 2, 3, 4)
80%!error blsprice ("invalid", "type", "argument", 4, 5)
81%!error blsprice ({1, 2, 3}, [2 7 8], [8 3 1], 4, 10)
82