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']])

  • 相关阅读:
    第 9 章 类
    导入模块
    第 8 章 函数
    第七章 用户输入和while语句
    第六章 字典
    测试经理/组长职责
    测试的发展之路
    【转】测试流程
    一个网页通用的测试用例(借鉴他人的保存,加注释)
    QTP自动化测试框架简述
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8137886.html
Copyright © 2011-2022 走看看