Integral3 with Error using - Matrix dimensions must agree.

2 views (last 30 days)
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)

Accepted Answer

Kristen Amaddio
Kristen Amaddio on 27 Jul 2017
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:

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!