2.1资本资产定价模型与证券市场线
from scipy import stats
stock_returns=[0.065,0.0265,-0.0593,-0.001,0.0346]
mkt_returns=[0.055,-0.09,-0.041,0.045,0.022]
beta,alpha,r_valuee,p_value,std_err=stats.linregress(stock_returns,mkt_returns)
print beta,alpha,r_valuee,p_value,std_err
2.2套利定价模型
import statsmodels.api as sm
import numpy as np
num_periodes=9
all_values=np.array([np.random.random(8) for i in range(num_periodes)])
y=all_values[:,0]
x=all_values[:,1:]
x=sm.add_constant(x)
results=sm.OLS(y,x).fit()
print results.summary()
print '-'*50
print results.params
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.822
Model: OLS Adj. R-squared: -0.426
Method: Least Squares F-statistic: 0.6587
Date: Wed, 05 Jun 2019 Prob (F-statistic): 0.742
Time: 21:36:50 Log-Likelihood: 6.2428
No. Observations: 9 AIC: 3.514
Df Residuals: 1 BIC: 5.092
Df Model: 7
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const -0.3284 1.420 -0.231 0.855 -18.367 17.710
x1 0.2241 1.059 0.211 0.867 -13.238 13.686
x2 0.8670 4.762 0.182 0.885 -59.644 61.378
x3 -0.1029 1.405 -0.073 0.953 -17.955 17.750
x4 0.8428 0.674 1.251 0.429 -7.719 9.405
x5 -0.0930 0.879 -0.106 0.933 -11.267 11.080
x6 0.3414 0.651 0.525 0.692 -7.926 8.609
x7 -0.3762 4.392 -0.086 0.946 -56.179 55.427
==============================================================================
Omnibus: 9.732 Durbin-Watson: 2.393
Prob(Omnibus): 0.008 Jarque-Bera (JB): 3.626
Skew: -1.384 Prob(JB): 0.163
Kurtosis: 4.417 Cond. No. 96.9
==============================================================================
--------------------------------------------------
[-0.32844643 0.2240503 0.86695489 -0.10287779 0.8428131 -0.09301731
0.34137313 -0.3762296 ]
2.4线性最优化
import pulp
x=pulp.LpVariable('x',lowBound=0)
y=pulp.LpVariable('y',lowBound=0)
problem=pulp.LpProblem('a simple maximization objective',pulp.LpMaximize)
problem += 3*x+2*y,"目标函数"
problem += 2*x+y <=100,"第1个限制条件"
problem += x+y<=80,"第2个限制条件"
problem += x<=40,"第3个限制条件"
problem.solve()
for variable in problem.variables():
print variable.name,'=',variable.varValue
import pulp
dealers=['x','y','z']
variable_costs={'x':500,
'y':350,
'z':450}
fixed_costs={'x':4000,
'y':2000,
'z':6000}
quantites=pulp.LpVariable.dicts('quantity',dealers,lowBound=0,cat=pulp.LpInteger)
is_orders=pulp.LpVariable.dicts('orders',dealers,cat=pulp.LpBinary)
model=pulp.LpProblem('费用最小化问题',pulp.LpMinimize)
model += sum([variable_costs[i]*quantites[i]+fixed_costs[i]*is_orders[i] for i in dealers]),"目标函数"
model += sum([quantites[i] for i in dealers])==150,"第1个限制条件,总和为150"
model += is_orders['x']*30 <= quantites['x'] <= is_orders['x']*100,'数量限制x'
model += is_orders['y']*30 <= quantites['y'] <= is_orders['y']*90,'数量限制y'
model += is_orders['z']*30 <= quantites['z'] <= is_orders['z']*70,'数量限制z'
model.solve()
for variable in model.variables():
print variable.name,'=',variable.varValue
print '总共花费:',pulp.value(model.objective)
orders_x = 0.0
orders_y = 1.0
orders_z = 1.0
quantity_x = 0.0
quantity_y = 90.0
quantity_z = 60.0
总共花费: 66500.0
2.5使用矩阵解线性方程组
import numpy as np
A=np.array([[2,1,1],
[1,3,2],
[1,0,0]])
B=np.array([4,5,6])
print np.linalg.solve(A,B)
[ 6. 15. -23.]
2.6LU分解
import scipy.linalg as linalg
import numpy as np
A=np.array([[2.,1.,1.],
[1.,3.,2.],
[1.,0.,0.]])
B=np.array([4.,5.,6.])
LU=linalg.lu_factor(A)
x=linalg.lu_solve(LU,B)
print(LU)
print(x)
(array([[ 2. , 1. , 1. ],
[ 0.5, 2.5, 1.5],
[ 0.5, -0.2, -0.2]]), array([0, 1, 2], dtype=int32))
[ 6. 15. -23.]
2.7Cholesky分解
import numpy as np
A=np.array([[10.,-1.,2.,0.],
[-1.,11.,-1.,3.],
[2.,-1.,10.,-1.],
[0.,3.,-1.,8.]])
B=np.array([6.,25.,-11.,15.])
L=np.linalg.cholesky(A)
y=np.linalg.solve(L,B)
x=np.linalg.solve(L.T.conj(),y)
print(x)
[ 1. 2. -1. 1.]
2.8QR分解
import scipy.linalg as linald
import numpy as np
A=np.array([[2.,1.,1.],
[1.,3.,2.],
[1.,0.,0.]])
B=np.array([4.,5.,6.])
Q,R=linalg.qr(A)
y=np.dot(Q.T,B)
x=linalg.solve(R,y)
print(x)
[ 6. 15. -23.]