01-06 Histograms and scatter plo

2019-05-13  本文已影响0人  非常暴龙兽
def test_run():
    #Read data
    dates = pd.date_range('2009-01-01', '2012-12-31')
    symbols = ['SPY']
    df = get_data(symbols, dates)
    #compute daily returns
    daily_returns = compute_daily_returns(dt)
    daily_returns.hist(bins = 20) #hist=histogram直方图,此处bins默认为10
    plt.show
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()
#compute kurtosis 计算峰度
print daily_returns.kurtosis()

#比较:mean >> return, std>>volatility(risk)
#两个图分开画
def test_run():
    #Read data
    dates = pd.date_range('2009-01-01', '2012-12-31')
    symbols = ['SPY', 'XOM']
    df = get_data(symbols, dates)
    plot_data(df)
    #Compute daily returns
    daily_returns = compute_daily_returns(df)
    plot_data(daily_returns, title = "Daily returns", ylabel = "Daily returns")
    #Plot histogram directly from dataframe
    daily_returns.hist(bins = 20)
#两张图叠在一起
def test_run():
    #Read data
    dates = pd.date_range('2009-01-01', '2012-12-31')
    symbols = ['SPY', 'XOM']
    df = get_data(symbols, dates)
    plot_data(df)
    #Compute daily returns
    daily_returns = compute_daily_returns(df)
    #compute and plot both histograms on the same chart
    daily_returns['SPY'].hist(bins = 20, label = 'SPY')
    daily_returns['XOM'].hist(bins = 20, label = 'XOM')
    plt.legend(loc = 'upper right')
    plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from util import get_data, plot_data
def compute_daily_return(df):
    daily_returns = df.copy()
    daily_returns[1: ] = (df[1: ] / df[: -1].values) - 1
    daily_returns.ix[0, : ] = 0
    #set daily returns, for row 0 to 0
    return daily_returns
def test_run():
    #Read data
    dates = pd.date_range('2009-01-01', '2012-12-31')
    symbols = ['SPY', 'XOM', 'GLD']
    df = get_data(symbols, dates)
    #compute daily returns
    daily_returns = compute_daily_returns(df)
    #Scatterplot SPY vs XOM
    daily_returns.plot(kind = 'scatter', x = 'SPY', y = 'XOM')
    beta_XOM, alpha_XOM = np.plotfit(daily_returns['SPY], daily_returns['XOM'], 1)
    print "beta_XOM = ",beta_XOM
    print "\n alpha_XOM = ", alpha_XOM
    plt.plot(daily_returns['SPY'], beta_XOM * daily_returns['SPY'] + alpha_XOM, '-', color = 'r')
    plt.show()
    #Scatterplot SPY vs GLD, 把上文XOM全换成GLD
    #Calculate correlation coefficient
    print daily_returns.corr(method = 'pearson')
上一篇 下一篇

猜你喜欢

热点阅读