I'd like to run a simple financial simulation with something like the following constraints:
- You start out with $S$ dollars. Let's say $S$ is $\$1M$ dollars
- You repeatedly make an investment that costs $I$ dollars (let's say $I = \$5K$) with an $F$ percent (let's say $F = 0.95$) chance of returning $\$0$ and an $(1-F)$ percent chance of returning $\%100,000$ of $I$ (or just $1,000 \cdot I$).
Is there a way in Mathematica to repeatedly make this investment hundreds of times over time? Could we have Mathematica repeatedly run the simulation and then plot the average of all of its simulation as a line as well?
Answer
It can certainly be done using NestList
:
s = 100;
i = 0.5;
f = 0.95;
nStep = 100;
sim := NestList[If[RandomReal[] < f, # - i, # - i + 1000 i] &, s,
nStep];
Now sim
will generate a simulation of such process at each evaluation. You can use the following to do multiple runs and plot:
runs = Table[sim, {r, 100}];
ListPlot[runs, Joined -> True, PlotRange -> All]
Also plot the means:
ListPlot[Mean @ runs, Joined -> True, PlotRange -> All]
Comments
Post a Comment