AI For Trading: Volatility (26)

Volatility

Volatility is an important measure of risk, but what is risk?

Volatility is simply the standard deviation of the probability distribution of log returns.

In finance, volatility (symbol σ) is the degree of variation of a trading price series over time as measured by the standard deviation of logarithmic returns.
在金融中,波动率(符号σ)是交易价格序列随时间的变化程度,通过对数收益的标准差来衡量。

Quize:Volatility

Volatility literally refers to how "volatile" a stock is, meaning how unpredictably its price might change. A statistical measure of dispersion, such as standard deviation, is commonly used to measure volatility.
波动率字面上指的是股票的“波动性”,意味着它的价格可能会有多大的变化。分散的统计测量,例如标准偏差,通常用于测量波动率。

In the exercise below, you're given daily prices for two sample stocks. Compute the standard deviations of their log returns, and return the ticker symbol for the stock that is more volatile.
在下面的练习中,您将获得两个样本股票的每日价格。计算其日志返回的标准偏差,并返回更易变的股票的股票代码。

import pandas as pd
import numpy as np

def get_most_volatile(prices):
    """Return the ticker symbol for the most volatile stock.

    Parameters
    ----------
    prices : pandas.DataFrame
        a pandas.DataFrame object with columns: ['ticker', 'date', 'price']

    Returns
    -------
    ticker : string
        ticker symbol for the most volatile stock
    """
    # TODO: Fill in this function.
    pass

def test_run(filename='prices.csv'):
    """Test run get_most_volatile() with stock prices from a file."""
    prices = pd.read_csv(filename, parse_dates=['date'])
    print("Most volatile stock: {}".format(get_most_volatile(prices)))

if __name__ == '__main__':
    test_run()

Quize:Rolling Windows

Rolling Windows

Pandas.DataFrame.rolling

You've just learned about rolling windows. Let's see how we can use rolling function in pandas to create the rolling windows

First, let's create a simple dataframe!

import numpy as np
import pandas as pd
from datetime import datetime

dates = pd.date_range(datetime.strptime('10/10/2018', '%m/%d/%Y'), periods=11, freq='D')
close_prices = np.arange(len(dates))

close = pd.Series(close_prices, dates)
close
2018-10-10     0
2018-10-11     1
2018-10-12     2
2018-10-13     3
2018-10-14     4
2018-10-15     5
2018-10-16     6
2018-10-17     7
2018-10-18     8
2018-10-19     9
2018-10-20    10
Freq: D, dtype: int64

Here, we will introduce rolling function from pandas. The rolling function helps to provide rolling windows that can be customized through different parameters.

You can learn more about rolling function here

Let's take a look at a quick sample.

close.rolling(window = 3)
Rolling [window=3,center=False,axis=0]

This returns a Rolling object. Just like what you've seen before, it's an intermediate object similar to the GroupBy object which breaks the original data into groups. That means, we'll have to apply an operation to these groups. Let's try with sum function.

close.rolling(window = 3).sum()
2018-10-10     NaN
2018-10-11     NaN
2018-10-12     3.0
2018-10-13     6.0
2018-10-14     9.0
2018-10-15    12.0
2018-10-16    15.0
2018-10-17    18.0
2018-10-18    21.0
2018-10-19    24.0
2018-10-20    27.0
Freq: D, dtype: float64

The window parameter defines the size of the moving window. This is the number of observations used for calculating the statistics which is the "sum" in our case.

For example, the output for 2018-10-12 is 3, which equals to the sum of the previous 3 data points, 0 + 1 + 2.
Another example is 2018-10-20 is 27, which equals to 8+ 9 + 10

Not just for summation, we can also apply other functions that we've learned in the previous lessons, such as max, min or even more.

Let's have a look at another quick example

close.rolling(window = 3).min()
2018-10-10    NaN
2018-10-11    NaN
2018-10-12    0.0
2018-10-13    1.0
2018-10-14    2.0
2018-10-15    3.0
2018-10-16    4.0
2018-10-17    5.0
2018-10-18    6.0
2018-10-19    7.0
2018-10-20    8.0
Freq: D, dtype: float64

Now, the output returns the minimum of the past three data points.

By the way, have you noticed that we are getting NaN for close.rolling(window = 3).sum(). Since we are asking to calculate the mininum of the past 3 data points. For 2018-10-10 and 2018-10-11, there are no enough data points in the past for our calculation, that's why we get NaN as outputs.

There are many other parameters you can play with for this rolling function, such as min_period or so. Please refer to the python documentation for more details

Quiz: Calculate Simple Moving Average

Through out the program, you will learn to generate alpha factors. However, signals are always noisy. A common practise from the industry is to smooth the factors by using simple moving average. In this quiz, we can create a simple function that you can specify the rolling window and calculate the simple moving average of a time series.

import quiz_tests

def calculate_simple_moving_average(rolling_window, close):
    """
    Compute the simple moving average.

    Parameters
    ----------
    rolling_window: int
        Rolling window length
    close : DataFrame
        Close prices for each ticker and date

    Returns
    -------
    simple_moving_average : DataFrame
        Simple moving average for each ticker and date
    """
    # TODO: Implement Function

    return close.rolling(rolling_window).mean()

quiz_tests.test_calculate_simple_moving_average(calculate_simple_moving_average)
Tests Passed

为者常成,行者常至