Clear Filters
Clear Filters

can you add probability to a for loop?

33 views (last 30 days)
Kitt
Kitt about 20 hours ago
Edited: dpb about 4 hours ago
I have a very long and complex fitness function that I want to add even more complexity to, and I'm wondering if I can shortcut it.
The basic idea is that an individual has to choose between two patches to forage in and the patches can now have a varying probability, either high or low, of finding food.
I want the combination of probabilities to be different from each other, that is
p1 = prob of finding food in patch 1 = hi or lo
p2 = prob of finding food in patch 2 = hi or lo
(p1,p2) combinations
30% chance of (hi,lo)
25% chance of (hi,hi)
25% chance of (lo,lo)
20% chance of (lo,hi)
The most straight forward way would just be adding it up
total fitness Fd = 0.3(F1) + 0.25(F2) + 0.25(F3) + 0.2(F4)
My code for fitness is already very long with only one combination of finding food probabilities. Is there a way I could do a for loop with these combinations and add in that probability or do I have to go the long way around?
  2 Comments
Matt J
Matt J about 19 hours ago
What is "the long way around"?
Kitt
Kitt about 19 hours ago
Fdd(i,j,k,tt)= (1-u1)*( ...
b(tt)*(...
m1(tt)*(...
p(z1(j),k)*(n1(tt)*state1 + (1-n1(tt))*state2)+...
(1-p(z1(j),k))*(n2(tt)*state3 + (1-n2(tt))*state4) ...
)+...
(1-m1(tt))*(...
p2(z2(j),k)*(n1(tt)*state5 + (1-n1(tt))*state6)+...
(1-p2(z2(j),k))*(n2(tt)*state7 + (1-n2(tt))*state8) ...
) ...
)+...
(1-b(tt))*( ...
m2(tt)*(...
p3(j,y1(k))*(n1(tt)*state9 + (1-n1(tt))*state10)+ ...
(1-p3(j,y1(k)))*(n2(tt)*state11 + (1-n2(tt))*state12) ...
)+ ...
(1-m2(tt))* ...
(p4(j,y2(k))*(n1(tt)*state13 + (1-n1(tt))*state14)+...
(1-p4(j,y2(k)))*(n2(tt)*state15 + (1-n2(tt))*state16) ...
) ...
) ...
);
This is the fitness with p1 (represented here as n1) and p2 (represented here as n2) being static. I would do this 4 times with a combination of n1=0.9 or 0.3 and n2=0.9 or 0.3
I know that's a totally valid way of doing it, but I want to try and learn how to do this in a more efficient way if possible.

Sign in to comment.

Answers (1)

dpb
dpb about 15 hours ago
Edited: dpb about 4 hours ago
Are F1, F2, ..computationally different other than the two probabilities I gather from the above?
If not, encapsulate the calculation in a function and just call the function with the combinations and return in an array
HL=[0.9 0.3];
P=unique(combnk([HL HL],2),'rows');
W=[0.25; 0.20; 0.30; 0.25];
N=size(W,1);
F=zeros(N,1);
for i=1:N
F(i)=functionF(P(i,:));
end
Fd=dot(W,F);
  2 Comments
Kitt
Kitt about 14 hours ago
Edited: Kitt about 13 hours ago
That's something I wanted to do, to see if there's a way to turn my long equation into a function. It's difficult because there's a lot of little different things. I already have a function for calculating the "state" the individual is in (of which there are 20), which is making it hard for me to conceptualize how to do this in a function?
%a snippet of the equation:
p(z1(j),k)*(n1*state1 + (1-n1)*state2)...
%state1 and state2 are completely different
%in one fitness function there are 16 states all at the same time, not
%just running through 1 at a time
%my equation is basically the law of total expectation embedded like 4
%times
I'm still new to learning matlab, but this gives me a starting point, I think, in how I can add this in!
dpb
dpb about 10 hours ago
%a snippet of the equation:
p(z1(j),k)*(n1*state1 + (1-n1)*state2)...
...
Variables like state1 and state2 ... stateN are likely indicators that state should be an array and then things could be written with vector/matrix algebraic expressions -- or at least turned into loops.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!