How to perform this for loop for multiple parameter values

I have the following script and vector of temperatures. I need to run it for parameter values of s=0:1:999, mu=0.1:0.1:100, om=0.01:0.01:10, i.e I need to run the script so many times such that all combinations of the parameters are used. I should end up with 1000^3 nll values stored in an array or table.
If anyone could offer some advice on how to approach this, it would be much appreciated.
% T is temperatures, t = time in days
load("Temperature.mat")
T = T'; % from column vector to row vector
AT = T-min(T); % this is the adjusted positive temperatures
s = 0; % lag in days
mu = 0.1;
om = 0.01; % omega
for t=s+1:length(AT)
k = abs(s:-1:(s-t+1));
id=[(t-1):-1:0]+1;
lambda(t) = mu+sum(om.^k.*AT(id))/sum(om.^k);
end
for j=1:length(lambda)
nll = -sum(j*log(lambda(j)))+sum(lambda(j));
end

4 个评论

Are you sure this is what you want? On my PC your script takes about 5 seconds. Running this 10^9 times would take about 160 years.
@Are Mjaavatten Hi, so the idea here is that I need to make a 3D plot such that the coordinates are the parameter values s, mu, om. The plotted points will be coloured and the colour will represent the value of the negative log likelihood (nll). I want to try it for a smaller range first so maybe 10 equally spaced points for each parameter, i.e.
s = 0:1:9, mu = 0.1:0.1:1, om = 0.1:0.1:1
Regarding the run time, I would need to see if I could vectorize the code once I have found a working code that can output the required nll values for each of the parameter values.
In line 16, you throw away all results except the last. Should it rather be
nll(j) = -sum(j*log(lambda(j)))+sum(lambda(j));
I think you need to rethink what you want and if you are on the right track. I recommend to start with just one year of data so things are quicker and easier to inspect.
Try to compare results for, say, s = 0 and s = 30 or 180. What do you want to use in your final plot? You surely do not want to plot 12054 x 1000 x 1000 x 1000 data points?
@Are Mjaavatten This is now my updated code
% parameter values
srange = [0 9];
om = 0.1;
mu = 0.1;
r = mypoiss(5,om,mu);
s = srange(1):srange(2);
nll = zeros(numel(s));
for si = 1:numel(s)
t = max(srange)+1:numel(AT);
for ti = 1:numel(t)
k = abs(s(si):-1:(s(si)-t(ti)+1));
id = ((t(ti)-1):-1:0)+1;
lambda(t(ti)) = mu+sum(om.^k.*AT(id))/sum(om.^k);
% store all the results
end
nll(si) = 0;
nll(si) = nll(si) - (r(ti).*log(lambda(t(ti)))) + (lambda(t(ti)));
end
where I am still loading the temperatures in same as before. r is the function
function r = mypoiss(s,om,mu)
% output; r is synthetic time series for number of cases
% input; s is lag, om is decay rate, mu is baseline rate
% T is temperatures, t = time in days
load("Temperature.mat")
T = T'; % from column vector to row vector
AT = T-min(T); % this is the adjusted positive temperatures
for t=s+1:length(AT)
k = abs(s:-1:(s-t+1));
id=[(t-1):-1:0]+1;
lambda(t) = mu+sum(om.^k.*AT(id))/sum(om.^k);
end
r = poissrnd(lambda);
So now this loops for all values of s from 0 to 9 and gives 10 nll values stored in a 10x10. I now want the parameter s to be from 0 to 60 but mu and om I think I can get away with just 100 equally spaced points for each.
The problem here is that I don't know how I can loop this for all the other parameter values.
An example of what I am looking for is the second plot from here:
The axis should be the parameters s, mu, om and the colorbar should be values of nll.
Thank you for your help so far!

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Graphics Performance 的更多信息

产品

版本

R2021a

提问:

2021-7-7

编辑:

2021-7-8

Community Treasure Hunt

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

Start Hunting!

Translated by