Skip to content

Commit

Permalink
expose obligor field
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowbean committed Sep 7, 2024
1 parent ac4eab6 commit 416b91a
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 170 deletions.
21 changes: 15 additions & 6 deletions src/AssetClass/AssetBase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,31 @@ data ReceivableFeeType = FixedFee Balance -- ^ a flat fee amo
deriving (Show,Generic,Eq,Ord)


data Obligor = Obligor {obligorId :: Int
, obligorTag :: [String]
, obligorFields :: Map.Map String (Either String Double)
} deriving (Show,Generic,Eq,Ord)

data OriginalInfo = MortgageOriginalInfo { originBalance :: Balance
,originRate :: IR.RateType
,originTerm :: Int
,period :: Period
,startDate :: Date
,prinType :: AmortPlan
,prepaymentPenalty :: Maybe PrepayPenaltyType }
,prepaymentPenalty :: Maybe PrepayPenaltyType
,obligor :: Maybe Obligor }
| LoanOriginalInfo { originBalance :: Balance
,originRate :: IR.RateType
,originTerm :: Int
,period :: Period
,startDate :: Date
,prinType :: AmortPlan }
,prinType :: AmortPlan
,obligor :: Maybe Obligor }
| LeaseInfo { startDate :: Date -- ^ lease start date
,originTerm :: Int -- ^ total terms
,paymentDates :: DatePattern -- ^ payment dates pattern
,originRental :: Amount} -- ^ rental by day
,originRental :: Amount -- ^ rental by day
,obligor :: Maybe Obligor }
| FixedAssetInfo { startDate :: Date
,originBalance :: Balance
,residualBalance :: Balance
Expand All @@ -155,7 +163,8 @@ data OriginalInfo = MortgageOriginalInfo { originBalance :: Balance
,originBalance :: Balance
,originAdvance :: Balance
,dueDate :: Date
,feeType :: Maybe ReceivableFeeType }
,feeType :: Maybe ReceivableFeeType
,obligor :: Maybe Obligor }
deriving (Show,Generic,Ord,Eq)


Expand Down Expand Up @@ -283,7 +292,7 @@ instance IR.UseRate ProjectedCashflow where
= Just $ (\(a,b,c) -> c) <$> fs


$(concat <$> traverse (deriveJSON defaultOptions) [''OriginalInfo, ''FixedAsset, ''AmortPlan, ''PrepayPenaltyType
$(concat <$> traverse (deriveJSON defaultOptions) [''Obligor, ''OriginalInfo, ''FixedAsset, ''AmortPlan, ''PrepayPenaltyType
, ''Capacity, ''AmortRule, ''ReceivableFeeType])


Expand Down Expand Up @@ -311,7 +320,7 @@ instance ToSchema (TsPoint IRate)
instance ToSchema (TsPoint Rational)
instance ToSchema (TsPoint Bool)
instance ToSchema (RoundingBy IRate)

instance ToSchema Obligor
instance ToSchema Index
instance ToSchema DayCount
instance ToSchema Direction
Expand Down
28 changes: 14 additions & 14 deletions src/AssetClass/Installment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ projectInstallmentFlow (startBal, lastPaidDate, (originRepay,originInt), startRa


instance Asset Installment where
calcCashflow inst@(Installment (LoanOriginalInfo ob or ot p sd ptype) cb rt st) asOfDay _
calcCashflow inst@(Installment (LoanOriginalInfo ob or ot p sd ptype _) cb rt st) asOfDay _
= CF.CashFlowFrame (cb,asOfDay,Nothing) flows
where
lastPayDate:cf_dates = lastN (rt+1) $ sd:getPaymentDates inst 0
Expand Down Expand Up @@ -103,30 +103,30 @@ instance Asset Installment where

getCurrentBal (Installment _ b _ _ ) = b

getOriginBal (Installment (LoanOriginalInfo ob _ _ _ _ _) _ _ _) = ob
getOriginBal (Installment (LoanOriginalInfo ob _ _ _ _ _ _) _ _ _) = ob

getOriginRate (Installment (LoanOriginalInfo _ or _ _ _ _) _ _ _)
getOriginRate (Installment (LoanOriginalInfo _ or _ _ _ _ _) _ _ _)
= case or of
Fix _ _r -> _r
Floater _ _ _ _r _ _ _ _ -> _r

isDefaulted (Installment _ _ _ (Defaulted _)) = True
isDefaulted (Installment {}) = False

getPaymentDates (Installment (LoanOriginalInfo _ _ ot p sd _) _ _ _) extra
getPaymentDates (Installment (LoanOriginalInfo _ _ ot p sd _ _) _ _ _) extra
= genDates sd p (ot+extra)

getOriginDate (Installment (LoanOriginalInfo _ _ ot p sd _) _ _ _) = sd
getOriginDate (Installment (LoanOriginalInfo _ _ ot p sd _ _) _ _ _) = sd

getRemainTerms (Installment (LoanOriginalInfo _ _ ot p sd _) _ rt _) = rt
getRemainTerms (Installment (LoanOriginalInfo _ _ ot p sd _ _) _ rt _) = rt

updateOriginDate (Installment (LoanOriginalInfo ob or ot p sd _type) cb rt st) nd
= Installment (LoanOriginalInfo ob or ot p nd _type) cb rt st
updateOriginDate (Installment (LoanOriginalInfo ob or ot p sd _type _obligor) cb rt st) nd
= Installment (LoanOriginalInfo ob or ot p nd _type _obligor) cb rt st

resetToOrig (Installment (LoanOriginalInfo ob or ot p sd _type) cb rt st)
= Installment (LoanOriginalInfo ob or ot p sd _type) ob ot st
resetToOrig (Installment (LoanOriginalInfo ob or ot p sd _type _obligor) cb rt st)
= Installment (LoanOriginalInfo ob or ot p sd _type _obligor) ob ot st

projCashflow inst@(Installment (LoanOriginalInfo ob or ot p sd pt) cb rt Current)
projCashflow inst@(Installment (LoanOriginalInfo ob or ot p sd pt _) cb rt Current)
asOfDay
pAssump@(A.InstallmentAssump defaultAssump prepayAssump recoveryAssump ams,_,_)
mRates
Expand All @@ -152,7 +152,7 @@ instance Asset Installment where


-- ^ project with defaulted at a date
projCashflow inst@(Installment (LoanOriginalInfo ob or ot p sd ptype) cb rt (Defaulted (Just defaultedDate)))
projCashflow inst@(Installment (LoanOriginalInfo ob or ot p sd ptype _) cb rt (Defaulted (Just defaultedDate)))
asOfDay
(_,_,(A.DefaultedRecovery rr lag timing))
mRates
Expand All @@ -171,7 +171,7 @@ instance Asset Installment where
projCashflow inst@(Installment _ cb rt (Defaulted Nothing)) asOfDay assumps _
= (CF.CashFlowFrame (cb, asOfDay, Nothing) $ [CF.LoanFlow asOfDay cb 0 0 0 0 0 0 (getOriginRate inst) Nothing],Map.empty)

splitWith (Installment (LoanOriginalInfo ob or ot p sd _type) cb rt st) rs
= [ Installment (LoanOriginalInfo (mulBR ob ratio) or ot p sd _type) (mulBR cb ratio) rt st | ratio <- rs ]
splitWith (Installment (LoanOriginalInfo ob or ot p sd _type _obligor) cb rt st) rs
= [ Installment (LoanOriginalInfo (mulBR ob ratio) or ot p sd _type _obligor) (mulBR cb ratio) rt st | ratio <- rs ]


68 changes: 34 additions & 34 deletions src/AssetClass/Lease.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ accrueRentals rc@(LeftBalanceCurve tps) pd@(payD:payDs) accrueCutoff payAmts
(payAmts ++ [accrueRentalBetween accrueCutoff payD rc]) -- `debug` ("ACCRENTALS"++ show accrueCutoff++">>"++show payD++"rc+"++ show rc)

nextLease :: Lease -> (RentChangeCurve, TermChangeRate, DayGap) -> (Lease, Date)
nextLease l@(RegularLease (LeaseInfo sd ot dp dr) bal rt _) (rcCurve,tc,gd)
= (RegularLease (LeaseInfo nextStartDate nextOriginTerm dp nextDailyRate) newBal rt Current,nextEndDate) -- `debug` ("1+tc"++show (1+tc) ++">>"++ show (mulIR ot (1+tc)))
nextLease l@(RegularLease (LeaseInfo sd ot dp dr ob) bal rt _) (rcCurve,tc,gd)
= (RegularLease (LeaseInfo nextStartDate nextOriginTerm dp nextDailyRate ob) newBal rt Current,nextEndDate) -- `debug` ("1+tc"++show (1+tc) ++">>"++ show (mulIR ot (1+tc)))
where
leaseEndDate = last $ projectCfDates dp sd ot
nextStartDate = T.addDays (succ (toInteger gd)) leaseEndDate -- `debug` ("Gap Day ->"++ show gd)
Expand All @@ -76,8 +76,8 @@ nextLease l@(RegularLease (LeaseInfo sd ot dp dr) bal rt _) (rcCurve,tc,gd)
nextDailyRate = dr + mulBR dr currentRateOnCurve*(fromRational yearsBetween)
newBal = fromRational $ mulBInteger nextDailyRate $ daysBetween nextStartDate nextEndDate

nextLease l@(StepUpLease (LeaseInfo sd ot dp dr) lsteupInfo bal rt _) (rcCurve,tc,gd)
= (StepUpLease (LeaseInfo nextStartDate nextOriginTerm dp nextDailyRate) lsteupInfo newBal rt Current,nextEndDate) -- `debug` ("leaseEndDate>>"++show leaseEndDate++">>>"++show (succ (toInteger gd)))
nextLease l@(StepUpLease (LeaseInfo sd ot dp dr ob) lsteupInfo bal rt _) (rcCurve,tc,gd)
= (StepUpLease (LeaseInfo nextStartDate nextOriginTerm dp nextDailyRate ob) lsteupInfo newBal rt Current,nextEndDate) -- `debug` ("leaseEndDate>>"++show leaseEndDate++">>>"++show (succ (toInteger gd)))
where
leaseEndDate = last $ projectCfDates dp sd ot
nextStartDate = T.addDays (succ (toInteger gd)) leaseEndDate -- `debug` ("Gap Day ->"++ show gd)
Expand Down Expand Up @@ -112,8 +112,8 @@ getGapDaysByBalance l tbl@(rows,defaultVal)
= let
tbl = ThresholdTable rows
pmt = case l of
(RegularLease (LeaseInfo _ _ _ dr) _ _ _) -> dr
(StepUpLease (LeaseInfo _ _ _ dr) _ _ _ _) -> dr
(RegularLease (LeaseInfo _ _ _ dr _) _ _ _) -> dr
(StepUpLease (LeaseInfo _ _ _ dr _) _ _ _ _) -> dr
in
fromMaybe defaultVal $ lookupTable tbl Down (>= pmt)

Expand All @@ -130,15 +130,15 @@ projectCfDates dp sd ot

-- ^ return a lease contract with opening balance and a payment cashflow on each payment date
patchBalance :: Lease -> (Lease,[Amount])
patchBalance (RegularLease (LeaseInfo sd ot dp dr) bal rt st)
patchBalance (RegularLease (LeaseInfo sd ot dp dr ob) bal rt st)
= let
cf_dates = lastN (succ rt) $ projectCfDates dp sd ot
pmts = [ fromRational (mulBInt dr ds) | ds <- getIntervalDays cf_dates ]
new_bal = sum pmts -- `debug` ("cf_date" ++ show cf_dates)
in
(RegularLease (LeaseInfo sd ot dp dr) new_bal rt st, pmts)
(RegularLease (LeaseInfo sd ot dp dr ob) new_bal rt st, pmts)

patchBalance (StepUpLease (LeaseInfo sd ot dp dr) lsu bal rt st)
patchBalance (StepUpLease (LeaseInfo sd ot dp dr ob) lsu bal rt st)
= let
p_dates = projectCfDates dp sd ot
cf_dates = lastN (succ rt) $ projectCfDates dp sd ot
Expand Down Expand Up @@ -168,47 +168,47 @@ patchBalance (StepUpLease (LeaseInfo sd ot dp dr) lsu bal rt st)
accrueRentals rate_curve (tail cf_dates) last_pay_date [] -- `debug` ("Using curve->"++show rate_curve++">>"++ show last_pay_date)
new_bal = sum pmts -- `debug` ("Patch balance pmts"++ show pmts)
in
(StepUpLease (LeaseInfo sd ot dp dr) lsu new_bal rt st,pmts)
(StepUpLease (LeaseInfo sd ot dp dr ob) lsu new_bal rt st,pmts)


instance Asset Lease where
calcCashflow l@(RegularLease {}) d _ =
CF.CashFlowFrame (0,d,Nothing) $ cutBy Inc Future d (zipWith3 CF.LeaseFlow (tail cf_dates) bals pmts)
where
(RegularLease (LeaseInfo sd ot dp dr) bal rt st,pmts) = patchBalance l
(RegularLease (LeaseInfo sd ot dp dr ob) bal rt st,pmts) = patchBalance l
cf_dates = lastN (succ rt) $ projectCfDates dp sd ot
daysBetween = getIntervalDays cf_dates -- `debug` (">>>>>> genSerialDates"++ show cf_dates)
bals = tail $ scanl (-) bal pmts -- `debug` ("PMTS for regular"++show pmts)

calcCashflow l@(StepUpLease {}) d _ =
CF.CashFlowFrame (0,d,Nothing) $ cutBy Inc Future d (zipWith3 CF.LeaseFlow (tail cf_dates) bals pmts)
where
(StepUpLease (LeaseInfo sd ot dp dr) lsu bal rt st,pmts) = patchBalance l -- `debug` ("1")
(StepUpLease (LeaseInfo sd ot dp dr ob) lsu bal rt st,pmts) = patchBalance l -- `debug` ("1")
p_dates = projectCfDates dp sd ot -- `debug` ("2")
cf_dates = lastN (succ rt) p_dates -- `debug` ("3") -- `debug` ("P dates"++ show p_dates)
bals = tail $ scanl (-) bal pmts -- `debug` ("4"++show pmts) -- `debug` ("PMTS->"++ show pmts)

getPaymentDates l@(RegularLease (LeaseInfo sd ot dp _) _ rt _) _
getPaymentDates l@(RegularLease (LeaseInfo sd ot dp _ _) _ rt _) _
= genSerialDates dp Inc sd ot

getPaymentDates l@(StepUpLease (LeaseInfo sd ot dp _) _ _ rt _) _
getPaymentDates l@(StepUpLease (LeaseInfo sd ot dp _ _) _ _ rt _) _
= genSerialDates dp Inc sd ot

getOriginDate (StepUpLease (LeaseInfo sd ot dp _) _ _ rt _) = sd
getOriginDate (RegularLease (LeaseInfo sd ot dp _) _ rt _) = sd
getOriginDate (StepUpLease (LeaseInfo sd ot dp _ _) _ _ rt _) = sd
getOriginDate (RegularLease (LeaseInfo sd ot dp _ _) _ rt _) = sd

getRemainTerms (StepUpLease (LeaseInfo sd ot dp _) _ _ rt _) = rt
getRemainTerms (RegularLease (LeaseInfo sd ot dp _) _ rt _) = rt
getRemainTerms (StepUpLease (LeaseInfo sd ot dp _ _) _ _ rt _) = rt
getRemainTerms (RegularLease (LeaseInfo sd ot dp _ _) _ rt _) = rt

updateOriginDate (StepUpLease (LeaseInfo sd ot dp dr) lsu bal rt st) nd
= StepUpLease (LeaseInfo nd ot dp dr) lsu bal rt st
updateOriginDate (RegularLease (LeaseInfo sd ot dp dr) bal rt st) nd
= RegularLease (LeaseInfo nd ot dp dr) bal rt st
updateOriginDate (StepUpLease (LeaseInfo sd ot dp dr ob) lsu bal rt st) nd
= StepUpLease (LeaseInfo nd ot dp dr ob) lsu bal rt st
updateOriginDate (RegularLease (LeaseInfo sd ot dp dr ob) bal rt st) nd
= RegularLease (LeaseInfo nd ot dp dr ob) bal rt st

resetToOrig (StepUpLease (LeaseInfo sd ot dp dr) lsu bal rt st)
= fst . patchBalance $ StepUpLease (LeaseInfo sd ot dp dr) lsu bal ot st
resetToOrig (RegularLease (LeaseInfo sd ot dp dr) bal rt st)
= fst . patchBalance $ RegularLease (LeaseInfo sd ot dp dr) bal ot st
resetToOrig (StepUpLease (LeaseInfo sd ot dp dr ob) lsu bal rt st)
= fst . patchBalance $ StepUpLease (LeaseInfo sd ot dp dr ob) lsu bal ot st
resetToOrig (RegularLease (LeaseInfo sd ot dp dr ob) bal rt st)
= fst . patchBalance $ RegularLease (LeaseInfo sd ot dp dr ob) bal ot st

projCashflow l asOfDay ((AP.LeaseAssump gapAssump rentAssump ed exStress),_,_) mRates
= (CF.CashFlowFrame (0,asOfDay,Nothing) allTxns, Map.empty)
Expand Down Expand Up @@ -237,8 +237,8 @@ instance Asset Lease where
StepUpLease _ _ bal _ _ -> bal
RegularLease _ bal _ _-> bal

getOriginRate (StepUpLease (LeaseInfo _ _ _ dr) _ _ _ _) = fromRational $ toRational dr
getOriginRate (RegularLease (LeaseInfo _ _ _ dr) _ _ _) = fromRational $ toRational dr
getOriginRate (StepUpLease (LeaseInfo _ _ _ dr _) _ _ _ _) = fromRational $ toRational dr
getOriginRate (RegularLease (LeaseInfo _ _ _ dr _) _ _ _) = fromRational $ toRational dr

isDefaulted (StepUpLease _ _ _ rt Current) = False
isDefaulted (StepUpLease _ _ _ rt _) = True
Expand All @@ -248,14 +248,14 @@ instance Asset Lease where
getOriginBal l =
let
_sd = case l of
RegularLease (LeaseInfo sd ot dp dr) bal _ _ -> sd
StepUpLease (LeaseInfo sd ot dp dr) _ bal _ _ -> sd
RegularLease (LeaseInfo sd ot dp dr _) bal _ _ -> sd
StepUpLease (LeaseInfo sd ot dp dr _) _ bal _ _ -> sd
CF.CashFlowFrame _ txns = calcCashflow l _sd Nothing
in
CF.mflowBegBalance $ head txns

splitWith (RegularLease (LeaseInfo sd ot dp dr) bal rt st ) rs
= [ RegularLease (LeaseInfo sd ot dp dr) (mulBR bal ratio) rt st | ratio <- rs ]
splitWith (StepUpLease (LeaseInfo sd ot dp dr) stup bal rt st ) rs
= [ StepUpLease (LeaseInfo sd ot dp dr) stup (mulBR bal ratio) rt st | ratio <- rs]
splitWith (RegularLease (LeaseInfo sd ot dp dr ob) bal rt st ) rs
= [ RegularLease (LeaseInfo sd ot dp dr ob) (mulBR bal ratio) rt st | ratio <- rs ]
splitWith (StepUpLease (LeaseInfo sd ot dp dr ob) stup bal rt st ) rs
= [ StepUpLease (LeaseInfo sd ot dp dr ob) stup (mulBR bal ratio) rt st | ratio <- rs]

Loading

0 comments on commit 416b91a

Please sign in to comment.