Skip to content

Commit

Permalink
enable mixed-asset type
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowbean committed Dec 9, 2023
1 parent 1a4fcfe commit cee3658
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 16 deletions.
8 changes: 8 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ data DealType = MDeal (DB.TestDeal AB.Mortgage)
| IDeal (DB.TestDeal AB.Installment)
| RDeal (DB.TestDeal AB.Lease)
| FDeal (DB.TestDeal AB.FixedAsset)
| UDeal (DB.TestDeal AB.AssetUnion)
deriving(Show, Generic)

instance ToSchema Ts
Expand All @@ -137,12 +138,15 @@ instance ToSchema (DB.TestDeal AB.Mortgage)
instance ToSchema (DB.TestDeal AB.Loan)
instance ToSchema (DB.TestDeal AB.Installment)
instance ToSchema (DB.TestDeal AB.Lease)
instance ToSchema (DB.TestDeal AB.AssetUnion)
instance ToSchema (DB.TestDeal AB.FixedAsset)
instance ToSchema (DB.PoolType AB.AssetUnion)
instance ToSchema (DB.PoolType AB.FixedAsset)
instance ToSchema (DB.PoolType AB.Mortgage)
instance ToSchema (DB.PoolType AB.Loan)
instance ToSchema (DB.PoolType AB.Installment)
instance ToSchema (DB.PoolType AB.Lease)
instance ToSchema (P.Pool AB.AssetUnion)
instance ToSchema HE.RateCap
instance ToSchema AB.LeaseStepUp
instance ToSchema AB.AccrualPeriod
Expand Down Expand Up @@ -261,6 +265,10 @@ wrapRun (FDeal d) mAssump mNonPerfAssump = let
(_d,_pflow,_rs,_p) = D.runDeal d D.DealPoolFlowPricing mAssump mNonPerfAssump
in
(FDeal _d,_pflow,_rs,_p)
wrapRun (UDeal d) mAssump mNonPerfAssump = let
(_d,_pflow,_rs,_p) = D.runDeal d D.DealPoolFlowPricing mAssump mNonPerfAssump
in
(UDeal _d,_pflow,_rs,_p)
wrapRun x _ _ = error $ "RunDeal Failed ,due to unsupport deal type "++ show x

wrapRunPool :: PoolType -> Maybe AP.ApplyAssumptionType -> Maybe [RateAssumption] -> (CF.CashFlowFrame, Map CutoffFields Balance)
Expand Down
2 changes: 0 additions & 2 deletions src/Asset.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ class (Show a,IR.UseRate a) => Asset a where
getRemainTerms :: a -> Int
-- | project asset cashflow under credit stress and interest assumptions
projCashflow :: a -> Date -> A.AssetPerf -> Maybe [RateAssumption] -> (CF.CashFlowFrame, Map.Map CutoffFields Balance)
-- | project cashflow under user input sequence
runCashflow :: a -> Date -> A.AssumpReceipes -> [RateAssumption] -> (CF.CashFlowFrame, Map.Map CutoffFields Balance)
-- | Get possible number of borrower
getBorrowerNum :: a -> Int
-- | Split asset per rates passed in
Expand Down
12 changes: 8 additions & 4 deletions src/AssetClass/AssetBase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ data AssetUnion = MO Mortgage
| FA FixedAsset
deriving (Show, Generic,Ord,Eq)

instance IR.UseRate MixedAsset where
getIndexes (MixedPool ma) = error "Not implemented"
instance IR.UseRate AssetUnion where

Check warning on line 141 in src/AssetClass/AssetBase.hs

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for

Check warning on line 141 in src/AssetClass/AssetBase.hs

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for
getIndex (MO ma) = IR.getIndex ma
getIndex (LO ma) = IR.getIndex ma
getIndex (IL ma) = IR.getIndex ma
getIndex (LS ma) = IR.getIndex ma
getIndex (FA ma) = IR.getIndex ma


instance IR.UseRate Mortgage where

Check warning on line 149 in src/AssetClass/AssetBase.hs

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for

Check warning on line 149 in src/AssetClass/AssetBase.hs

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for
Expand All @@ -150,11 +154,11 @@ instance IR.UseRate Mortgage where

instance IR.UseRate Loan where

Check warning on line 155 in src/AssetClass/AssetBase.hs

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for

Check warning on line 155 in src/AssetClass/AssetBase.hs

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for
getIndex (PersonalLoan oi@LoanOriginalInfo{originRate = IR.Floater _ idx _ _ _ _ _ _ } _ _ _ _) = Just idx
getIndex (PersonalLoan {}) = Nothing
getIndex PersonalLoan {} = Nothing

instance IR.UseRate Installment where

Check warning on line 159 in src/AssetClass/AssetBase.hs

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for

Check warning on line 159 in src/AssetClass/AssetBase.hs

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for
getIndex (Installment oi@LoanOriginalInfo{originRate = IR.Floater _ idx _ _ _ _ _ _ } _ _ _) = Just idx
getIndex (Installment {}) = Nothing
getIndex Installment {} = Nothing

instance IR.UseRate Lease where

Check warning on line 163 in src/AssetClass/AssetBase.hs

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for

Check warning on line 163 in src/AssetClass/AssetBase.hs

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for
getIndex :: Lease -> Maybe Index
Expand Down
2 changes: 2 additions & 0 deletions src/AssetClass/Loan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,7 @@ instance Asset Loan where
projCashflow m@(PersonalLoan (LoanOriginalInfo ob or ot p sd prinPayType) cb cr rt (Defaulted Nothing)) asOfDay assumps _
= (CF.CashFlowFrame [CF.LoanFlow asOfDay 0 0 0 0 0 0 0 cr Nothing],Map.empty)

projCashflow a b c d = error $ "failed to match projCashflow"++show a++show b++show c++show d

splitWith l@(PersonalLoan (LoanOriginalInfo ob or ot p sd prinPayType) cb cr rt st) rs
= [ PersonalLoan (LoanOriginalInfo (mulBR ob ratio) or ot p sd prinPayType) (mulBR cb ratio) cr rt st | ratio <- rs ]
118 changes: 111 additions & 7 deletions src/AssetClass/MixedAsset.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,121 @@ import qualified Asset as Ast



instance P.Asset MixedAsset where
instance P.Asset AssetUnion where

getCurrentBal ma = 0
calcCashflow ma asOfDay mRates = error "not implemented for asset union"

getCurrentBal ma = curBal ma

getOriginBal ma = origBal ma

getOriginRate ma = origRate ma

getOriginBal ma = 0
getOriginDate ma = origDate ma

getOriginInfo ma = origInfo ma

isDefaulted = isDefault

getPaymentDates ma n = getPaydates ma n

getOriginRate ma = 0
getRemainTerms = remainTerms

calcCashflow pl asOfDay mRates
= CF.CashFlowFrame []
projCashflow ma asOfDay assumps mRates = projAssetUnion ma asOfDay assumps mRates

getBorrowerNum = borrowerNum

splitWith = splitWith

updateOriginDate = updateOrigDate

calcAlignDate = calcAlignDate

curBal:: ACM.AssetUnion -> Balance
curBal (ACM.MO ast) = P.getCurrentBal ast
curBal (ACM.LO ast) = P.getCurrentBal ast
curBal (ACM.IL ast) = P.getCurrentBal ast
curBal (ACM.LS ast) = P.getCurrentBal ast
curBal (ACM.FA ast) = P.getCurrentBal ast

origBal :: ACM.AssetUnion -> Balance
origBal (ACM.MO ast) = P.getOriginBal ast
origBal (ACM.LO ast) = P.getOriginBal ast
origBal (ACM.IL ast) = P.getOriginBal ast
origBal (ACM.LS ast) = P.getOriginBal ast
origBal (ACM.FA ast) = P.getOriginBal ast

origRate :: ACM.AssetUnion -> IRate
origRate (ACM.MO ast) = P.getOriginRate ast
origRate (ACM.LO ast) = P.getOriginRate ast
origRate (ACM.IL ast) = P.getOriginRate ast
origRate (ACM.LS ast) = P.getOriginRate ast
origRate (ACM.FA ast) = P.getOriginRate ast

origDate :: ACM.AssetUnion -> Date
origDate (ACM.MO ast) = P.getOriginDate ast
origDate (ACM.LO ast) = P.getOriginDate ast
origDate (ACM.IL ast) = P.getOriginDate ast
origDate (ACM.LS ast) = P.getOriginDate ast
origDate (ACM.FA ast) = P.getOriginDate ast

origInfo :: ACM.AssetUnion -> OriginalInfo
origInfo (ACM.MO ast) = P.getOriginInfo ast
origInfo (ACM.LO ast) = P.getOriginInfo ast
origInfo (ACM.IL ast) = P.getOriginInfo ast
origInfo (ACM.LS ast) = P.getOriginInfo ast
origInfo (ACM.FA ast) = P.getOriginInfo ast

isDefault :: ACM.AssetUnion -> Bool
isDefault (ACM.MO ast) = P.isDefaulted ast
isDefault (ACM.LO ast) = P.isDefaulted ast
isDefault (ACM.IL ast) = P.isDefaulted ast
isDefault (ACM.LS ast) = P.isDefaulted ast
isDefault (ACM.FA ast) = P.isDefaulted ast

getPaydates :: ACM.AssetUnion -> Int -> [Date]
getPaydates (ACM.MO ast) n = P.getPaymentDates ast n
getPaydates (ACM.LO ast) n = P.getPaymentDates ast n
getPaydates (ACM.IL ast) n = P.getPaymentDates ast n
getPaydates (ACM.LS ast) n = P.getPaymentDates ast n
getPaydates (ACM.FA ast) n = P.getPaymentDates ast n

remainTerms :: ACM.AssetUnion -> Int
remainTerms (ACM.MO ast) = P.getRemainTerms ast
remainTerms (ACM.LO ast) = P.getRemainTerms ast
remainTerms (ACM.IL ast) = P.getRemainTerms ast
remainTerms (ACM.LS ast) = P.getRemainTerms ast
remainTerms (ACM.FA ast) = P.getRemainTerms ast

borrowerNum :: ACM.AssetUnion -> Int
borrowerNum (ACM.MO ast) = P.getBorrowerNum ast
borrowerNum (ACM.LO ast) = P.getBorrowerNum ast
borrowerNum (ACM.IL ast) = P.getBorrowerNum ast
borrowerNum (ACM.LS ast) = P.getBorrowerNum ast
borrowerNum (ACM.FA ast) = P.getBorrowerNum ast

splitWith :: ACM.AssetUnion -> [Rate] -> [ACM.AssetUnion]
splitWith (ACM.MO ast) rs = ACM.MO <$> P.splitWith ast rs
splitWith (ACM.LO ast) rs = ACM.LO <$> P.splitWith ast rs
splitWith (ACM.IL ast) rs = ACM.IL <$> P.splitWith ast rs
splitWith (ACM.LS ast) rs = ACM.LS <$> P.splitWith ast rs
splitWith (ACM.FA ast) rs = ACM.FA <$> P.splitWith ast rs

updateOrigDate :: ACM.AssetUnion -> Date -> ACM.AssetUnion
updateOrigDate (ACM.MO ast) d = ACM.MO $ P.updateOriginDate ast d
updateOrigDate (ACM.LO ast) d = ACM.LO $ P.updateOriginDate ast d
updateOrigDate (ACM.IL ast) d = ACM.IL $ P.updateOriginDate ast d
updateOrigDate (ACM.LS ast) d = ACM.LS $ P.updateOriginDate ast d
updateOrigDate (ACM.FA ast) d = ACM.FA $ P.updateOriginDate ast d

calcAlignDate :: ACM.AssetUnion -> Date -> Date
calcAlignDate (ACM.MO ast) = P.calcAlignDate ast
calcAlignDate (ACM.LO ast) = P.calcAlignDate ast
calcAlignDate (ACM.IL ast) = P.calcAlignDate ast
calcAlignDate (ACM.LS ast) = P.calcAlignDate ast
calcAlignDate (ACM.FA ast) = P.calcAlignDate ast


projAssetUnion :: ACM.AssetUnion -> Date -> A.AssetPerf -> Maybe [RateAssumption] -> (CF.CashFlowFrame, Map.Map CutoffFields Balance)
projAssetUnion (ACM.MO ast) d assumps mRates = P.projCashflow ast d assumps mRates
projAssetUnion (ACM.LO ast) d assumps mRates = P.projCashflow ast d assumps mRates
Expand Down Expand Up @@ -83,4 +187,4 @@ projectCashflow (MixedPool assetMap) asOfDate mAssump mRate
mRate)
assetMap
in
mWithCf
mWithCf
4 changes: 2 additions & 2 deletions src/Deal/DealQuery.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ debug = flip trace

-- | calcuate target balance for a reserve account, 0 for a non-reserve account
calcTargetAmount :: P.Asset a => TestDeal a -> Date -> A.Account -> Balance
calcTargetAmount t d (A.Account _ n i Nothing _ ) = 0
calcTargetAmount t d (A.Account _ n i (Just r) _ ) =
calcTargetAmount t d (A.Account _ _ _ Nothing _ ) = 0
calcTargetAmount t d (A.Account _ _ _ (Just r) _ ) =
eval r
where
eval ra = case ra of
Expand Down
2 changes: 1 addition & 1 deletion src/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ data PoolId = PoolName String
deriving (Eq,Ord,Generic)

instance Show PoolId where
show (PoolName n) = "PoolName:"++n
show (PoolName n) = n
show PoolConsol = "PoolConsol"

instance (Read PoolId) where
Expand Down
Loading

0 comments on commit cee3658

Please sign in to comment.