以下内容转自 数析学院,只节选了部分,有需要的同学可以直接查看原文
主要介绍:
- ython 类的基础知识
- 实现短期利率类
- 实现现金流序列类
2、简单的短期汇率类
- import numpy as np
- def discount_factor(r, t):
- ''' Function to calculate a discount factor.
-
- Parameters
- ==========
- r : float
- positive, constant short rate
- t : float, array of floats
- future date(s), in fraction of years;
- e.g. 0.5 means half a year from now
-
- Returns
- =======
- df : float
- discount factor
- '''
- df = np.exp(-r * t)
- # 使用一个 NumPy 通用函数来实现向量化
- return df
- import matplotlib.pyplot as plt
- %matplotlib inline
- # 不同短期利率在5年内的折现因子
- # 标签: disc_fact_1
- t = np.linspace(0, 5)
- for r in [0.01, 0.05, 0.1]:
- plt.plot(t, discount_factor(r, t), label='r=%4.2f' % r, lw=1.5)
- plt.xlabel('years')
- plt.ylabel('discount factor')
- plt.grid(True)
- plt.legend(loc=0)
- class short_rate(object):
- ''' Class to model a constant short rate object.
-
- Parameters
- ==========
- name : string
- name of the object
- rate : float
- positive, constant short rate
-
- Methods
- =======
- get_discount_factors :
- returns discount factors for given list/array
- of dates/times (as year fractions)
- '''
- def __init__(self, name, rate):
- self.name = name
- self.rate = rate
- def get_discount_factors(self, time_list):
- ''' time_list : list/array-like '''
- time_list = np.array(time_list)
- return np.exp(-self.rate * time_list)
- sr = short_rate('r', 0.05)
- sr.name, sr.rate
以上内容转自 数析学院,如需完整内容可以直接查看原文


雷达卡




京公网安备 11010802022788号







