Vectorization of a function

11 ビュー (過去 30 日間)
Francesco Rossi
Francesco Rossi 2019 年 9 月 30 日
編集済み: the cyclist 2019 年 9 月 30 日
I have dotted all the variable that are defined as a vector, but matlab gives me an error.
Do you see where is my problem?
function [C] = BlackScholesCall(S,K,t,r,sigma)
% Calculates the price of a call option
% INPUT S 1x1 ... Current stock price (underlying)
% K 1x1 ... Strike price
% t 1x1 ... Time to maturity
% r 1x1 ... Risk-free interest rate
% sigma 1x1 ... standard deviation (volatility of the underlying)
% OUTPUT C 1x1 ... The price of a call option
% USAGE BlackScholesCall(S,K,t,r,sigma)
d1=(log(S/K.))+(r+(1/2)*sigma^2)*t)/(sigma*sqrt(t));
d2=d1-sigma*sqrt(t.);
C=(S*normcdf(d1))-(K.*(exp(-r*.t))*normcdf(d2));
end
Code to call the function:
S = 22
K = 20:25
t = 0.1:0.1:0.6
r = 0.02
sigma = 0.25
C = BlackScholesCall(S, K, t, r, sigma)
Thank you very much!
  2 件のコメント
Geoff Hayes
Geoff Hayes 2019 年 9 月 30 日
編集済み: Geoff Hayes 2019 年 9 月 30 日
Francesco - please copy and paste the full error message to this question. In the function header, you have
t 1x1 ... Time to maturity
which implies that t is a scalar...but in your input to this function, you define t as an array
t = 0.1:0.1:0.6
Which should it be - a scalar or an array? Perhaps this is the problem...you are passing in an array but the code is expecting a scalar? Are you the author of BlackScholesCall?
Francesco Rossi
Francesco Rossi 2019 年 9 月 30 日
Error in solution: Line: 12 Column: 13
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
This is the error message I got if I run the function.
Thank you for spotting the error in the description, it should be an array, as defined later.

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2019 年 9 月 30 日
編集済み: the cyclist 2019 年 9 月 30 日
The problem is that you don't seem to have a grasp on how the "." syntax actually works. (Sorry if that sounds harsh!)
For example, you have
log(S/K.)
I'm not sure what you intend there -- just stating that K is a vector? -- but that's just not how it works. You don't just "dot the vectors". Oversimplifying a bit -- you dot the operations not the variables.
I would start by reading this documentation.
Your code is just a bit tricky to fix, because you are also trying to use two vectors of different lengths, and I'm guessing you actually want all combinations of K and t to generate results. So, you actually have two-dimensional input. Is that right?
  2 件のコメント
the cyclist
the cyclist 2019 年 9 月 30 日
編集済み: the cyclist 2019 年 9 月 30 日
Oh, maybe not as tricky as I thought. Are the values of K and t paired, such that each pair of inputs gives one value of C? In that case, this should work:
d1=(log(S./K))+(r+(1/2)*sigma^2)*t./(sigma*sqrt(t));
d2=d1-sigma*sqrt(t);
C=(S*normcdf(d1))-(K.*(exp(-r*t)).*normcdf(d2));
in place of what you had. Notice how I moved some of your dots to be associated with vector operations, not the variables themselves.
the cyclist
the cyclist 2019 年 9 月 30 日
Still easier than I expected if you do not want K and t paired, but there just happened to be the same number of inputs for each.
Due to "implicit expansion", you can send in a row vector for K, and a column vector of t, and get all the combinations. Just change your input to this:
t = (0.1:0.1:0.6)'
(in addition to making the changes I suggested to your code).

サインインしてコメントする。

その他の回答 (1 件)

meghannmarie
meghannmarie 2019 年 9 月 30 日
I think you have some of your dot operators wrong:
d1 = (log(S./K) +(r+(1/2)*sigma^2)*t)/(sigma*sqrt(t));
d2 = d1-sigma*sqrt(t);
C = (S*normcdf(d1)) - (K.*(exp(-1.*t)).*normcdf(d2));

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by