Skip to content

Commit

Permalink
Added two new Miscellaneous 2D equations
Browse files Browse the repository at this point in the history
  • Loading branch information
zunzun committed Oct 7, 2016
1 parent 52756c0 commit da1215a
Showing 1 changed file with 107 additions and 4 deletions.
111 changes: 107 additions & 4 deletions Models_2D/Miscellaneous.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@




# pyeq3 is a collection of equations expressed as Python classes
#
# Copyright (C) 2013 James R. Phillips
Expand Down Expand Up @@ -2695,3 +2691,110 @@ def SpecificCodeCPP(self):



class NelsonSiegel(pyeq3.Model_2D_BaseClass.Model_2D_BaseClass):

_baseName = "Nelson-Siegel"
_HTML = 'y(m) = B0 + B1*((1-exp(-m/t))/(m/t)) + B2*(((1-exp(-m/t))/(m/t)) - exp(-m/t)))'
_leftSideHTML = 'y(m)'
_coefficientDesignators = ['B0', 'B1', 'B2', 't']
_canLinearSolverBeUsedForSSQABS = False

webReferenceURL = 'https://en.wikipedia.org/wiki/Fixed-income_attribution#Modeling_the_yield_curve'

baseEquationHasGlobalMultiplierOrDivisor_UsedInExtendedVersions = False
autoGenerateOffsetForm = False
autoGenerateReciprocalForm = True
autoGenerateInverseForms = True
autoGenerateGrowthAndDecayForms = True

independentData1CannotContainZeroFlag = False
independentData1CannotContainPositiveFlag = False
independentData1CannotContainNegativeFlag = False
independentData2CannotContainZeroFlag = False
independentData2CannotContainPositiveFlag = False
independentData2CannotContainNegativeFlag = False


def GetDataCacheFunctions(self):
functionList = []
functionList.append([pyeq3.DataCache.DataCacheFunctions.X(NameOrValueFlag=1), []])
return self.extendedVersionHandler.GetAdditionalDataCacheFunctions(self, functionList)


def CalculateModelPredictions(self, inCoeffs, inDataCacheDictionary):
x_in = inDataCacheDictionary['X'] # only need to perform this dictionary look-up once

B0 = inCoeffs[0]
B1 = inCoeffs[1]
B2 = inCoeffs[2]
t = inCoeffs[3]

try:
MoT = x_in/t
expNegMoT = numpy.exp(-MoT)
temp = B0 + B1*((1.0-expNegMoT)/MoT) + B2*(((1.0-expNegMoT)/MoT) - expNegMoT)
return self.extendedVersionHandler.GetAdditionalModelPredictions(temp, inCoeffs, inDataCacheDictionary, self)
except:
return numpy.ones(len(inDataCacheDictionary['DependentData'])) * 1.0E300


def SpecificCodeCPP(self):
s = "\ttemp = B0 + B1*((1.0-exp(-x_in/t))/(x_in/t)) + B2*(((1.0-exp(-x_in/t))/(x_in/t)) - exp(-x_in/t));\n"
return s



class NelsonSiegelSvensson(pyeq3.Model_2D_BaseClass.Model_2D_BaseClass):

_baseName = "Nelson-Siegel-Svensson"
_HTML = 'y(m) = B0 + B1*((1-exp(-m/t))/(m/t)) + B2*(((1-exp(-m/t))/(m/t)) - exp(-m/t)) + B3*(((1-exp(-m/t2))/(m/t2)) - exp(-m/t2))'
_leftSideHTML = 'y(m)'
_coefficientDesignators = ['B0', 'B1', 'B2', 'B3', 't', 't2']
_canLinearSolverBeUsedForSSQABS = False

webReferenceURL = 'https://en.wikipedia.org/wiki/Fixed-income_attribution#Modeling_the_yield_curve'

baseEquationHasGlobalMultiplierOrDivisor_UsedInExtendedVersions = False
autoGenerateOffsetForm = False
autoGenerateReciprocalForm = True
autoGenerateInverseForms = True
autoGenerateGrowthAndDecayForms = True

independentData1CannotContainZeroFlag = False
independentData1CannotContainPositiveFlag = False
independentData1CannotContainNegativeFlag = False
independentData2CannotContainZeroFlag = False
independentData2CannotContainPositiveFlag = False
independentData2CannotContainNegativeFlag = False


def GetDataCacheFunctions(self):
functionList = []
functionList.append([pyeq3.DataCache.DataCacheFunctions.X(NameOrValueFlag=1), []])
return self.extendedVersionHandler.GetAdditionalDataCacheFunctions(self, functionList)


def CalculateModelPredictions(self, inCoeffs, inDataCacheDictionary):
x_in = inDataCacheDictionary['X'] # only need to perform this dictionary look-up once

B0 = inCoeffs[0]
B1 = inCoeffs[1]
B2 = inCoeffs[2]
B3 = inCoeffs[3]
t = inCoeffs[4]
t2 = inCoeffs[5]

try:
MoT = x_in/t
expNegMoT = numpy.exp(-MoT)
MoT2 = x_in/t2
expNegMoT2 = numpy.exp(-MoT2)
temp = B0 + B1*((1.0-expNegMoT)/MoT) + B2*(((1.0-expNegMoT)/MoT) - expNegMoT) + B3*(((1.0-expNegMoT2)/MoT2) - expNegMoT2)
return self.extendedVersionHandler.GetAdditionalModelPredictions(temp, inCoeffs, inDataCacheDictionary, self)
except:
return numpy.ones(len(inDataCacheDictionary['DependentData'])) * 1.0E300


def SpecificCodeCPP(self):
s = "\ttemp = B0 + B1*((1.0-exp(-x_in/t))/(x_in/t)) + B2*(((1.0-exp(-x_in/t))/(x_in/t)) - exp(-x_in/t)) + B3*(((1-exp(-x_in/t2))/(x_in/t2)) - exp(-x_in/t2));\n"
return s

0 comments on commit da1215a

Please sign in to comment.