[R-pkg-devel] Reference Classes
Glenn Schultz
glennmschultz at me.com
Mon Aug 22 00:31:39 CEST 2016
All,
My package BondLab is written in S4. I need to build recursive tree to create human readable mortgage payment waterfalls in Bond Lab. I am simply asking if the below idea is doable
The current waterfall implementation is a list of lists in a dataframe and payment rules are iterated over the data frame with a call to source which is the script used to allocate principal and interest (standard practice in MBS structured finance) This works fine but suffers from two drawbacks:
1) The source script is bespoke to the deal.
2) The source script is not human readable - in this case human readable means intuitive. That is without examining/following procedural code.
So, I think a recursive tree is better.
I have tested this idea on data.tree and it works well. However, data.tree uses R6, which lives in the S3 world and forcing S4 classes into S3 does not seem a particularly good idea. My research thus far has led me to believe that I can use S4 ReferenceClass to build a recursive tree which whose nodes (leafs?) would contain a class I have created called TrancheDetails and whose root is always “Deal".
For example, I have dput below a class TrancheDetails which is one of 5 tranches classes belonging to FNMA 2016-53 - each with unique payment rules. I am thinking something along the lines of the following. Tranche is a leaf and Deal is the root (I am still learning about trees). With this scheme I can now create methods whose functional representation is the waterfall payment rules. I have done this successfully with data.tree on a mock deal set-up using the package. The real goal is to be able to load TrancheDetails which is the complete data representation of the Tranche (investor bond) within a REMIC with methods for principal and interest allocation.
Before I go down this road is my understanding of ReferenceClass correct and can I build a recursive tree with ReferenceClasses? I am very close to the dream of creating open source analytics and human readable waterfalls for mortgage and asset backed securities and I know the answer lies in R I just have to unlock it.
My initial thoughts:
setRefClass("Tranche",
contains = "TrancheDetails")
setRefClass("Deal",
contains = "Tranche")
methods to initialize the tree
new("TrancheDetails"
, DealName = "FNMA2016053"
, TrancheNumber = "1"
, NumberofComponents = 0
, ComponentofTranches = "0"
, TrancheName = "053-AF"
, TranchePrincipal = "pass-through"
, TranchePrincipalDesc = "PT"
, TrancheInterestDesc = "FLT"
, TrancheOtherDescription = ""
, Cusip = "3136AS4X1"
, TrancheOrigBal = 38400349
, TrancheInterest = "FLT"
, TrancheCoupon = 0.95
, AccrualRate = 0
, TreasuryMaturity = 0
, TreasuryYield = 0
, TreasurySpread = 0
, TrancheYield = 0
, TranchePrice = 0
, TrancheProceedsWithInterest = 0
, TrancheAvgLife = 7.03937
, TrancheDuration = 4.70661
, TrancheDatedDate = "07-25-2016"
, TrancheFirstPmtDate = "08-25-2016"
, TrancheLastPmtDate = "02-25-2045"
, TrancheNextPmtDate = "08-25-2016"
, TrancheFinalPmtDate = "08-25-2046"
, Delay = 0
, InterestPmtFrequency = 12
, PrinPmtFrequency = 12
, PacLowBand = 0
, PacHighBand = 0
, FloaterIndex = "LIBOR1BBA"
, InitialIndexValue = 0.45
, FloaterMargin = 0.5
, FloaterMultiplier = 1
, FloaterCap = 6.5
, FloaterFloor = 0.5
, FloaterInitialCoupon = 0.95
, FloaterResetFrequency = 12
, FloaterFirstResetDate = "08-25-2016"
, FloaterFormula = function (Cap, Floor, Margin, Index, Multiplier)
{
min(Cap, max((Index * Multiplier) + Margin, Floor))
}
, Group = 1
, TrancheType = "character"
, Schedule = FALSE
, Fixed = FALSE
)
More information about the R-package-devel
mailing list