• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Finance Train

Finance Train

High Quality tutorials for finance, risk, data science

  • Home
  • Data Science
  • CFA® Exam
  • PRM Exam
  • Tutorials
  • Careers
  • Products
  • Login

Simulate Random Walk (RW) in R

Data Science, Statistics

This lesson is part 17 of 27 in the course Financial Time Series Analysis 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.

Previous Lesson

‹ Simulate White Noise (WN) in R

Next Lesson

AutoRegressive (AR) Model in R ›

Join Our Facebook Group - Finance, Risk and Data Science

Posts You May Like

How to Improve your Financial Health

CFA® Exam Overview and Guidelines (Updated for 2021)

Changing Themes (Look and Feel) in ggplot2 in R

Coordinates in ggplot2 in R

Facets for ggplot2 Charts in R (Faceting Layer)

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

In this Course

  • Financial Time Series Data
  • Exploring Time Series Data in R
  • Plotting Time Series in R
  • Handling Missing Values in Time Series
  • Creating a Time Series Object in R
  • Check if an object is a time series object in R
  • Plotting Financial Time Series Data (Multiple Columns) in R
  • Characteristics of Time Series
  • Stationary Process in Time Series
  • Transforming a Series to Stationary
  • Time Series Transformation in R
  • Differencing and Log Transformation
  • Autocorrelation in R
  • Time Series Models
  • ARIMA Modeling
  • Simulate White Noise (WN) in R
  • Simulate Random Walk (RW) in R
  • AutoRegressive (AR) Model in R
  • Estimating AutoRegressive (AR) Model in R
  • Forecasting with AutoRegressive (AR) Model in R
  • Moving Average (MA) Model in R
  • Estimating Moving Average (MA) Model in R
  • ARIMA Modelling in R
  • ARIMA Modelling – Identify Model for a Time Series
  • Forecasting with ARIMA Modeling in R – Case Study
  • Automatic Identification of Model Using auto.arima() Function in R
  • Financial Time Series in R – Course Conclusion

Latest Tutorials

    • Data Visualization with R
    • Derivatives with R
    • Machine Learning in Finance Using Python
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Financial Reporting Standards
    • Fraud
Facebook Group

Membership

Unlock full access to Finance Train and see the entire library of member-only content and resources.

Subscribe

Footer

Recent Posts

  • How to Improve your Financial Health
  • CFA® Exam Overview and Guidelines (Updated for 2021)
  • Changing Themes (Look and Feel) in ggplot2 in R
  • Coordinates in ggplot2 in R
  • Facets for ggplot2 Charts in R (Faceting Layer)

Products

  • Level I Authority for CFA® Exam
  • CFA Level I Practice Questions
  • CFA Level I Mock Exam
  • Level II Question Bank for CFA® Exam
  • PRM Exam 1 Practice Question Bank
  • All Products

Quick Links

  • Privacy Policy
  • Contact Us

CFA Institute does not endorse, promote or warrant the accuracy or quality of Finance Train. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.

Copyright © 2021 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us