我正在使用Mibian模块来计算通话选项。我有一个三个嵌套列表的列表。每个嵌套列表代表执行价格。每个嵌套列表都有自己的相应日期到期,即my_list [2]还剩30天。
import mibian as mb
import pandas as pd
my_list = [[20, 25, 30, 35, 40, 45],
[50, 52, 54, 56, 58, 60, 77, 98, 101],
[30, 40, 50, 60]]
days_left = [5, 12, 30]
my_list[2]
[30, 40, 50, 60]
days_left[2]
30
用于计算看涨期权的Mibian Black-Scholes代码的结构。
mb.BS([stock price, strike price, interest rate, days to maturity], volatility)
data1 = dict()
for x, sublist in enumerate(my_list):
data1[x] = option3 = []
for i in sublist:
c = mb.BS([120, i, 1, 20], 10)
option3.append(c.callPrice)
给出一个包含3个列表的字典输出,调用价格基于my_list中三个嵌套列表中的每一个。
data1
{0: [100.01095590221843,
95.013694877773034,
90.016433853327641,
85.019172828882233,
80.021911804436854,
75.024650779991447],
1: [70.027389755546068,
68.028485345767905,
66.029580935989742,
64.030676526211579,
62.03177211643343,
60.032867706655267,
43.042180223540925,
22.05368392087027,
19.055327306203068],
2: [90.016433853327641,
80.021911804436854,
70.027389755546068,
60.032867706655267]}
我想要的是嵌套列表和要一起迭代的日期
我想使用字典创建与上面相同的内容,但不仅会迭代my_list,还会按顺序迭代days_left。
我通过new_list = list(zip(days_left,my_list))尝试了一个zip列表,但它给了我一个错误。有人可以帮忙吗?非常感谢。
new_list = list(zip(my_list, days_left))
[([20, 25, 30, 35, 40, 45], 5),
([50, 52, 54, 56, 58, 60, 77, 98, 101], 12),
([30, 40, 50, 60], 30)]
data5 = dict()
for x, days_left, my_list in enumerate(new_list):
data5[x] = option5 = []
for days_left, my_list in new_list:
c = mb.BS([120, my_list, 1, days_left ], 10)
option5.append(c.callPrice)
对于像my_list [2]这样的单个嵌套列表。输出是:
range_list = list(range(1))
data2 = dict()
for x in range_list:
data2[x] = option2 = []
for i in my_list[2]:
c = mb.BS([120, i, 1, 30 ], 10)
option2.append(c.callPrice)
option2
[90.024647403788975,
80.032863205051967,
70.041079006314973,
60.049294807577965]
值类似,但与data1 [2]中的值不同。理想的输出应该与data1具有相同的结构,有三个字典,但由于days_left,值略有不同。差异可能看起来微不足道,但后来,我必须将它们乘以100,这样才会产生差异。
答案 0 :(得分:1)
我认为这可以满足您的需求。请注意,大多数情况都是试图模拟您的环境 - 您只关心最后几行。
也就是说,由连续数字索引的数据结构不应该是一个字典,它应该是一个列表。 ; - )
Magic_numbers = [
100.01095590221843,
95.013694877773034,
90.016433853327641,
85.019172828882233,
80.021911804436854,
75.024650779991447,
70.027389755546068,
68.028485345767905,
66.029580935989742,
64.030676526211579,
62.03177211643343,
60.032867706655267,
43.042180223540925,
22.05368392087027,
19.055327306203068,
90.016433853327641,
80.021911804436854,
70.027389755546068,
60.032867706655267,
]
Magic_index = 0
def mb(details, volatility):
class C:
def __init__(self, n):
self.callPrice = n
global Magic_index
result = C(Magic_numbers[Magic_index])
Magic_index += 1
return result
mb.BS = mb
strike_prices = [
[20, 25, 30, 35, 40, 45],
[50, 52, 54, 56, 58, 60, 77, 98, 101],
[30, 40, 50, 60]
]
days_left = [5, 12, 30]
data99 = {} # This is silly. A dict indexed by sequential numbers should be a list.
for i, (days, prices) in enumerate(zip(days_left, strike_prices)):
data99[i] = [mb.BS([120, price, 1, days], 10).callPrice for price in prices]
import pprint
pprint.pprint(data99)
输出如下:
{0: [100.01095590221843,
95.01369487777303,
90.01643385332764,
85.01917282888223,
80.02191180443685,
75.02465077999145],
1: [70.02738975554607,
68.0284853457679,
66.02958093598974,
64.03067652621158,
62.03177211643343,
60.03286770665527,
43.042180223540925,
22.05368392087027,
19.05532730620307],
2: [90.01643385332764,
80.02191180443685,
70.02738975554607,
60.03286770665527]}
答案 1 :(得分:0)
我认为答案可能很简单:
for x, (days_left, my_list) in enumerate(new_list):
data5[x] = option5 = []
for days_left, my_list in new_list:
c = mb.BS([120, my_list, 1, days_left ], 10)
option5.append(c.callPrice)
由于enumerate
的输出格式为(i, x)
,因此在这种情况下,x
是一个元组(即(i, (x, y))
)。