Why is my while loop not working?

From this code, I am looking to get the nll values for all s = 0:999 where s is a parameter. When I run it, it runs very slow and only gives out one value of nll. Can anyone help me with getting the nll values for all values of the parameter s from 0 to 999.
% Initial parameter values
s = 0;
mu = 0.1;
om = 0.01;
% AT is a row vector
while s<=999
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);
j = 1:length(lambda);
nll = -sum(j.*log(lambda(j)))+sum(lambda(j));
end
s = s+1;
end

5 个评论

Provide the equation you are trying to solve. I would be guessing otherwise.
Is nll supposed to be a 2D rectangular array, or is it supposed to be a triangular array or a vector? Obviously, the code as given simply generates a scalar output, but this:
t = s+1:length(AT)
implies that the output is a triangular matrix of some shape, since the number of inner loop cycles varies with each pass of the outer loop.
For clarification:
a = 1:10
a = 1×10
1 2 3 4 5 6 7 8 9 10
b = 3+1:10
b = 1×7
4 5 6 7 8 9 10
c = 3+(1:10)
c = 1×10
4 5 6 7 8 9 10 11 12 13
It doesn't make sense that the output should be a vector either, since nothing in the inner loop appears to depend prior passes. It would just be discarding calculated data.
EDIT:
Here's this. I have no idea if this even makes sense, but it makes numbers. I doubt it's right, but I have no idea what it's supposed to do.
AT = 1:10;
% Initial parameter values
mu = 0.1;
om = 0.01;
srange = [0 9];
s = srange(1):srange(2);
nll = zeros(numel(AT),numel(s));
for si = 1:numel(s)
t = s(si)+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);
% this changes size only for the first pass, which seems wrong
% this means col1 values of nll are skewed wrt other columns
j = 1:length(lambda);
% store all the results
nll(ti,si) = -sum(j.*log(lambda(j)))+sum(lambda(j));
end
end
nll
nll = 10×10
1.0047 -46.7848 -45.2013 -41.5583 -36.0918 -29.0874 -20.8972 -11.9727 -2.9242 5.3554 1.6204 -46.6161 -44.6436 -40.6113 -34.7554 -27.3616 -18.7821 -9.4682 -0.0303 0 1.3258 -46.4974 -44.2457 -39.9338 -33.7983 -26.1250 -17.2658 -7.6723 0 0 -0.2183 -46.4059 -43.9363 -39.4060 -33.0523 -25.1607 -16.0833 0 0 0 -3.2647 -46.3315 -43.6830 -38.9736 -32.4409 -24.3702 0 0 0 0 -8.0146 -46.2687 -43.4685 -38.6073 -31.9228 0 0 0 0 0 -14.6354 -46.2144 -43.2826 -38.2897 0 0 0 0 0 0 -23.2704 -46.1665 -43.1185 0 0 0 0 0 0 0 -34.0450 -46.1238 0 0 0 0 0 0 0 0 -47.0705 0 0 0 0 0 0 0 0 0
Thank you @DGM, the code you have provided is a good baseline for me to work on! @David Hill The idea here is that for each value of the parameter s, we get a vector named lambda. Then we calculate the negative log likelihood (nll) by using the equation
j = 1:length(lambda);
nll = -sum(j.*log(lambda(j)))+sum(lambda(j));
So we should end up with nll values for each parameter s. For example if I wanted s = 0:9 then we would get 10 nll values for each of those s values.
Better describe the equation of lambda with respect to s and your constants.
One reason for slow is you have not initiated lambda.

请先登录,再进行评论。

回答(0 个)

类别

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

产品

版本

R2021a

提问:

2021-7-7

评论:

2021-7-14

Community Treasure Hunt

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

Start Hunting!

Translated by