9 篇文章 0 订阅
39 篇文章 0 订阅

# python金融正太var-协方差外部传参

import sys
import numpy as np
import scipy.stats as st
from pandas import DataFrame

# 设P0为固值
P0 = 1000

# 到时候外部传参下边可以用这段进行（这是ziliao的参数）
# 6.8272,6.8474,6.8647,6.8925,6.8855,6.9116,6.9180,6.9179,6.9143,6.9192,6.9215,6.9318,6.9501,6.9440,6.9363,6.9413,6.9611,6.9680,6.9535,6.9366,6.9735,6.9809

def aaa(arf,N,ziliao):
# print(ziliao)
#这个时候因为ziliao传入进来，是"6",".","8","2"这种形式，所以先对列表进行整合
sum1 = ''
for i in ziliao:
sum1+=i
# 这个时候sum1是一个字符串，内容包含6.8272,6.8474,6.8647,6.8925,6.8855,6.9116,6.9180,6.9179,6.9143,6.9192,6.9215,6.9318,6.9501,6.9440,6.9363,6.9413,6.9611,6.9680,6.9535,6.9366,6.9735,6.9809这些，所以进行切割
# 同时为了方便还是取“ziliao”这个变量名，这个时候“ziliao”的内容为["6.8272","6.8474"]这些
ziliao = sum1.split(",")

# 为了让ziliao的内容变为[6.8272,6.8474],再用一次for循环（这是我笨人的笨方法..）
sum=[]
for i in ziliao:
i = float(i)
sum.append(i)
# print(sum)
ziliao = sum

ziliao_return = DataFrame(ziliao)

# 股票日收益
stock_return = np.log(ziliao_return/ziliao_return.shift(1))

# 删除缺失行
stock_return = stock_return.dropna()
# print(stock_return)
a = stock_return.loc[:]
# print(a)
# a_mean为平均收益率  b为保留小数点后四位的a_mean
a_mean =a.mean()
b = "%.4f" % a_mean
b = float(b)
# a_mean = "%.2f%%" % (a.mean()*100)

# p是置信水平在不同情况下（0.9或者0.95或者0.99）的平均收益率
p = np.percentile(a, (1, 5, 10), interpolation='midpoint')

if arf ==0.95:
p = np.abs(p[1])
elif arf==0.90 or arf==0.9:
p = np.abs(p[0])
elif arf==0.99:
p = np.abs(p[2])
# print(p)

# z是置信水平在不同情况下（0.9或者0.95或者0.99）的取值
z = st.norm.ppf(q=1 - arf)
# z取绝对值
z = np.abs(z)
# print(z)

# t是持有天数
t = np.sqrt(N)

# print(b,p,z,t)
return b,p,z,t

# a1,a2,a3是外部传参，a1传参为列表
a2 = float(sys.argv[2])
# print(a2)
a3 = float(sys.argv[3])
# print(a3)
a1 = list(sys.argv[1])
# print(a1)

ttt = list(aaa(a2,a3,a1))
# print(ttt)
# print(ttt[0])
var_N = ttt[3]*P0*(ttt[2]*ttt[1]-float(ttt[0]))
print(var_N)

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

02-28 3万+

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客

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