仓库源文站点原文


layout: post title: "ML4T笔记 | 01-06 Histograms and scatterplots" date: "2019-01-12 01:12:12" categories: 计算机科学 auth: conge

tags: Machine_Learning Trading OMSCS

01 - Histograms and scatterplots

Time: 00:00:19

02 - A closer look at daily returns

histograms

Time: 00:02:12

03 - Quiz What would it look like

What the histogram of S&P 500 daily return over many years look like?

The correct answer: bell curve.

Time: 00:00:16

04 - Histogram of daily returns

Statistics we can run on it to characterize histograms.

Time: 00:02:25

05 - How to plot a histogram

daily_returns.hist(bin=20) will plot daily_return as histogram with 20 bins. the default bin parameter is 10.

Time: 00:02:03

06 - Computing histogram statistics

Calculate mea and deviation and kurtosis:

mean = daily_returns['SPY'].mean()
std = daily_returns['SPY'].std()
kurtosis = daily_returns.kurtosis()

Plot mean and diviation using axvline() in the Matplotlib library .

plt.axvline(mean, color='w', linestyle='dashed', linewidth=2)
plt.axvline(std, color='r', linestyle='dashed', linewidth=2)
plt.axvline(-std, color='r', linestyle='dashed', linewidth=2)
plt.show()

image.png

Time: 00:02:11

07 - quiz: Compare two histograms

Quiz: Select the option that best describes the relationship between XYZ and SPY.

Note:

correct answer: XYZ has a lower return and higher volatility than SPY.

8 - Plot two histograms together

Since the daily_returns data frame has data for two stocks, daily_returns.hist(bin=20) will plot the data in two subplots.

daily_returns['SPY'].hist(bin=20,label="SPY")
daily_returns['XOM'].hist(bin=20,label="XOM")
...

Time: 00:01:31

9 - Scatterplots

A scatterplot is another way to visualize the differences between daily returns of individual stocks. The left graph is daily return of two stocks. S&P 500 and XYZ.

Time: 00:02:02

10 - Fitting a line to data points

Time: 00:01:53

11 - Slope does not equal correlation

Time: 00:01:15

12 - Quiz: Correlation vs slope

quiz

Select the option that best compares ABC against XYZ, in terms of beta (slope of linear fit) and correlation with the market (represented by SPY).

13 - Scatterplots in python

Key codes

daily_returns.plot(kind='scattr',x='SPY', y='XOM') # scatterplot 
beta_XOM,alpha_XOM=np.polyfit(daily_returns['SPY'],daily_returns['XOM'], 1)
plt.plot(daily_returns['SPY'],beta_XOM*daily_returns['SPY'] + alpha_XOM, '-',color='r')
plt.show()

One last thing is to find the correlation yet again.

daily_returns.corr(method='pearson') will output in the correlation matrix with the correlation of each column with each other column.

Time: 00:04:45

14 - Real world use of kurtosis

Time: 00:01:06

Total Time: 00:24:11

2019-01-12 初稿