Simulate Random Walk (RW) in R

When a series follows a random walk model, it is said to be non-stationary. We can stationarize it by taking a first-order difference of the time series, which will produce a stationary series, that is, a Zero Mean White Noise series. For example, the stock prices of a stock follow a random walk model, and the series of returns (differencing of pricing series) will follow White Noise model.

Let's understand this phenomenon in more detail.

Since the differencing of a Random Walk series is a White Noise series, we can say that a Random Walk series is a cumulative sum (i.e., Integration) of a zero mean White Noise series. With this information, we can define Random Walk series in the form of ARIMA model as follows:

ARIMA(0,1,0)
Where:
- Augoregressive part, p = 0
- Integration, d = 1
- Moving average part, q = 0

Simulate Random Walk Series

We can now simulate a random walk series in R by supplying the appropriate parameters to the arima.sim() function as shown below:

RW <- arima.sim(model= list(order = c(0, 1, 0)), n=200)

We can plot the newly generated series using the plot.ts() function.

> plot.ts(RW,main="Random Walk", col=4)

As we can clearly observe, this is a non-stationary series and its mean and standard deviation is not constant over time.

First Difference Series

In order to make the series stationary, we take the first order difference of the series.

> RW_diff <- diff(RW)

When plotted you will notice that the difference series resembles White Noise.

The statistics for the RW_diff series are calculated below:

> mean(RW_diff)
[1] -0.00903282
> sd(RW_diff)
[1] 1.020447
>

Random Walk with Drift

The above Random Walk series that we simulated wanders up and down around the mean. However, we can have the Random Walk series follow an up or a down trend, called drift. To do so, we provide an additional argument mean/intercept to the arima.sim() function. This intercept is the slope for the model. We can also change the standard deviation of the simulated series. In the following code, we supply a mean of 1 and a standard deviation of 5.

> RW_drift <- arima.sim(model= list(order = c(0, 1, 0)), n=200, mean=1,sd=5)
> plot.ts(RW_drift, main="Random Walk with Drift")

Estimating Random Walk Model

To fit a random walk model with a drift to a time series, we will follow the following steps

  1. Take the first order difference of the data.
  2. Fit the white noise model to the differenced data using arima() function with order of c(0,0,0).
  3. Plot the original time series plot.
  4. Add the estimated trend using the abline() function by supplying the intercept we got by fitting the White Noise model as the slope.

1. First Order Difference

In order to make this series stationary, we will take the difference of the series.

> RW_drift_diff <-diff(RW_drift)
> plot.ts(RW_drift_diff, col=4, main="First Order Difference")

2. Fit White Noise model to differenced data

We can now use the arima() function to fit the White Noise model to the differenced data.

> whitenoise_model <- arima(RW_drift_diff, order = c(0, 0, 0))
> whitenoise_model
Call:
arima(x = RW_drift_diff, order = c(0, 0, 0))
Coefficients:
      intercept
         0.9189
s.e.     0.3631
sigma^2 estimated as 26.37:  log likelihood = -611.01,  aic = 1226.01
>

We can see that the fitted White Noise model has an intercept of 0.9189.

3. Plot the original Random Walk data

This can be done using the following command:

> plot.ts(RW_drift,col=4, main="Random Walk with Drift")

4. Add the estimated trend

Now on the same plot we want to add the estimated trend. At the beginning of this lesson we explained how a Random Walk series is a cumulative sum (i.e., Integration) of a zero mean White Noise series. So, the intercept, in effect, is actually the slope for our random walk series.

We can plot the trend line using the abline(a,b) function, where a is the intercept and b is the slope of the line. In our case, we will specify 'a=0' and 'b=intercept' of the White Noise model.

> abline(0, whitenoise_model$coef,col=2)

The estimated trend line will be added to our plot.

Related Downloads

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book includes PDFs, explanations, instructions, data files, and R code for all examples.

Get the Bundle for $39 (Regular $57)
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book comes with PDFs, detailed explanations, step-by-step instructions, data files, and complete downloadable R code for all examples.