Nested integrals spends quiet different time to calculate for different input values
I’m trying to calculate the cumulative distribution of the functional relationship between several random variables and a specific value’s difference (or margin). The mathematical expression should be ∫pdf (x) * pdf (y)* φ (x, y) dxdy. φ The m function calculated for margin.
Assume there are two normal distributions. X follows a normal distribution N(100,10^2), and Y follows a normal distribution N(200, 20^2). I need to calculate the probability of [X+Y ≤ 3/input], where input is a constant variable.
The code I wrote is:
P= integral(@(x) arrayfun(@(x) 1/(2*pi)^0.5/sigma1*exp(-(x-mu1)^2/2/sigma1^2)* integral(@(y) arrayfun(@(y) 1/(2*pi)^0.5/sigma2*exp(-(y-mu2)^2/2/sigma2^2)* margin(x, y, input),y),-inf,+inf),x),-inf,+inf);
function [m] = margin(x, y, input)
I = 3/(x+y);
m = I – input;
if m >= 0
m = 1;
else
m = 0;
end
end
I found that on my computer, when the input is 0.01 and 0.011, the calculation time is 1.099 s and 1597 s, with a significant difference.
What is the reason for the significant difference in calculation time? How to reduce integration calculation time?I’m trying to calculate the cumulative distribution of the functional relationship between several random variables and a specific value’s difference (or margin). The mathematical expression should be ∫pdf (x) * pdf (y)* φ (x, y) dxdy. φ The m function calculated for margin.
Assume there are two normal distributions. X follows a normal distribution N(100,10^2), and Y follows a normal distribution N(200, 20^2). I need to calculate the probability of [X+Y ≤ 3/input], where input is a constant variable.
The code I wrote is:
P= integral(@(x) arrayfun(@(x) 1/(2*pi)^0.5/sigma1*exp(-(x-mu1)^2/2/sigma1^2)* integral(@(y) arrayfun(@(y) 1/(2*pi)^0.5/sigma2*exp(-(y-mu2)^2/2/sigma2^2)* margin(x, y, input),y),-inf,+inf),x),-inf,+inf);
function [m] = margin(x, y, input)
I = 3/(x+y);
m = I – input;
if m >= 0
m = 1;
else
m = 0;
end
end
I found that on my computer, when the input is 0.01 and 0.011, the calculation time is 1.099 s and 1597 s, with a significant difference.
What is the reason for the significant difference in calculation time? How to reduce integration calculation time? I’m trying to calculate the cumulative distribution of the functional relationship between several random variables and a specific value’s difference (or margin). The mathematical expression should be ∫pdf (x) * pdf (y)* φ (x, y) dxdy. φ The m function calculated for margin.
Assume there are two normal distributions. X follows a normal distribution N(100,10^2), and Y follows a normal distribution N(200, 20^2). I need to calculate the probability of [X+Y ≤ 3/input], where input is a constant variable.
The code I wrote is:
P= integral(@(x) arrayfun(@(x) 1/(2*pi)^0.5/sigma1*exp(-(x-mu1)^2/2/sigma1^2)* integral(@(y) arrayfun(@(y) 1/(2*pi)^0.5/sigma2*exp(-(y-mu2)^2/2/sigma2^2)* margin(x, y, input),y),-inf,+inf),x),-inf,+inf);
function [m] = margin(x, y, input)
I = 3/(x+y);
m = I – input;
if m >= 0
m = 1;
else
m = 0;
end
end
I found that on my computer, when the input is 0.01 and 0.011, the calculation time is 1.099 s and 1597 s, with a significant difference.
What is the reason for the significant difference in calculation time? How to reduce integration calculation time? nested integration, calculation time MATLAB Answers — New Questions