Python for Finance——入门到放弃

Python for Finance——入门到放弃

这个是这本书的最后一篇笔记了。。。我看的是第一版的,2013年出的书,太老了,很多代码都运行不下去,所以打算换本新一点的书看。本篇内容是Chapter 8 开头部分。主要介绍了pandas和statsmodels包。

使用pandas和statsmodels

使用Pandas生成一个数据框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import numpy as np
import pandas as pd
dates = pd.date_range('20130101', periods = 5)
dates
np.random.seed(12345)
x = pd.DataFrame(np.random.rand(5, 2), index = dates, columns = ('A', 'B'))
x
x.describe() # 显示x的每一列的描述性统计量
y = pd.Series([0.1, 0.02, -0.03, np.nan, 0.130, 0.125]) # y contains a NA
y
y.mean()
y.fillna(y.mean()) # fillup y's NA with its mean value
Out[12]:
0 0.100
1 0.020
2 -0.030
3 0.069
4 0.130
5 0.125
dtype: float64

statsmodels使用示例——OLS

1
2
3
4
5
6
7
8
9
import statsmodels.api as sm
import numpy as np
y = [1, 2, 3, 4, 2, 3, 4]
x = range(1, 8) # x is from 1 to 7
x = sm.add_constant(x) # 给x添加一列全为1的列,同时x又range变成ndarray
x
r = sm.OLS(y, x).fit() # OLS
r.params
Out[13]: array([ 1.28571429, 0.35714286])

数据录入

从剪贴板获取数据

1
2
import pandas as pd
data = pd.read_clipboard() # 从剪贴板上读取数据(在此之前我复制了平安银行的数据)

从txt文件中读取数据

1
2
3
import pandas as pd
x = pd.read_table('ff_month.txt')
x

从Excel中读取数据

1
2
3
import pandas as pd
infile=pd.ExcelFile('pingan.xlsx') # 这里可能会出错,需要安装xlrd
x=infile.parse('Sheet1',header=0)

从csv中读取数据

1
2
import pandas as pd
f = pd.read_csv('pingan.csv')

##收益率的计算

1
2
3
4
5
6
7
import numpy as np
p = np.array([1, 1.1, 0.9, 1.05])
print(p[1:]) # 去掉第一个
print(p[:-1]) # 去掉最后一个
ret = (p[1:]-p[:-1])/p[:-1]
[ 1.1 0.9 1.05]
[ 1. 1.1 0.9]

例如绘制平安银行的收益率分布图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
import tushare as ts
from matplotlib.pyplot import *
from matplotlib.finance import *
import matplotlib.mlab as mlab
from matplotlib.font_manager import *
pingan = ts.get_k_data('000001')
close = np.array(pingan.close)
p_ret = (close[1:]-close[:-1])/close[:-1]
myfont = FontProperties(fname='/Library/Fonts/Songti.ttc')
matplotlib.rcParams['axes.unicode_minus'] = False
[n, bins, patches] = hist(p_ret, 100)
mu = np.mean(p_ret)
sigma = np.std(p_ret)
x = mlab.normpdf(bins, mu, sigma)
plot(bins, x, color = 'red', lw = 2)
title(u"平安银行收益率分布", fontproperties = myfont)
xlabel(u"收益率", fontproperties = myfont)
ylabel(u"频率", fontproperties = myfont)
rcParams['savefig.dpi'] = 800
savefig('20180624a1.png')
show()

平安银行股票收益分布

# Python

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了607.9k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×