Git Product home page Git Product logo

dsc-3-26-03-basic-time-series-models-lab-online-ds-ft-031119's Introduction

Basic Time Series Models - Lab

Introduction

Now that you have an initial understanding of the the White Noise and the Random Walk model, let's try to generate some examples!

Objectives

You will be able to:

  • Explain what the goal is of time series modeling
  • Understand and explain what a White Noise model is
  • Understand and explain what a Random Walk model is
  • Understand the mathematical formulations of Random Walk and White Noise models

A White Noise Model

To get a good sense of how a model works, it is always a good idea to generate a process. Let's consider the following example:

  • Every day in August, September and October 2018, Nina takes the subway to work. Let's ignore weekends for now and assume that Nina works every day.
  • We know that on average, it takes her 25 minutes, and the standard deviation is 4 minutes.
  • Create and visualize a time series that reflects this information.

Let's import pandas, numpy and matplotlib.

np.random.seed(12) # do not change this seed.

Create the dates. You can do this using pd.date_range, more info here.

# your code here

Generate the values for the wite noise process representing Nina's commute in August and September.

# your code here

Create a time series with the dates and the commute times.

# your code here

Visualize the time series and set appropriate axis labels.

# your code here

Print Nina's shortest and longest commute.

# your code here
# your code here

Look at the distribution of commute times.

# your code here

Compute the standard deviation and the mean of the commute series. The fact that the mean and standard error are constant over time is crucial!

# your code here
# your code here

Now, let's look at the mean and standard error for August and October respectively.

# your code here

Because you've generated this data, you know that the mean and constant will be the same over time. But comparing mean and standard deviation over time is useful practice for real data examples to check if a process is White Noise!

A Random Walk Model

Remember from the lecture that the Random Walk Model:

  • Has no specified mean or variance
  • Has a strong depencence over time

Mathematically, this can be written as:

$$Y_t = Y_{t-1} + \epsilon_t$$

Because today's value depends on yesterday's, you need a starting value when you start off your time series. In practice, this is what the first few time series values look like: $$ Y_0 = \text{some specified starting value}$$ $$Y_1= Y_{0}+ \epsilon_1 $$ $$Y_2= Y_{1}+ \epsilon_2 = Y_{0} + \epsilon_1 + \epsilon_2 $$ $$Y_3= Y_{2}+ \epsilon_3 = Y_{0} + \epsilon_1 + \epsilon_2 + \epsilon_3 $$ $$\ldots $$

Keeping this in mind, let's create a random walk model:

  • starting from a value of 1000 USD of a share value upon a company's first IPO (initial public offering) in 2010 until end of november of the same year, generate a random walk model with a white noise error term, which has a standard error of 10.
# keep the random seed
np.random.seed(11)

# create a series with the specified dates

Visualize the time series with correct axis labels

# your code here

You can see how this very much looks like the exchange rate series you looked at in the lecture!

Random Walk with a Drift

Repeat the above, but include a drift parameter $c$ of 8 now!

# keep the random seed
# your code here

Note that there is a very strong drift here!

Differencing in a Random Walk model

One important property of the Random Walk model is that a differenced random walk returns a white noise. This is a result of the mathematical formula:

$$Y_t = Y_{t-1} + \epsilon_t$$ which is equivalent to $$Y_t - Y_{t-1} = \epsilon_t$$

and we know that $\epsilon_t$ is a mean-zero white noise process!

Plot the differenced time series for the shares time series (no drift).

# your code here

This does look a lot like a white noise series!

Plot the differenced time series for the shares time series (with a drift).

# your code here

This is also a white noise series, but what can you tell about the mean?

The mean is equal to the drift $c$, so 8 for this example!

Summary

Great, you now know how a Random Walk and Moving Average model work!

dsc-3-26-03-basic-time-series-models-lab-online-ds-ft-031119's People

Contributors

loredirick avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.