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

[Python] Histograms for analysis Daily return的更多相关文章

  1. [Python] Scatter Plot for daily return

    Sploe = 2: means that SPY move up 1, ABC move up 2 Correlation: how close those dots close to the li ...

  2. 数据分析---《Python for Data Analysis》学习笔记【03】

    <Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...

  3. 《python for data analysis》第九章,数据聚合与分组运算

    # -*- coding:utf-8 -*-# <python for data analysis>第九章# 数据聚合与分组运算import pandas as pdimport nump ...

  4. 数据分析---《Python for Data Analysis》学习笔记【04】

    <Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...

  5. 数据分析---《Python for Data Analysis》学习笔记【02】

    <Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...

  6. 数据分析---《Python for Data Analysis》学习笔记【01】

    <Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...

  7. 《python for data analysis》第十章,时间序列

    < python for data analysis >一书的第十章例程, 主要介绍时间序列(time series)数据的处理.label:1. datetime object.time ...

  8. 《python for data analysis》第七章,数据规整化

    <利用Python进行数据分析>第七章的代码. # -*- coding:utf-8 -*-# <python for data analysis>第七章, 数据规整化 imp ...

  9. 《python for data analysis》第五章,pandas的基本使用

    <利用python进行数据分析>一书的第五章源码与读书笔记 直接上代码 # -*- coding:utf-8 -*-# <python for data analysis>第五 ...

随机推荐

  1. windows2008搭建ftp服务器

    控制面板 操作—〉添加角色 (web,文件服务) windows 搜索中: 添加ftp站点: 计算机---管理 –服务器管理  --- 添加一个用户 ,密码. 防护墙高级设置中添加 出入站规则 允许2 ...

  2. Thread Control Block

    Thread Control Block The following is the declaration of the Thread Control Block. struct tcb { u32_ ...

  3. iptables 简单介绍及应用 Linux防火墙

    iptables 即 Linux防火墙 的简单介绍及使用 iptables生效位置如下图: 其中, 网络防火墙也可以使用一台启用了iptables的Linux主机代替; 路由器或集线器等设施在拓扑中省 ...

  4. angular7升级到angular8

    1.首先我们对:angular的命令的安装 ng install -g @angular/cli的安装则会升级到最新的版本,并且再次创建项目的时候,我们就能够使用ng version查看到已经是最新的 ...

  5. JZOJ5787轨道(容斥+DP)

    JZOJ5787轨道 Description 2018年1月31日,152年一遇的超级大月全食在中国高空出现(没看到的朋友真是可惜),小B看到月食,便对月球的轨道产生了兴趣.他上网查重力加速度的公式, ...

  6. linux进程管理之进程创建(三)

    在linux系统中,许多进程在诞生之初都与其父进程共同用一个存储空间.但是子进程又可以建立自己的存储空间,并与父进程“分道扬镳”,成为与父进程一样真正意义上的进程. linux系统运行的第一个进程是在 ...

  7. ActiveMQ maven

    http://outofmemory.cn/java/mq/apache-activemq-demo

  8. hadoop-10-创建yum资源库

    hadoop-10-创建yum资源库 1,在/etc/yum.repos.d/下面创建 ambari.repo  HDP.repo  HDP-UTILS.repo 三个文件: [root@server ...

  9. jquery-ui日期时间控件实现

    日期控件和时间控件为独立控件,日期时间控件要同一时候导入日期控件和时间控件的js,然后在日期控件加入时间控件显示參数,没有导入时间控件js.日期控件函数设置的时间控件參将包错 日期控件官网网址:htt ...

  10. Dubbo分布式服务框架入门(附project)

    要想了解Dubbo是什么,我们不防先了解它有什么用. 使用场景:比方我想开发一个网上商城项目.这个网上商城呢,比較复杂.分为pc端web管理后台.微信端销售公众号,那么我们分成四个项目,pc端站点,微 ...