美文网首页
01-06 Histograms and scatter plo

01-06 Histograms and scatter plo

作者: 非常暴龙兽 | 来源:发表于2019-05-13 22:06 被阅读0次
  • 峰度 kurtosis
    峰度为正,fat tails 尾部比一般Gaussian分布要大;峰度为负,skinning tails,与fat tails相反。

  • 如何绘制直方图

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()
  • python中的散点图
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')

相关文章

网友评论

      本文标题:01-06 Histograms and scatter plo

      本文链接:https://www.haomeiwen.com/subject/mhcraqtx.html