フィルターのクリア

Integral3 with Error using - Matrix dimensions must agree.

2 ビュー (過去 30 日間)
Shu Yeung Wong
Shu Yeung Wong 2017 年 7 月 24 日
コメント済み: Shu Yeung Wong 2017 年 7 月 28 日
I tried to use the numerical triple integral to price the basket option, however this error message keeps popping up and I cannot locate the misused "-"
S1_0=100; S2_0=85; S3_0=75;
T = 2;
a = [1/3 1/3 1/3].';
K = 85;
r = ones(3,1)*0.05; % interest rate
sigma = [0.2 0.15 0.28]';
sigma_sqr = (sigma.^2);
sigma_cov = kron(sigma.',sigma);
rho = [1 -0.2 -0.3; -0.2 1 0.25; -0.3 0.25 1];
sigma_cov = sigma_cov.* rho;
sigma_cov_T= sigma_cov*T;
sigma_cov_T_inv = (sigma_cov*T)^(-1);
sqrt_det_2pi_Sigma_T= (det(2*pi*sigma_cov_T))^(1/2);
payoff = @ (x1, x2, x3) max(a(1)*S1_0*exp(x1) + a(2)*S2_0*exp(x2) + a(3)*S3_0*exp(x3) - K, 0);
normal_density_3d =@(x1,x2,x3) 1/sqrt_det_2pi_Sigma_T*...
exp((-1/2).*(([x1 ;x2; x3]-((r-(1/2)*sigma_sqr)*T))'...
* sigma_cov_T_inv...
*([x1 ;x2; x3]-(r-(1/2)*sigma_sqr)*T)));
integrand = @(x1, x2, x3) payoff(x1, x2, x3) .* ...
normal_density_3d(x1,x2,x3);
price = exp(-r*T)*integral3(integrand,-3,3,-3,3,-3,3);
Error using -
Matrix dimensions must agree.
Error in
@(x1,x2,x3)1/sqrt_det_2pi_Sigma_T*exp((-1/2).*(([x1;x2;x3]-((r-(1/2)*sigma_sqr)*T))'*sigma_cov_T_inv*([x1;x2;x3]-(r-(1/2)*sigma_sqr)*T)))
Error in @(x1,x2,x3)payoff(x1,x2,x3).*normal_density_3d(x1,x2,x3)

採用された回答

Kristen Amaddio
Kristen Amaddio 2017 年 7 月 27 日
The error is coming from the following part of the code within the 'normal_density_3d' anonymous function:
[x1 ;x2; x3]-((r-(1/2)*sigma_sqr)*T)
The issue here is that there is a dimensions mismatch between the left-hand side and the right-hand side of the minus sign (-). 'x1', 'x2', and 'x3' are all 14x14 in dimension, so '[x1; x2; x3]' is 42x14 in dimension. On the other side, '((r-(1/2)*sigma_sqr)*T)' is 1x3 in dimension. These dimensions are not compatible for subtraction with each other.
Setting breakpoints and debugging your code can help you identify some of these issues. The following post shows you how you can debug anonymous functions:
  1 件のコメント
Shu Yeung Wong
Shu Yeung Wong 2017 年 7 月 28 日
Thank you very much. I did not know the dimension of x1 x2 and x3.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by