Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Challenge: Visualize Stock Volatility | Financial Data Visualization and Trend Analysis
Python for Financial Analysts
セクション 2.  3
single

single

bookChallenge: Visualize Stock Volatility

メニューを表示するにはスワイプしてください

Rolling volatility, measured as the standard deviation of returns over a moving window, is a key indicator of how much a stock's price fluctuates over time. In financial analysis, understanding volatility helps you assess risk, compare assets, and make informed investment decisions. By tracking rolling volatility, you can identify periods of market turbulence or calm, spot trends in risk, and better manage portfolio exposure.

123456789101112131415161718
import pandas as pd import numpy as np # Create a date range for one year of business days dates = pd.date_range(start="2023-01-01", end="2023-12-31", freq="B") # Simulate daily returns for four stocks with different volatility levels np.random.seed(42) returns_data = { "AAPL": np.random.normal(0, 0.012, len(dates)), "MSFT": np.random.normal(0, 0.010, len(dates)), "GOOGL": np.random.normal(0, 0.015, len(dates)), "AMZN": np.random.normal(0, 0.018, len(dates)), } returns_df = pd.DataFrame(returns_data, index=dates) # Display the first few rows print(returns_df.head())
copy

To analyze and visualize the volatility of these stocks, you will calculate the 30-day rolling standard deviation of returns for each stock. This rolling standard deviation represents the recent volatility and is commonly used to monitor risk over time. After computing the rolling volatility, plot all four stocks' rolling volatility on a single chart using matplotlib. Make sure to plot each stock as a separate line, label each line with the stock symbol, and add a legend so viewers can easily distinguish between the stocks. Label the axes and provide a clear title to make your plot informative and professional.

タスク

スワイプしてコーディングを開始

Given a DataFrame containing daily returns for four stocks over a year, your goal is to visualize their rolling volatility. Calculate the 30-day rolling standard deviation for each stock and plot the results on a single chart. Ensure that each stock's rolling volatility is clearly labeled and distinguishable.

  • Calculate the 30-day rolling standard deviation for each stock in the DataFrame.
  • Plot the rolling volatility of all four stocks on one chart, with each stock as a separate line.
  • Label each line with the corresponding stock symbol, and add a legend.
  • Label the x-axis as "Date" and the y-axis as "Rolling Std Dev (Volatility)".
  • Add a clear title to the plot.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  3
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt