Error: Index in position 2 exceeds array bounds (must not exceed 730)
11 ビュー (過去 30 日間)
古いコメントを表示
Hi all, Im working on a code that simulates a stock price over two years and then compares it to a strike price (call options). I want to simulate the stock price 3000 times for each starting value (30 to 150), then take the average payout (for that particular staring price) and plot that vs the starting price. The idea is that if the final price is higher than the strike price (starting price) you get a payout equal to the difference between the final and strike price, if not you get 0.
I keep getting an error (Error: Index in position 2 exceeds array bounds (must not exceed 730)) It gives the error in the line
S(k+1, n) = S(k, n) + S(k, n).*r.*dt + sqrt(dt).*sigma.*S(k, n).*eta(k, n);
Here is my code
r = 0.05;
T = 2;
dt = 1/365;
sigma = sqrt(r);
rng('shuffle')
eta = randn(1, 365*T);
n_iterations = 3000;
S = zeros(365*T, n_iterations);
for S_0 = 30 : 150
S(1, :) = S_0 ;
n = 1 : n_iterations;
for k = 1 : 365*T
S(k+1, n) = S(k, n) + S(k, n).*r.*dt + sqrt(dt).*sigma.*S(k, n).*eta(k, n);
end
strike = S(1, :);
price = S((365*T + 1), :);
eurocallreturn(strike, price);
avereturn = mean(eurocallreturn);
hold on
plot(strike, avereturn)
xlabel('Price')
ylabel('Return')
end
plot([50:130], exp(0.05*2)*blsprice([50:130], 90, 0.05, 2, sqrt(0.05)))
function R = eurocallreturn(strike, price)
if price > strike
R = price - strike;
else if price <= strike
R = 0;
end
end
end
%What am I doing wrong? And how do I get matlab to
% store all the outputs for avereturn (for each starting value) in a vector?
%Thanks in advance
0 件のコメント
採用された回答
Walter Roberson
2019 年 9 月 24 日
編集済み: Walter Roberson
2019 年 9 月 24 日
T = 2;
eta = randn(1, 365*T);
365*2 = 730, so eta is 1 x 730
n_iterations = 3000;
n = 1 : n_iterations;
so n = 1 : 3000
for k = 1 : 365*T
S(k+1, n) = S(k, n) + S(k, n).*r.*dt + sqrt(dt).*sigma.*S(k, n).*eta(k, n);
end
eta(k,n) with k = 1 : 365*2 could try to use up to 730 as the first dimension of eta, but eta is only a single row and you never make eta have more rows.
eta(k,n) with n = 1 : 3000 will try to access up to column 3000 in eta, but eta only has 730 columns.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!