Member-only story
In my last blog post on this subject, I carried out a Monte Carlo random walk in Python, and that post can be read here:- https://tracyrenee61.medium.com/carry-out-a-monte-carlo-random-walk-in-python-6be36570eb17
In this video I intend to carry out a Monte Carlo random walk in R. both programs are similar in the way they are coded, but still need to be covered separately to get the full value of both languages.
I have written the code in MyCompiler because this is a free online platform that will produce graphics.
There are no libraries that need to be imported into the program, as all of the necessary functions are built into the programming language or have been coded into the program.
The first thing that needs to be done is to define and initialise the variables:-
- N_steps is initialised at 20
- N_simulations is initialised at 1
I then created the function, random_walk, which accepts n_steps as input:-
- The variable, steps, is defined, with a sample of either -1 or 1 for the number of n_steps, with replacement.
- The variable, position, is defined as the cumulative sum of the steps.
- The function will return the position.