zoukankan      html  css  js  c++  java
  • [Python] Histograms for analysis Daily return

     A histogram is an accurate representation of the distribution of numerical data. 

    Y axis is the occurances, X axis is the % of daily return. 

    There are three things can meature histogram

    1. Standard deviation

    2. Mean

    3. Kurtosis : In probability theory and statisticskurtosis is a measure of the "tailedness" of the probability distribution of a real-valued random variable.

    Plot a histogram:

    import os
    import pandas as pd
    import matplotlib.pyplot as plt
    
    def compute_daily_return(df):
        dr = df.copy()
        dr = (df / df.shift(-1)) -1
        return dr
    
    def histogram(df):
        dr = compute_daily_return(df)
        plot_data(dr, title="Daily returns", yLabel="Daily returns")
        dr.hist(bins=20)
        plt.show()
    
    
    if __name__ == '__main__':
        df=test_run()
        #rolling_mean(df)
        histogram(df['SPY'])

    Plot 'mean' and 'std', Get 'kurtosis' value as well:

    def histogram(df):
        dr = compute_daily_return(df)
        plot_data(dr, title="Daily returns", yLabel="Daily returns")
        dr.hist(bins=20)
        
    
        # Get mean and standard deviation
        mean = dr.mean()
        print("mean=", mean)
        std = dr.std()
        print("std=", std)
    
        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()
    
        # Get kurtosis
        print("kurtosis=", dr.kurtosis())
    
    
    if __name__ == '__main__':
        df=test_run()
        histogram(df['SPY'])

    Now, let see how to plot tow histgram in the same plot:

    def histogram(df):
    
        dr = compute_daily_return(df)
        plot_data(dr, title="Daily returns", yLabel="Daily returns")
    
        dr['SPY'].hist(bins=20, label="SPY") 
        dr['GLD'].hist(bins=20, label="GLD") 
        plt.legend(loc='upper right')
    
        # Get mean and standard deviation
        mean_spy = dr['SPY'].mean()
        mean_gld = dr['GLD'].mean()
    
        std_spy = dr['SPY'].std()
        std_gld = dr['GLD'].std()
    
        plt.axvline(mean_spy, color='w', linestyle='dashed', linewidth=2)
        plt.axvline(std_spy, color='r', linestyle='dashed', linewidth=2)
        plt.axvline(-std_spy, color='r', linestyle='dashed', linewidth=2)
    
        plt.axvline(mean_gld, color='b', linestyle='dashed', linewidth=2)
        plt.axvline(std_gld, color='g', linestyle='dashed', linewidth=2)
        plt.axvline(-std_gld, color='g', linestyle='dashed', linewidth=2)
        plt.show()
    
    if __name__ == '__main__':
        df=test_run()
        histogram(df[['SPY', 'GLD']])

  • 相关阅读:
    Linq组合查询与分页组合查询结合
    jQuery常见用法
    验证码制作
    文件上传,图片加水印
    【2017-06-06】Ajax完整结构、三级联动的制作
    【2017-06-05】Jquery.ajax
    JQuery选择器,动画,事件和DOM操作
    Linq高级查询,分页查询及查询分页结合
    Linq基础+Lambda表达式对数据库的增删改及简单查询
    WebForm文件上传
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8137886.html
Copyright © 2011-2022 走看看