user124123
user124123

Reputation: 1683

Mathematica simulation random simulation

I'm trying to simulate mutation of two parts a and b in Mathematica. I put in the genetic drift (totally random element) by:

p[sim_, 0] = Join[Table[a, {15}], Table[b, {15}]]
p[sim_, t_] := p[sim, t] = RandomChoice[p[sim, t - 1], 30]

This created two tables with randomly composed of parts a and b.

I want to add in mutation, occurring with a given probability, so I've tried appending the following onto my code above

Do[ReplacePart[p, i -> b] && If[RandomReal[] > .2, t], {t, 1, 15}]

So it ought to replace an element i of the original two tables if a randomly drawn number between 0 and 1 is greater than .2.

So my final code looks like

p[sim_, 0] = Join[Table[a, {15}], Table[b, {15}]]
p[sim_, t_] := p[sim, t] = RandomChoice[p[sim, t - 1], 30];
Do[If[RandomReal[] < .2, 
  ReplacePart[p[sim, t], {_} -> RandomChoice[{a, b}]], 
  p[sim, t]], {t, 1, 15}]

But I can tell its not working as changing the value RandomReal has to be greater than is not doing anything, but I'm not getting an error message.

Can anyone tell me what's going wrong? Any help would be greatly appreciated!!

Upvotes: 1

Views: 201

Answers (1)

agentp
agentp

Reputation: 6989

Not sure if this is your only problem, but ReplacePart operates on the result returned by P[], but does not affect the function itself, ie.

f[0]={1,2,3}
ReplacePart[f[0],2->42] -> {1,42,3}

but you still get:

f[0] -> {1,2,3}

Also

{_} -> 

tells ReplacePart to replace every item. I'm not sure what your intent is there... the first incarnation i-> seems to make sense..

Upvotes: 2

Related Questions