Skip to content

Commit

Permalink
Refactor weighted average formula (#193)
Browse files Browse the repository at this point in the history
* expose WA formula
* bump version to-> < 0.28.15 >
  • Loading branch information
yellowbean authored Jul 31, 2024
1 parent bf60fd5 commit c240965
Show file tree
Hide file tree
Showing 33 changed files with 2,179 additions and 589 deletions.
23 changes: 21 additions & 2 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
# Changelog for Hastructure

## 0.28.3
### TODO
## 0.28.15
### 2024-7-31
* FIX: enable compound formula on `weighted average` formula.


## 0.28.14
### 2024-07-06
* FIX: enable `annualized rate fee type` with formula `bondbalance` on `bondGroup`

## 0.28.13
### 2024-06-30
* NEW: new assumption `issue bond` which allow funding by issuing new bonds during cashflow projection.
* NEW: new asset class `projectScheduleFlow` which can be divided projected cashflow with fix portion and float portions. The interest from the float portion will be affected by interest rate assumption.
* ENHANCE: enable formula `bondRate`/`bondWaRate` on `bondGroup`
* FIX: `formula` will return `inf` if a `divide` with zero instead of just throw exception
* FIX: `financial reports` was failing because it can't access to `interest due` on bond group.
* FIX: enable formula query on `bond groups`


## 0.28.8
### 2024-06
* FIX: `limit` on `payFee` was not working with `duePct`
* ENHANCE: expose `transaction statement` for `triggers`

Expand Down
7 changes: 6 additions & 1 deletion Hastructure.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.2.
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack

Expand Down Expand Up @@ -37,6 +37,7 @@ library
AssetClass.Loan
AssetClass.MixedAsset
AssetClass.Mortgage
AssetClass.ProjectedCashFlow
AssetClass.Receivable
Assumptions
Call
Expand All @@ -63,6 +64,7 @@ library
Triggers
Types
Util
Validation
Waterfall
other-modules:
Paths_Hastructure
Expand All @@ -76,6 +78,7 @@ library
, containers
, hashable
, lens
, numeric-limits
, openapi3
, regex-base
, regex-pcre-builtin
Expand Down Expand Up @@ -115,6 +118,7 @@ executable Hastructure-exe
, lens
, lucid
, mtl
, numeric-limits
, openapi3
, regex-base
, regex-pcre-builtin
Expand Down Expand Up @@ -173,6 +177,7 @@ test-suite Hastructure-test
, containers
, hashable
, lens
, numeric-limits
, openapi3
, regex-base
, regex-pcre-builtin
Expand Down
27 changes: 21 additions & 6 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import qualified AssetClass.Installment
import qualified AssetClass.Mortgage
import qualified AssetClass.Loan
import qualified AssetClass.Lease
import qualified AssetClass.ProjectedCashFlow
import qualified AssetClass.MixedAsset as MA
import qualified AssetClass.AssetBase as AB
import qualified Assumptions as AP
Expand Down Expand Up @@ -102,16 +103,17 @@ $(deriveJSON defaultOptions ''Version)
instance ToSchema Version

version1 :: Version
version1 = Version "0.28.6"
version1 = Version "0.28.15"


data DealType = MDeal (DB.TestDeal AB.Mortgage)
| LDeal (DB.TestDeal AB.Loan)
| IDeal (DB.TestDeal AB.Installment)
| RDeal (DB.TestDeal AB.Lease)
| FDeal (DB.TestDeal AB.FixedAsset)
| VDeal (DB.TestDeal AB.Receivable)
| UDeal (DB.TestDeal AB.AssetUnion)
| VDeal (DB.TestDeal AB.Receivable)
| PDeal (DB.TestDeal AB.ProjectedCashflow)
| UDeal (DB.TestDeal AB.AssetUnion)
deriving(Show, Generic)

instance ToSchema CF.CashFlowFrame
Expand All @@ -122,6 +124,7 @@ instance ToSchema AB.LeaseStepUp
instance ToSchema AB.Lease
instance ToSchema AB.FixedAsset
instance ToSchema AB.Receivable
instance ToSchema AB.ProjectedCashflow
instance ToSchema CutoffFields
instance ToSchema (P.Pool AB.Mortgage)
instance ToSchema (P.Pool AB.Loan)
Expand All @@ -130,12 +133,13 @@ instance ToSchema (P.Pool AB.Lease)
instance ToSchema (P.Pool AB.FixedAsset)
instance ToSchema (P.Pool AB.Receivable)
instance ToSchema (P.Pool AB.AssetUnion)
instance ToSchema (P.Pool AB.ProjectedCashflow)
instance ToSchema AB.AssetUnion
instance ToSchema PoolId
instance ToSchema DealStatus
instance ToSchema DateType
instance ToSchema DateDesp
instance ToSchema ActionOnDate
instance ToSchema DB.DateDesp
instance ToSchema DB.ActionOnDate
instance ToSchema DealStats
instance ToSchema Cmp
instance ToSchema PricingMethod
Expand Down Expand Up @@ -181,7 +185,7 @@ instance ToSchema CE.LiqDrawType
instance ToSchema CustomDataType
instance ToSchema TRG.Trigger
instance ToSchema TRG.TriggerEffect
instance ToSchema OverrideType
instance ToSchema DB.OverrideType
instance ToSchema Types.BalanceSheetReport
instance ToSchema Types.CashflowReport
instance ToSchema Types.BookItem
Expand All @@ -191,6 +195,7 @@ instance ToSchema AB.AssociateExp
instance ToSchema AB.AssociateIncome
instance ToSchema RV.RevolvingPool
instance ToSchema (TsPoint [AB.AssetUnion])
instance ToSchema (TsPoint AP.IssueBondEvent)
instance ToSchema AP.NonPerfAssumption
instance ToSchema AP.BondPricingInput
instance ToSchema AP.RevolvingAssumption
Expand All @@ -214,6 +219,7 @@ instance ToSchema (DB.TestDeal AB.Loan)
instance ToSchema (DB.TestDeal AB.Installment)
instance ToSchema (DB.TestDeal AB.Lease)
instance ToSchema (DB.TestDeal AB.Receivable)
instance ToSchema (DB.TestDeal AB.ProjectedCashflow)
instance ToSchema (DB.TestDeal AB.AssetUnion)
instance ToSchema (DB.TestDeal AB.FixedAsset)

Expand All @@ -223,6 +229,7 @@ instance ToSchema (DB.PoolType AB.Installment)
instance ToSchema (DB.PoolType AB.Lease)
instance ToSchema (DB.PoolType AB.FixedAsset)
instance ToSchema (DB.PoolType AB.Receivable)
instance ToSchema (DB.PoolType AB.ProjectedCashflow)
instance ToSchema (DB.PoolType AB.AssetUnion)

instance ToSchema (DB.UnderlyingDeal AB.Mortgage)
Expand All @@ -231,6 +238,7 @@ instance ToSchema (DB.UnderlyingDeal AB.Installment)
instance ToSchema (DB.UnderlyingDeal AB.Lease)
instance ToSchema (DB.UnderlyingDeal AB.FixedAsset)
instance ToSchema (DB.UnderlyingDeal AB.Receivable)
instance ToSchema (DB.UnderlyingDeal AB.ProjectedCashflow)
instance ToSchema (DB.UnderlyingDeal AB.AssetUnion)

instance ToSchema ResultComponent
Expand Down Expand Up @@ -268,6 +276,11 @@ wrapRun (VDeal d) mAssump mNonPerfAssump = let
(_d,_pflow,_rs,_p) = D.runDeal d D.DealPoolFlowPricing mAssump mNonPerfAssump
in
(VDeal _d,_pflow,_rs,_p)
wrapRun (PDeal d) mAssump mNonPerfAssump = let
(_d,_pflow,_rs,_p) = D.runDeal d D.DealPoolFlowPricing mAssump mNonPerfAssump
in
(PDeal _d,_pflow,_rs,_p)

wrapRun x _ _ = error $ "RunDeal Failed ,due to unsupport deal type "++ show x


Expand All @@ -277,6 +290,7 @@ data PoolTypeWrap = LPool (DB.PoolType AB.Loan)
| RPool (DB.PoolType AB.Lease)
| FPool (DB.PoolType AB.FixedAsset)
| VPool (DB.PoolType AB.Receivable)
| PPool (DB.PoolType AB.ProjectedCashflow)
| UPool (DB.PoolType AB.AssetUnion)
deriving(Show, Generic)

Expand All @@ -293,6 +307,7 @@ wrapRunPoolType (IPool pt) assump mRates = D.runPoolType pt assump $ Just (AP.No
wrapRunPoolType (RPool pt) assump mRates = D.runPoolType pt assump $ Just (AP.NonPerfAssumption{AP.interest = mRates})
wrapRunPoolType (FPool pt) assump mRates = D.runPoolType pt assump $ Just (AP.NonPerfAssumption{AP.interest = mRates})
wrapRunPoolType (VPool pt) assump mRates = D.runPoolType pt assump $ Just (AP.NonPerfAssumption{AP.interest = mRates})
wrapRunPoolType (PPool pt) assump mRates = D.runPoolType pt assump $ Just (AP.NonPerfAssumption{AP.interest = mRates})
wrapRunPoolType (UPool pt) assump mRates = D.runPoolType pt assump $ Just (AP.NonPerfAssumption{AP.interest = mRates})
wrapRunPoolType x _ _ = error $ "RunPool Failed ,due to unsupport pool type "++ show x

Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies:
- swagger2
- split
- tabular
- numeric-limits

library:
source-dirs:
Expand Down
2 changes: 1 addition & 1 deletion src/Accounts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ deposit :: Amount -> Date -> TxnComment -> Account -> Account
deposit amount d source acc@(Account bal _ _ _ maybeStmt) =
acc {accBalance = newBal, accStmt = newStmt}
where
newBal = bal + amount
newBal = bal + amount -- `debug` ("Date:"++show d++ "deposit"++show amount++"from"++show bal)
newStmt = appendStmt maybeStmt (AccTxn d newBal amount source)

-- | draw cash from account with a comment
Expand Down
8 changes: 1 addition & 7 deletions src/Asset.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class (Show a,IR.UseRate a) => Asset a where
splitWith :: a -> [Rate] -> [a]
-- | ! Change the origination date of an asset
updateOriginDate :: a -> Date -> a
-- | get Last Interest Payment date
-- | Get Last Interest Payment date
getLastInterestPaymentDate :: a -> Maybe Date
-- | Calculate Accrued Interest
calcAccruedInterest :: a -> Date -> Balance
Expand All @@ -96,12 +96,6 @@ class (Show a,IR.UseRate a) => Asset a where









-- | apply ExtraStress on prepayment/default rates
applyExtraStress :: Maybe A.ExtraStress -> [Date] -> [Rate] -> [Rate] -> ([Rate],[Rate])
applyExtraStress Nothing _ ppy def = (ppy,def)
Expand Down
33 changes: 20 additions & 13 deletions src/AssetClass/AssetBase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module AssetClass.AssetBase
,LeaseStepUp(..),AccrualPeriod(..),PrepayPenaltyType(..)
,AmortPlan(..),Loan(..),Mortgage(..),AssetUnion(..),MixedAsset(..),FixedAsset(..)
,AmortRule(..),Capacity(..),AssociateExp(..),AssociateIncome(..),ReceivableFeeType(..),Receivable(..)
,ProjectedCashflow(..)
,calcAssetPrinInt, calcPmt
)
where
Expand Down Expand Up @@ -171,6 +172,16 @@ data Mortgage = Mortgage OriginalInfo Balance IRate RemainTerms (Maybe BorrowerN
| ScheduleMortgageFlow Date [CF.TsRow] DatePattern
deriving (Show,Generic,Eq,Ord)


type FixRatePortion = (Rate, IRate)
type FloatRatePortion = (Rate, Spread, Index)


data ProjectedCashflow = ProjectedFlowFixed CF.CashFlowFrame DatePattern
| ProjectedFlowMixFloater CF.CashFlowFrame DatePattern FixRatePortion [FloatRatePortion]
deriving (Show,Generic,Eq,Ord)


data Receivable = Invoice OriginalInfo Status
| DUMMY4
deriving (Show,Generic,Eq,Ord)
Expand Down Expand Up @@ -212,6 +223,7 @@ data AssetUnion = MO Mortgage
| LS Lease
| FA FixedAsset
| RE Receivable
| PF ProjectedCashflow
deriving (Show, Generic,Ord,Eq)

instance IR.UseRate AssetUnion where

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

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for

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

View workflow job for this annotation

GitHub Actions / build

• No explicit implementation for
Expand All @@ -221,6 +233,7 @@ instance IR.UseRate AssetUnion where
getIndex (LS ma) = IR.getIndex ma
getIndex (FA ma) = IR.getIndex ma
getIndex (RE ma) = IR.getIndex ma
getIndex (PF ma) = IR.getIndex ma


instance IR.UseRate Mortgage where
Expand All @@ -247,36 +260,31 @@ instance IR.UseRate FixedAsset where
instance IR.UseRate Receivable where
getIndex _ = Nothing

instance IR.UseRate ProjectedCashflow where
getIndex (ProjectedFlowFixed cf _) = Nothing

getIndex (ProjectedFlowMixFloater cf _ _ (f:fs)) = Just $ (\(a,b,c) -> c) f
getIndexes (ProjectedFlowMixFloater cf _ _ fs )
= Just $ (\(a,b,c) -> c) <$> fs


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


-- $(deriveJSON defaultOptions ''AmortRule)
-- $(deriveJSON defaultOptions ''Capacity)
$(deriveJSON defaultOptions ''AssociateExp)
$(deriveJSON defaultOptions ''AssociateIncome)
-- $(deriveJSON defaultOptions ''FixedAsset)
$(deriveJSON defaultOptions ''Status)
-- $(deriveJSON defaultOptions ''AmortPlan)
-- $(deriveJSON defaultOptions ''ReceivableFeeType)
-- $(deriveJSON defaultOptions ''OriginalInfo)
$(deriveJSON defaultOptions ''Installment)
$(deriveJSON defaultOptions ''LeaseStepUp)
$(deriveJSON defaultOptions ''Mortgage)
$(deriveJSON defaultOptions ''Loan)
$(deriveJSON defaultOptions ''Lease)
$(deriveJSON defaultOptions ''Receivable)
$(deriveJSON defaultOptions ''ProjectedCashflow)
$(deriveJSON defaultOptions ''AssetUnion)
-- $(deriveJSON defaultOptions ''PrepayPenaltyType)


-- instance ToSchema TsPoint
-- instance ToSchema (Ratio Integer)
instance ToSchema Capacity
instance ToSchema AmortRule
-- instance ToSchema (Ratio Integer)
instance ToSchema (Ratio Integer) where
declareNamedSchema _ = NamedSchema Nothing <$> declareSchema (Proxy :: Proxy Double)

Expand All @@ -295,7 +303,6 @@ instance ToSchema Direction
instance ToSchema AmortPlan
instance ToSchema DatePattern
instance ToSchema IR.RateType
-- instance ToSchema (IR.RoundingBy IR.RateType)
instance ToSchema CF.TsRow
instance ToSchema Period
instance ToSchema IR.ARM
Expand Down
3 changes: 3 additions & 0 deletions src/AssetClass/MixedAsset.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import AssetClass.Installment
import AssetClass.Receivable
import AssetClass.AssetCashflow
import AssetClass.FixedAsset
import AssetClass.ProjectedCashFlow

import Debug.Trace
import Assumptions (AssetDefaultAssumption(DefaultCDR))
Expand Down Expand Up @@ -165,6 +166,7 @@ calcAssetUnion (ACM.IL ast) d mRates = P.calcCashflow ast d mRates
calcAssetUnion (ACM.LS ast) d mRates = P.calcCashflow ast d mRates
calcAssetUnion (ACM.FA ast) d mRates = P.calcCashflow ast d mRates
calcAssetUnion (ACM.RE ast) d mRates = P.calcCashflow ast d mRates
calcAssetUnion (ACM.PF ast) d mRates = P.calcCashflow ast d mRates
calcAssetUnion x _ _ = error ("Failed to match proj AssetUnion"++ show x)

projAssetUnion :: ACM.AssetUnion -> Date -> A.AssetPerf -> Maybe [RateAssumption] -> (CF.CashFlowFrame, Map.Map CutoffFields Balance)
Expand All @@ -174,6 +176,7 @@ projAssetUnion (ACM.IL ast) d assumps mRates = P.projCashflow ast d assumps mRat
projAssetUnion (ACM.LS ast) d assumps mRates = P.projCashflow ast d assumps mRates
projAssetUnion (ACM.FA ast) d assumps mRates = P.projCashflow ast d assumps mRates
projAssetUnion (ACM.RE ast) d assumps mRates = P.projCashflow ast d assumps mRates
projAssetUnion (ACM.PF ast) d assumps mRates = P.projCashflow ast d assumps mRates
projAssetUnion x _ _ _ = error ("Failed to match proj AssetUnion"++ show x)

projAssetUnionList :: [ACM.AssetUnion] -> Date -> A.ApplyAssumptionType -> Maybe [RateAssumption] -> (CF.CashFlowFrame, Map.Map CutoffFields Balance)
Expand Down
Loading

0 comments on commit c240965

Please sign in to comment.