[Python] bootstrapping and forward curve生成python范例 →→→→→进入此内容的聊天室

来自 , 2020-12-29, 写在 Python, 查看 103 次.
URL http://www.code666.cn/view/009a5510
  1. #
  2. # Description: example of a bootstrapping and forward curve generation
  3. # script, this can be used to build a set of curves for different currencies
  4. # TODO: include some spline smoothing to the zero curve, from first principles!
  5. #
  6.  
  7. from sympy.solvers import solve
  8. from sympy import Symbol, abs, Real
  9. x = Symbol('x', real=True)
  10.  
  11. import pylab as pylab
  12.  
  13. def g(yieldCurve, zeroRates,n, verbose):
  14.     '''
  15.        generates recursively the zero curve
  16.        expressions eval('(0.06/1.05)+(1.06/(1+x)**2)-1')
  17.        solves these expressions to get the new rate
  18.        for that period
  19.    
  20.    '''
  21.     if len(zeroRates) >= len(yieldCurve):
  22.         print "\n\n\t+zero curve boot strapped [%d iterations]" % (n)
  23.         return
  24.     else:
  25.         legn = ''
  26.         for i in range(0,len(zeroRates),1):
  27.             if i == 0:
  28.                 legn = '%2.6f/(1+%2.6f)**%d'%(yieldCurve[n], zeroRates[i],i+1)
  29.             else:
  30.                 legn = legn + ' +%2.6f/(1+%2.6f)**%d'%(yieldCurve[n], zeroRates[i],i+1)
  31.         legn = legn + '+ (1+%2.6f)/(1+x)**%d-1'%(yieldCurve[n], n+1)
  32.         # solve the expression for this iteration
  33.         if verbose:
  34.             print "-[%d] %s" % (n, legn.strip())
  35.         rate1 = solve(eval(legn), x)
  36.         # Abs here since some solutions can be complex
  37.         rate1 = min([Real(abs(r)) for r in rate1])
  38.         if verbose:
  39.             print "-[%d] solution %2.6f" % (n, float(rate1))
  40.         # stuff the new rate in the results, will be
  41.         # used by the next iteration
  42.         zeroRates.append(rate1)
  43.         g(yieldCurve, zeroRates,n+1, verbose)
  44.        
  45. verbose = True
  46. tenors = [.1,.25,0.5,1,2,3,5,7,10,20,30]
  47. #
  48. # money market, futures, swap rates
  49. #
  50. yieldCurve = [0.07,     0.09,   0.15,   0.21,   0.37,   0.57,   1.13,   1.70,   2.31,   3.08    ,3.41]
  51.  
  52. #yieldCurve = [0.05, 0.06, 0.07, 0.08 ,0.085 ,0.0857 ,0.0901,0.0915,0.0925,0.0926,0.0934,0.0937]
  53. zeroRates = [yieldCurve[0]] # TODO: check that this is the correct rate
  54.  
  55. print "\n\n\tAlexander Baker, March 2012\n\tYield Curve Bootstrapper\n\tAlexander Baker\n\n"
  56.  
  57. # kick off the recursive code
  58. g(yieldCurve, zeroRates, 1, verbose)
  59. print "\tZeroRate Array",zeroRates
  60.  
  61. pylab.plot(tenors,yieldCurve)
  62. pylab.plot(tenors,zeroRates)
  63. pylab.show()
  64. ## end of http://code.activestate.com/recipes/578257/ }}}
  65.  
  66. #//python/5034

回复 " bootstrapping and forward curve生成python范例"

这儿你可以回复上面这条便签

captcha