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