R White Noise Simuation

08.04.2021

Intro

White noise is a base line model that appears when we have removed correlations and difference. The model is a simple list of random errors and serves as a base for many time series models. In this article, we will learn how to simulate white noise in R.

What is White Noise

White noise is defined that for each observation, we can compute this observation as random noise, usually from the normal distribution. The equation is as follows:

xt = wt

Where wt is distributed from normal distribution of mean 0 and 1.

Simulating by Hand

To get a very good understanding, let’s try to simulate this by hand. We do this by sample each element from the random normal distribution, for example, say we ran rnorm(1, mean = 0, sd = 1) and received 1.5. That will be our first value.

Then, for our second value, we run the function again and get .4, that will be our second value. We can continue this way for as long as we like.

And now we have a small time series [1.5, 4]. We can continue this for as long as we want to simulate. Let’s continue that way in code.

Simulating White Noise in R

First we will set a seed so that you can reproduce the same results, and we will create a size variable to designate how large of a time series we want to simulate.

set.seed(1)

size <- 200

Next, we will create an empty vector to hold all the data and initialize the first time series entry from a normal distribution.

# Create an empty array to hold our time series
x = numeric(size)

# Create the first value
x[1] <- rnorm(n = 1, mean = 0, sd = 1)

Now, we loop until we have enough variables using the same computation we did above to create a variable at each step.

for (t in 2:size) {
    # Create random noise for this observation
    w.error = rnorm(n = 1, mean = 0, sd = 1)
    
    # Compute the current value based on the previous value and the current noise
    x[t] <- w.error
}

Now, we can plot this data, and we will see a familiar white noise shape.

plot(x)

unnamed chunk 4 1

Simulating with a Library

In practice, we often want simulate white noise by hand or incrementally in code. Instead, we can use the build in arima.sim function. To do this, we can call the method and pass the model c(0, 0, 0). This stands for 0 AR components, 0 difference component, and 0 MA components.

The details are a bit out of scope for this article, but will be covered in a future post for how to simulate ARIMA models by hand. But, we note that white noise removes all components to 0.

Finally, we pass the number of observations we want to the n parameters

set.seed(1)

white.noise = arima.sim(model = list(order = c(0, 0, 0)), n = 200)

We can end by plotting to see the white noise shape as expected.

plot(white.noise)

unnamed chunk 6 1