It is a basic program to describe the Moran process in the evolutionary game, however with my limited knowledge of Matlab, I have difficulties to quickly understand what the code means. Could someone help me to explain what it means.
If you look at the code, there are a few things I am confused about.
- is variable
statein the code an array ? - if
stateis an array, what does it mean to dostate(2,:), state(:,2), state(:) - in
uniquefunction, what does this statement mean:u(u((1:end-1)')==u((2:end)')) = []; - in
mnrndfunction, what does this statement mean:r = find(rand < cumsum(p),1); - what is the meaning of
frequency = histc(state(:,2), livingTypes)./length(state(:,2));,
and in particular,histc?
Here is the function:
function state = moranprocess(initialState, nSteps, tau)
%# assume 1 step if not specified
if(nargin < 2)
nSteps = 1;
end
%# if it isn't specified, assume frequency indepdence.
if(nargin < 3)
tau = 0;
end
%# initialize with starting state
state = initialState;
%# perform the moran process
for t = 1:nSteps
state(selection(state, 0), :) = state(selection(state, tau), :);
end
end
%# frequency dependent selection with parameter tau determining the
%# strength of selection
function i = selection(state, tau)
%# find all of the living types
livingTypes = unique(state(:,2))';
%# create a multinomial of living type frequencies.
frequency = histc(state(:,2), livingTypes)./length(state(:,2));
%#frequency = makemultinomial(state(:,2));
fitness = (frequency.^tau)./sum(frequency.^tau);
%# selection is proportional to fitnesss
selected_type = livingTypes(mnrnd(1, (frequency.*fitness) ./ sum(frequency.*fitness)));
%# choose randomly among those of the selected type
thoseOfSelectedType = find(state(:,2) == selected_type);
i = thoseOfSelectedType(ceil(length(thoseOfSelectedType)*rand));
end
%# fast unique
function u = unique(x)
u = sort(x(:));
u(u((1:end-1)')==u((2:end)')) = [];
end
%# fast mnrnd
function r = mnrnd(n,p)
r = find(rand < cumsum(p),1);
end
1 It certainly looks as if
stateis a variable and a 2D array at that. If you have the workspace window visible on your desktop (that’s the window with the titleWorkspacenot any of the other windows) you should be able to double-click on the variable name and open up the variable editor.2
state(2,:)means row 2 of the 2D arraystate;state(:)uses a somewhat confusing (to the beginner) Matlab shorthand to mean all the elements instateconsidered as a 1D vector, and don’t forget that Matlab stores arrays in column-major order. Matlab has the facility to use 1D indexes into greater-than-1-D arrays. You should be able to figure outstate(:,2)yourself, if not, play around; one of Matlab’s strengths is it’s readiness to let you play around.3 The expression
u(u((1:end-1)')==u((2:end)')) = []is only confusing when you try to take it in all at once. You can work it out with a little care and attention. Start from the inside:(1:end-1)means all the elements of (in this case the vectoru) starting at the first and going on to the last one but one. So, ifuhas 10 elements this expression selects elements1:9. The'is Matlab’s transposition operator, it converts a row-vector to a column-vector (and vice-versa). Since it’s applied on both sides of the==I’m not sure it serves any purpose here, but experiment with(1:9)and(1:9)'and similar expressions.Next, the expression
u((1:end-1)')==u((2:end)')compares, for equality, the elementsu(1:end-1)with the elementsu(2:end), in other words it finds those cases where an element ofuis the same as its neighbour. Like other boolean operators in Matlab this will return0forfalseand1fortrue— again experiment with operations such as1==1and1==2. This expression is going to return what Matlab calls a logical index intou, that is it will select those elements ofufor which the evaluation istrue(or1).Finally, with the expression
= []Matlab assigns the empty array to the elements ofuselected by the logical indexing. This operation deletes those element.So, if I’ve figured this out correctly the statement deletes from
uall those elements which are the same as the preceding element.4 Now it’s way past time for you to do some of your own work.
find,randandcumsumare all basic Matlab functions which are well documented. As I suggested above, take some time to figure out what they do in isolation, then start playing around with various combinations of them. You’ll catch on much more quickly that way than by reading any more of this drivel.