Brownian Motion In R

Hi there. This page will be on Brownian Motion in R. It is assumed that the reader is familiar with Brownian motion.

Most of this code is based on the pages Random Walks In R and Creating Multiple Random Walk Paths In R



One Brownian Motion Path

Adding A Drift Component To Brownian Motion

Multiple Brownian Motion Paths


One Brownian Motion Path

Assuming that ggplot2 is installed into R, load in ggplot2 with the code:

In this next code, I create a function in R where the inputs are the initial time (t0), the terminal end time (T), the number of subintervals (N) from the initial time to the end time and the colour of the Brownian motion path line.

The if section in the beginning is to control for the initial time and T. I want the times to be non-negative and for the initial time to be less than the end time. The stop() function gives a customized error message letting the user what went wrong.

The else part assumes that there are no errors. A time increment variable dt is made along with a time vector named time_vect.

I put a comment stating that W_0 = 0 (i.e. Standard Brownian motion with no drift at time = 0). However,  the user may not put t_{0} = 0. To adjust for this, the first value would be W_{t0} \sim Norm(\mu = 0, \sigma^{2} = t_0). With these considerations, the position vector of  Brownian motion is associate with each time point with the c() and cumsum() functions.

I combine the time_vect vector and the position vector into a data frame inside the function called bm_df. I rename the column titles with colnames() and print a preview of the data frame with print() and head().

This next section of code in the function is the Brownian Motion plot with ggplot2. Time is on the horizontal axis and the Brownian position is on the vertical. To get the line, geom_line() is needed. All the other add-on functions are for titles and aesthetics.


Function Calls & Outputs


Adding A Drift Component To Brownian Motion

It was assumed that the drift (or y-intercept) was 0. A drift term (mu) can be added with a few modifications to the function code. Please refer to the code and comments.


Multiple Brownian Motion Paths

The plot above was for generating a single Brownian Motion path. This section will deal with multiple Brownian motion paths. One way is with using a for loop but I won’t show that here. Instead of a for loop, I will be using the replicate() function.

The first part of this code deals with initializing values. I set the number of Brownian motion paths to eight (as an example).

To store the information on time points and Brownian position for each path, a matrix is needed.

Instead of a for loop, the replicate() function is used to generate Brownian motion paths. The columns represent each of the eight Brownian motion paths.

The table needs some fine tuning. A time column will be added.

Notice how the column titles include Time, X1, X2 to X8? Those titles can be changed with the colnames() function. I use a for loop with the paste0() command to rename the column titles in an efficient manner.


Plotting The Brownian Motion Paths

The table is fixed for the most part but it is not ready for plotting purposes in ggplot2. The tidyr and dplyr packages will be used to convert this table from a wide format to a long format. The key function here is the gather() function from tidyr. (The %>% pipe operator is from dplyr.)

The data is ready for plotting in ggplot2. I propose two different plots which differ in colour.

This first plot has Time on the horizontal axis, the Brownian position on the vertical axis and the lines are grouped by Path (number). The add-on function geom_line() adds the lines. Functions such as labs(), theme() are for aesthetics. I have added a red dashed line with the use of geom_hline().

(If you are doing Brownian motion with drift change the yintercept from 0 to the drift value in geom_hline())



Having different coloured paths requires a small adjustment in the code.


  • You can also refer to the R documentation for BM() function in library(sde) or

Leave a Reply