0

我正在尝试使用包含不同选项数据的 pandas 数据框来计算隐含波动率。对于隐含波动率,我使用mibian。这是代码:

optionsData.to_dict():

    {'callclose': {0: Decimal('4'),
      1: Decimal('2.62'),
      2: Decimal('2.64'),
      3: Decimal('1.7'),
      4: Decimal('1.35')},
     'daystoexpiration': {0: 43L, 1: 43L, 2: 43L, 3: 43L, 4: 43L},
     'expiration': {0: datetime.date(2013, 2, 16),
      1: datetime.date(2013, 2, 16),
      2: datetime.date(2013, 2, 16),
      3: datetime.date(2013, 2, 16),
      4: datetime.date(2013, 2, 16)},
     'impvol': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan},
     'putclose': {0: Decimal('0.54'),
      1: Decimal('0.65'),
      2: Decimal('0.76'),
      3: Decimal('1.08'),
      4: Decimal('1.56')},
     'strike': {0: Decimal('39'),
      1: Decimal('40'),
      2: Decimal('41'),
      3: Decimal('42'),
      4: Decimal('43')},
     'symbol': {0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'A'},
     'underlyingclose': {0: Decimal('42.86'),
      1: Decimal('42.86'),
      2: Decimal('42.86'),
      3: Decimal('42.86'),
      4: Decimal('42.86')}}

optionsData = optionsData.T

def calcvol(info):
    print info.name
    print 'Starting procedure.'
    tempmb = mb.BS([info['underlyingclose'], 
            info['strike'], 
            .25, 
            info['daystoexpiration']],
           callPrice=float(info['callclose']), 
           putPrice=info['putclose'])
    print 'mb created'
    impvol = tempmb.impliedVolatility
    print 'implied vol calculated'
    info['impvol'] = impvol
    print 'impvol set'
    del impvol, tempmb
    print 'vars deleted'
    return info

a = optionsData.apply(calcvol)

当我运行所有这些时,它在 optionsData 中的第一个元素上设置 impvol,但似乎随后给了我这个错误:

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-9-116f3c010b9c> in <module>()
----> 1 a = optionsData.apply(calcvol)

C:\Python27\lib\site-packages\pandas-0.10.0-py2.7-win32.egg\pandas\core\frame.pyc in apply(self, func, axis, broadcast, raw, args, **kwds)
   4079                     return self._apply_raw(f, axis)
   4080                 else:
-> 4081                     return self._apply_standard(f, axis)
   4082             else:
   4083                 return self._apply_broadcast(f, axis)

C:\Python27\lib\site-packages\pandas-0.10.0-py2.7-win32.egg\pandas\core\frame.pyc in _apply_standard(self, func, axis, ignore_failures)
   4154                     # no k defined yet
   4155                     pass
-> 4156                 raise e
   4157 
   4158         if len(results) > 0 and _is_sequence(results[0]):

ZeroDivisionError: ('float division by zero', u'occurred at index 1')

0
Starting procedure.
mb created
implied vol calculated
impvol set
vars deleted
0
Starting procedure.
mb created
implied vol calculated
impvol set
vars deleted
1
Starting procedure.

我必须错过一些简单的东西。当我将它们传递给 mibian 时,我尝试将每个值包装在 float() 中,但仍然遇到同样的问题。我非常感谢任何指导。

此外,如果您知道使用数据框计算隐含波动率的更有效方法,我会全力以赴。

4

1 回答 1

1

正如海登评论的那样,我确实单独尝试了该功能,但样本量不够大。

显然,对于这些变量的某些值,它将接近于零。我更改了我的calcvol函数以尝试使用 mibian 来获取波动性,如果 aZeroDivisionError被捕获则设置impvolNaN. 这将帮助我弄清楚是哪些人引起了这种骚动。

于 2013-01-06T17:50:49.333 回答