IniziaInizia gratis

Visualize monthly mean, median and standard deviation of S&P500 returns

You have also learned how to calculate several aggregate statistics from upsampled data.

Let's use this to explore how the monthly mean, median and standard deviation of daily S&P500 returns have trended over the last 10 years.

Questo esercizio fa parte del corso

Manipulating Time Series Data in Python

Visualizza il corso

Istruzioni dell'esercizio

As usual, we have imported pandas as pd and matplotlib.pyplot as plt for you.

  • Use pd.read_csv() to import 'sp500.csv', set a DateTimeIndex based on the 'date' column using parse_dates and index_col, assign the results to sp500, and inspect using .info().
  • Convert sp500 to a pd.Series() using .squeeze(), and apply .pct_change() to calculate daily_returns.
  • .resample() daily_returns to month-end frequency (alias: 'M'), and apply .agg() to calculate 'mean', 'median', and 'std'. Assign the result to stats.
  • .plot() stats.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# Import data here
sp500 = ____

# Calculate daily returns here
daily_returns = ____

# Resample and calculate statistics
stats = ____

# Plot stats here


Modifica ed esegui il codice