统计学基础_13描述性统计
1. 書籍和文中所提到的數(shù)據(jù)會在文末提供百度云下載,所有數(shù)據(jù)都不會有加密,可以放心下載使用
2. 文中計(jì)算的結(jié)果與書中不同是由于數(shù)據(jù)使用的時(shí)間段不同
目錄
1. 準(zhǔn)備數(shù)據(jù)
2. 繪制頻數(shù)直方圖
?3. 數(shù)據(jù)的位置
4. 數(shù)據(jù)的離散度
1. 準(zhǔn)備數(shù)據(jù)
原始數(shù)據(jù) 600000.csv 600050.csv 601398.csv 三個(gè)文件里是收盤價(jià),通過收盤價(jià)計(jì)算日收益率
import pandas as pd # 中國工商銀行 ICBC 601398 # 浦發(fā)銀行 SPDB 600000 # 中國聯(lián)通 ChinaUnicom 600050 df_601398 = pd.read_csv('./601398.csv',encoding='utf-8') df_600000 = pd.read_csv('./600000.csv',encoding='utf-8') df_600050 = pd.read_csv('./600050.csv',encoding='utf-8') df_601398.head() df_601398['o_date'] = df_601398['tradeDate'] df_601398['o_date'] = pd.to_datetime(df_601398['o_date']) df_601398.sort_values(by='o_date',ascending=True,inplace=True) # daily_returns 日收益率 df_601398['daily_returns'] = (df_601398['closePrice']-df_601398['closePrice'].shift(1))/df_601398['closePrice'].shift(1) df_601398.head() df_600000['o_date'] = df_600000['tradeDate'] df_600000['o_date'] = pd.to_datetime(df_600000['o_date']) df_600000.sort_values(by='o_date',ascending=True,inplace=True) df_600000['daily_returns'] = (df_600000['closePrice']-df_600000['closePrice'].shift(1))/df_600000['closePrice'].shift(1)df_600050['o_date'] = df_600050['tradeDate'] df_600050['o_date'] = pd.to_datetime(df_600050['o_date']) df_600050.sort_values(by='o_date',ascending=True,inplace=True) df_600050['daily_returns'] = (df_600050['closePrice']-df_600050['closePrice'].shift(1))/df_600050['closePrice'].shift(1)# 中國工商銀行 gsyh df_601398.rename(columns={'daily_returns':'gsyh'},inplace=True) # 浦發(fā)銀行 pfyh df_600000.rename(columns={'daily_returns':'pfyh'},inplace=True) # 中國聯(lián)通 zglt df_600050.rename(columns={'daily_returns':'zglt'},inplace=True)df_601398 = df_601398.loc[:,['tradeDate','gsyh']] df_600000 = df_600000.loc[:,['tradeDate','pfyh']] df_600050 = df_600050.loc[:,['tradeDate','zglt']]合并到一個(gè)表中,按日期對齊
three_df = pd.merge(df_601398,df_600000,how='inner',on='tradeDate') three_df = pd.merge(three_df,df_600050,how='inner',on='tradeDate') three_df.head() three_df.to_csv('chapter_13_dailyreturns.csv',encoding='utf-8')2. 繪制頻數(shù)直方圖
import matplotlib.pyplot as plt # 頻數(shù)直方圖 gsyh = three_df.gsyh plt.hist(gsyh)?
?3. 數(shù)據(jù)的位置
概念:
樣本平均數(shù)(Sample Mean)、中位數(shù)(Median)、眾數(shù)(Mode)、百分位數(shù)(Percentile)
# 平均數(shù) three_df.zglt.mean() # out: 0.0004284562650822751# 中位數(shù) three_df.zglt.median() # out: 0.0# 眾數(shù) three_df.zglt.mode() # out:0 0.0 # dtype: float64# 上下四分位數(shù) [three_df.zglt.quantile(i) for i in [0.25,0.75]] # out: [-0.010317115551694248, 0.010245151847786325]4. 數(shù)據(jù)的離散度
概念:
4.1 極差(Range)
4.2 平均絕對偏差(Mean Absolute Deviation)
4.3 方差(Variance) 和標(biāo)準(zhǔn)差(Standard Deviation)
方差公式:
?標(biāo)準(zhǔn)差公式:
# 極差 three_df.zglt.max()-three_df.zglt.min() # out: 0.2016438874062682# 平均絕對偏差 three_df.zglt.mad() # out: 0.015984757661984345# 方差 three_df.zglt.var() # out: 0.0005796109187107064# 標(biāo)準(zhǔn)差 three_df.zglt.std() # out: 0.024075109941819713PS:
鏈接:https://pan.baidu.com/s/1Jck5WqDunfyph18UvguJ5g?
提取碼:5fo3
書籍
鏈接:https://pan.baidu.com/s/1xJD85-LuaA9z-Jy_LU5nOw?
提取碼:ihsg
總結(jié)
以上是生活随笔為你收集整理的统计学基础_13描述性统计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python产生随机整数数组_生成随机整
- 下一篇: 如何使用计算机语言画经纬网,使用R语言绘