Python金融大数据分析——第15章 估值框架 笔记

第15章 估值框架

15.1 资产定价基本定理

15.1.1 简单示例

15.1.2 一般结果

15.2 风险中立折现

15.2.1 日期建模和处理

import datetime as dt

dates = [dt.datetime(2018, 1, 1), dt.datetime(2018, 7, 1), dt.datetime(2019, 1, 1)]

(dates[1] - dates[0]).days / 365
# 0.4958904109589041

(dates[2] - dates[1]).days / 365
# 0.5041095890410959

fractions = [0.0, 0.5, 1.0]
# dates 和 fraction 定义(大致)等价

import numpy as np


# 获取年分数
def get_year_deltas(date_list, day_count=365.):
    """
    Return vector of floats with day deltas in years.
    Initial value normalized to zero.
    :param date_list: list or array
    collection of datetime objects
    :param day_count: float
    number of days for a year
    :return:
    delta_list:array
    year fractions
    """
    start = date_list[0]
    delta_list = [(date - start).days / day_count for date in date_list]
    return np.array(delta_list)


import datetime as dt

dates = [dt.datetime(2018, 1, 1), dt.datetime(2018, 7, 1), dt.datetime(2019, 1, 1)]
get_year_deltas(dates)
# array([0.        , 0.49589041, 1.        ])

15.2.2 固定短期利率

我们将焦点放在短期利率折现的最简单情况——也就是短期利率一直保持不变的情况。 许多期权定价模型, 如Black-Scholes-Merton 、 Merton 和 Cox-Ross-Rubinstein 都做出这个假设。我们假定连续折现,因为这是期权定价应用中常见的假设。在这种情况下,在给定未来日期 t 和固定短期利率 r 下,今天的一般折现因子可以由 D0(t)=ert 得出。 当然,在结束日有一个特例D0(T)=ert。注意,t 和 T 都是年分数。

折现因子也可以解释为分别在 t 和 T 到期的单位零息债券(ZCB)的当日价值。给定两个日期 ts0,从 t 到 s 的折现因子可以由公式 Ds(t)=D0(t)/D0(s)=ert/ers=er(ts) 给出。

固定短期利率下的风险中立折现类

class constant_short_rate(object):
    """
    Class for constant short rate discounting
    """

    def __init__(self, name, short_rate):
        """

        :param name:string
         name of the object
        :param short_rate:float(positive)
         constant rate for discounting
        """
        self.name = name
        self.short_rate = short_rate
        if short_rate < 0:
            raise ValueError('Short rate negative.')

    def get_discount_factors(self, date_list, dtobjects=True):
        """
        get discount factors given a list/array of datetime objects or year fractions
        """
        if dtobjects is True:
            dlist = get_year_deltas(date_list)
        else:
            dlist = np.array(date_list)
        dflist = np.exp(self.short_rate * np.sort(-dlist))
        return np.array((date_list, dflist)).T


import datetime as dt

dates = [dt.datetime(2018, 1, 1), dt.datetime(2018, 7, 1), dt.datetime(2019, 1, 1)]
csr = constant_short_rate('csr', 0.05)
csr.get_discount_factors(dates)
# array([[datetime.datetime(2018, 1, 1, 0, 0), 0.951229424500714],
#        [datetime.datetime(2018, 7, 1, 0, 0), 0.9755103387657228],
#        [datetime.datetime(2019, 1, 1, 0, 0), 1.0]], dtype=object)

deltas = get_year_deltas(dates)
csr.get_discount_factors(deltas, dtobjects=False)
# array([[0.        , 0.95122942],
#        [0.49589041, 0.97551034],
#        [1.        , 1.        ]])

15.3 市场环境

建立由常量、 列表和曲线组成的市场环境模型的类

# 建立由常量、 列表和曲线组成的市场环境模型的类
class market_environment(object):
    def __init__(self, name, pricing_date):
        self.name = name
        self.pricing_date = pricing_date
        self.constants = {}
        self.lists = {}
        self.curves = {}

    def add_constant(self, key, constant):
        self.constants[key] = constant

    def get_constant(self, key):
        return self.constants[key]

    def add_list(self, key, list_object):
        self.lists[key] = list_object

    def get_list(self, key):
        return self.lists[key]

    def add_curve(self, key, curve):
        self.curves[key] = curve

    def get_curve(self, key):
        return self.curves[key]

    def add_environment(self, env):
        for key in env.constants:
            self.constants[key] = env.constants[key]
        for key in env.lists:
            self.lists[key] = env.lists[key]
        for key in env.curves:
            self.curves[key] = env.curves[key]


import datetime as dt

dates = [dt.datetime(2018, 1, 1), dt.datetime(2018, 7, 1), dt.datetime(2019, 1, 1)]
csr = constant_short_rate('csr', 0.05)
me_1 = market_environment('me_1', dt.datetime(2018, 1, 1))
me_1.add_list('symbols', ['AAPL', 'MSFT', 'FB'])
me_1.get_list('symbols')
# ['AAPL', 'MSFT', 'FB']

me_2 = market_environment('me_2', dt.datetime(2018, 1, 1))
me_2.add_constant('volatility', 0.2)
me_2.add_curve('short_rate', csr)
me_2.get_curve('short_rate')
# <__main__.constant_short_rate at 0x1a226781cc0>

me_1.add_environment(me_2)
me_1.get_curve('short_rate')
# <__main__.constant_short_rate at 0x1a226781cc0>

me_1.constants
# {'volatility': 0.2}

me_1.lists
# {'symbols': ['AAPL', 'MSFT', 'FB']}

me_1.curves
# {'short_rate': <__main__.constant_short_rate at 0x1a226781cc0>}

me_1.get_curve('short_rate').short_rate
# 0.05

这一章节的内容比较少,这里只记录了一下Python代码 ^_^

展开阅读全文

没有更多推荐了,返回首页