Random walk II
In the last video, you have also seen how to create a random walk of returns by sampling from actual returns, and how to use this random sample to create a random stock price path.
In this exercise, you'll build a random walk using historical returns from Facebook's stock price since IPO through the end of May 31, 2017. Then you'll simulate an alternative random price path in the next exercise.
This exercise is part of the course
Manipulating Time Series Data in Python
Exercise instructions
We have already imported pandas
as pd
, choice
and seed
from numpy.random
, seaborn
as sns
, and matplotlib.pyplot
as plt
. We have also imported the FB stock price series since IPO in May 2012 as the variable fb
. Inspect this using .head()
.
- Set seed to 42.
- Apply
.pct_change()
to generate daily Facebook returns, drop missing values, and assign todaily_returns
. - Create a variable
n_obs
that contains the.count()
of Facebookdaily_returns
. - Use
choice()
to randomly selectn_obs
samples fromdaily_returns
, and assign torandom_walk
. - Convert
random_walk
to apd.Series
and reassign it to itself. - Use
sns.distplot()
to plot the distribution ofrandom_walk
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Set seed here
# Calculate daily_returns here
daily_returns = ____
# Get n_obs
n_obs = ____
# Create random_walk
random_walk = ____
# Convert random_walk to pd.series
random_walk = ____
# Plot random_walk distribution