2金融中的线性问题

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
#0.5077431878770808 -0.008481900352462384 0.38455905003823676 0.5227155239089462 0.7036705543221569

2.2套利定价模型

import statsmodels.api as sm
import numpy as np
num_periodes=9
#构建9行*8列的数据
all_values=np.array([np.random.random(8) for i in range(num_periodes)])
y=all_values[:,0]
x=all_values[:,1:]
#在x的第一列加上常数项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
#结果
#x = 20.0
#y = 60.0
#二进制线性优化
import pulp
dealers=['x','y','z'] #3个经纪人
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.]
#p,l,u=linalg.lu(A)

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) #L.L*.x=B,令L*.x=y
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.]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值