I have a fairly complex function that I want to do double integration as the following:
r1 = 0.0185;
r2 = 0.0172;
sig1 = 0.2222;
sig2 = 0.1450;
sigF = 0.1013;
rhot = -0.5112;
rho = 0.7908;
T = 2;
muX = (r1 - rhot*sig1*sigF - 0.5*(sig1^2))*T;
muY = (r2 - 0.5*(sig2^2))*T;
sigX = sqrt((sig1^2)*T);
sigY = sqrt((sig2^2)*T);
mu = [muX, muY];
sig = [sigX^2, rho*sigX*sigY; rho*sigX*sigY, sigY^2];
ymin = @(x) log(exp(x)+0.05);
fun = @(x,y) (exp(x) - exp(y))' * reshape(mvnpdf([x(:),y(:)],mu,sig), size(x));
D = integral2(fun, -Inf, Inf, ymin, Inf , 'RelTol',1e-10);
The error says that I have to set ArrayValued to true. The problems are 1. I don't know the size of the numerical data 2. There isn't a parameter to set in integral2. How can I solve this problem?

 채택된 답변

Torsten
Torsten 2017년 12월 1일
편집: Torsten 2017년 12월 1일

0 개 추천

function main
r1 = 0.0185;
r2 = 0.0172;
sig1 = 0.2222;
sig2 = 0.1450;
sigF = 0.1013;
rhot = -0.5112;
rho = 0.7908;
T = 2;
muX = (r1 - rhot*sig1*sigF - 0.5*(sig1^2))*T;
muY = (r2 - 0.5*(sig2^2))*T;
sigX = sqrt((sig1^2)*T);
sigY = sqrt((sig2^2)*T);
mu = [muX, muY];
sig = [sigX^2, rho*sigX*sigY; rho*sigX*sigY, sigY^2];
ymin = @(x) log(exp(x)+0.05);
D = integral2(@(x,y)fun(x,y,mu,sig), -Inf, Inf, ymin, Inf , 'RelTol',1e-10);
function f = fun(x,y,mu,sig)
for i=1:numel(x)
vec = [x(i) y(i)];
f(i) = (exp(x(i)-exp(y(i))*mvnpdf(vec,mu,sig);
end
Best wishes
Torsten.

댓글 수: 2

N/A
N/A 2017년 12월 1일
I want to know a bit more about why my problem exist and how this change solved it.
Torsten
Torsten 2017년 12월 1일
You will need pointwise multiplication instead of vector multiplication in your function "fun". I think Walters's answer shows the difference.
Best wishes
Torsten.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 12월 1일

0 개 추천

The function will be called with two arrays the same size, and must return a value the same size.
Suppose the array size is 3 x 2. Then you have
fun = @(x,y) (exp(x) - exp(y))' * reshape(mvnpdf([x(:),y(:)],mu,sig), size(x));
The (exp(x) - exp(y))' part would be 2 x 3 because of the transpose. The reshape(mvnpdf([x(:),y(:)],mu,sig), size(x)) part is going to be 3 x 2, the same size as the input.
You now have a 2 x 3 matrix multiplied by a 3 x 2. The result is going to be 2 x 2, but the size of result needed is 3 x 2.
In general if the input is m x n, your output is going to be n x n instead of m x n.
Perhaps you need
fun = @(x,y) (exp(x) - exp(y)) .* reshape(mvnpdf([x(:),y(:)],mu,sig), size(x));

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

N/A
2017년 12월 1일

댓글:

N/A
2017년 12월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by