Improve running time of code with integrals
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MGee
am 22 Sep. 2023
Kommentiert: Dyuman Joshi
am 23 Sep. 2023
Hello,
I'm aiming to create the following covariance matrix,
,
where the set of possible combinations is given by
.
This results in different combinations, meaning Σ is of dimension .
The integrand is given by with some parameters .
Below is my created function, which takes as input an array of parameters , the output is the resulting covariance matrix Σ.
Because the covariance matrix is symmetric, I just compute the lower triangular part and add the upper triangular part in the last step.
Computing this takes very long, after 5 minutes I just got the first 200 lines of the matrix. Regarding that all this is just one step in computing a Likelihood-function, which I aim to maximize for numerically, the running time has to be reduced. The problem is obviously the part where I'm integrating in the middle of the code, can anyone give me an advice how to optimize this part?
function Sigma = covariance_matrix(P)
sig = @(t,x) (P(1) + P(2).*exp(P(3).*(x+t))) .* (P(4)+t) .* exp(-P(5).*t);
i = 0;
for x1 = 30:95
for t1 = 0:95-x1
i = i+1;
j = 1;
for x2 = 30:x1
t2 = 0;
while j <= i && t2 <= 95-x2
Sigma(i,j) = integral(@(s) integral(@(u) sig(u,x1-1+s), ...
t1+1-s,t1+2-s).*integral(@(u) sig(u,x2-1+s), ...
t2+1-s,t2+2-s),0,1,'ArrayValued',true);
t2 = t2+1;
j = j+1;
end
end
end
end
Sigma = Sigma + (Sigma-diag(diag(Sigma)))';
end
0 Kommentare
Akzeptierte Antwort
Harald
am 22 Sep. 2023
Hi,
it may be an alternative to calculate Sigma analytically using Symbolic Math Toolbox (please excuse any typos :) )
syms k c r a b tau x s t1 t2 u t x1 x2
sig(tau, s) = (k + c*exp(r*(tau+x)))*(a+t)*exp(-b*tau);
S = int( int(sig(u, x1-1+s), u, t1+1-s, t1+2-s) ...
.* int(sig(u, x2-1+s), u, t2+1-s, t2+2-s), s, 0, 1)
Using matlabFunction, you can convert this to a function handle.
This is quite a long expression and may take a bit to evaluate as well, but an advantage I see is that you can likely evaluate this for several t2 in a vectorized operation.
Best wishes,
Harald
5 Kommentare
Dyuman Joshi
am 23 Sep. 2023
Another modification can be to combine this
Comb(i,j,1) = t1;
Comb(i,j,2) = t2;
Comb(i,j,3) = x1;
Comb(i,j,4) = x2;
to
Comb(i,j,1:4) = [t1 t2 x1 x2];
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calculus finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!