diff --git a/src/Analytics.hs b/src/Analytics.hs index 9081e3a3..3262be34 100644 --- a/src/Analytics.hs +++ b/src/Analytics.hs @@ -54,10 +54,10 @@ pv pc today d amt = -- ^ calculate present value in the future using constant rate pv2 :: IRate -> Date -> Date -> Amount -> Amount pv2 discount_rate today d amt = - realToFrac $ (realToFrac amt) * (1/denominator) + realToFrac $ (realToFrac amt) * (1/denominator) -- `debug` ("pv: cash"++ show amt++" deno"++ show denominator++">> rate"++show discount_rate) where denominator::Double = (1 + realToFrac discount_rate) ** (distance / 365) - distance::Double = fromIntegral $ daysBetween today d + distance::Double = fromIntegral $ daysBetween today d -- `debug` ("days betwwen"++ show (daysBetween today d)++">>"++ show d ++ ">>today>>"++ show today) -- ^ calcualte present value given a series of amount with dates pv3 :: Ts -> Date -> [Date] -> [Amount] -> Balance diff --git a/src/Cashflow.hs b/src/Cashflow.hs index ee3e3f5e..2f622887 100644 --- a/src/Cashflow.hs +++ b/src/Cashflow.hs @@ -254,7 +254,7 @@ addTsCF m1@(MortgageFlow d1 b1 p1 i1 prep1 def1 rec1 los1 rat1 mbn1 pn1 st1) m2@ p = (+) <$> pn1 <*> pn2 st = maxStats st1 st2 in - MortgageFlow d1 (min b1 b2) (p1 + p2) (i1 + i2) (prep1 + prep2) (def1 + def2) (rec1 + rec2) (los1+los2) (fromRational (weightedBy [b1,b2] (toRational <$> [rat1,rat2]))) bn p st `debug` ("weighted rate"++ show (weightedBy [b1,b2] (toRational <$> [rat1,rat2]))++"m1"++ show m1++"m2"++ show m2) + MortgageFlow d1 (min b1 b2) (p1 + p2) (i1 + i2) (prep1 + prep2) (def1 + def2) (rec1 + rec2) (los1+los2) (fromRational (weightedBy [b1,b2] (toRational <$> [rat1,rat2]))) bn p st addTsCF (MortgageDelinqFlow d1 b1 p1 i1 prep1 delinq1 def1 rec1 los1 rat1 mbn1 pn1 st1) (MortgageDelinqFlow d2 b2 p2 i2 prep2 delinq2 def2 rec2 los2 rat2 mbn2 pn2 st2) = let bn = min <$> mbn1 <*> mbn2 @@ -268,11 +268,10 @@ addTsCF (LoanFlow d1 b1 p1 i1 prep1 def1 rec1 los1 rat1 st1) (LoanFlow _ b2 p2 i addTsCF (LeaseFlow d1 b1 r1) (LeaseFlow d2 b2 r2) = LeaseFlow d1 (min b1 b2) (r1 + r2) sumTs :: [TsRow] -> Date -> TsRow -sumTs trs d = tsSetDate (foldr1 addTs trs) d +sumTs trs = tsSetDate (foldr1 addTs trs) - -sumTsCF :: [TsRow] -> Date -> TsRow -- ^ group cashflow from same entity by a single date +sumTsCF :: [TsRow] -> Date -> TsRow sumTsCF trs = tsSetDate (foldl1 addTsCF trs) -- `debug` ("Summing"++show trs++">>"++ show (tsSetDate (foldr1 addTsCF trs) d)) tsTotalCash :: TsRow -> Balance diff --git a/src/Deal.hs b/src/Deal.hs index 73d141b2..999ba96a 100644 --- a/src/Deal.hs +++ b/src/Deal.hs @@ -279,7 +279,7 @@ run t@TestDeal{accounts=accMap,fees=feeMap,triggers=mTrgMap,bonds=bndMap,status= collectedFlow = Map.map (over CF.cashflowTxn (cutBy Inc Past d)) poolFlowMap outstandingFlow = Map.map (over CF.cashflowTxn (cutBy Exc Future d)) poolFlowMap -- deposit cashflow to SPV from external pool cf - accs = depositPoolFlow (collects t) d collectedFlow accMap `debug` ("d"++ show d++">>>"++ show collectedFlow) + accs = depositPoolFlow (collects t) d collectedFlow accMap -- `debug` ("d"++ show d++">>>"++ show collectedFlow++"\n") dAfterDeposit = (appendCollectedCF d t collectedFlow) {accounts=accs} -- `debug` ("CF size collected"++ show (CF.getTsCashFlowFrame)) -- newScheduleFlowMap = Map.map (over CF.cashflowTxn (cutBy Exc Future d)) (fromMaybe Map.empty (getScheduledCashflow t Nothing)) @@ -741,7 +741,7 @@ getInits t@TestDeal{fees=feeMap,pool=thePool,status=status,bonds=bndMap} mAssump -> Map.map (\p -> P.aggPool (P.issuanceStat p) $ runPool p mAssumps (AP.interest =<< mNonPerfAssump)) pm -- (poolCf,historyStats) = P.aggPool $ runPool thePool mAssumps (AP.interest <*> mNonPerfAssump) -- `debug` ("agg pool flow") -- poolCfTs = cutBy Inc Future startDate $ CF.getTsCashFlowFrame poolCf -- `debug` ("Pool Cf in pool>>"++show poolCf++"\n start date"++ show startDate) - poolCfTsM = Map.map (\x -> cutBy Inc Future startDate (CF.getTsCashFlowFrame (fst x))) pCfM `debug` ("proj"++ show pCfM) + poolCfTsM = Map.map (\x -> cutBy Inc Future startDate (CF.getTsCashFlowFrame (fst x))) pCfM -- `debug` ("proj"++ show pCfM) -- poolAggCf = CF.aggTsByDates poolCfTs (getDates pActionDates) poolCfTsMwithBegRow = Map.map (\(x:xs) -> buildBegTsRow startDate x:x:xs) poolCfTsM poolAggCfM = Map.map (\x -> CF.aggTsByDates x (getDates pActionDates)) poolCfTsMwithBegRow @@ -755,7 +755,8 @@ getInits t@TestDeal{fees=feeMap,pool=thePool,status=status,bonds=bndMap} mAssump -> Map.map (\p -> P.aggPool (P.issuanceStat p) $ runPool p Nothing (AP.interest =<< mNonPerfAssump)) pm pTxnOfSpv = Map.map (\x -> cutBy Inc Future startDate (CF.getTsCashFlowFrame (fst x))) pScheduleCfM pTxnWithBegRow = Map.map (\(x:xs) -> buildBegTsRow startDate x:x:xs) pTxnOfSpv - pAggCfM = Map.map (\x -> CF.aggTsByDates x (getDates pActionDates)) pTxnWithBegRow + -- pAggCfM = Map.map (\x -> CF.aggTsByDates x (getDates pActionDates)) pTxnWithBegRow + pAggCfM = pTxnWithBegRow pUnstressedAfterCutoff = Map.map CF.CashFlowFrame pAggCfM -- if preclosing deal , issuance balance is using beg balance of projected cashflow -- if it is ongoing deal, issuance balance is user input ( deal is not aware of issuance balance as point of time) diff --git a/src/Deal/DealQuery.hs b/src/Deal/DealQuery.hs index 57b0ef9b..8a1e4c54 100644 --- a/src/Deal/DealQuery.hs +++ b/src/Deal/DealQuery.hs @@ -250,6 +250,12 @@ queryDeal t@TestDeal{accounts=accMap, bonds=bndMap, fees=feeMap, ledgers=ledgerM in sum $ maybe 0 (CF.mflowBalance . head . view CF.cashflowTxn) <$> scheduleFlowM + FutureCurrentSchedulePoolBegBalance mPns -> + let + scheduleFlowM = Map.elems $ view dealScheduledCashflow t + in + sum $ maybe 0 (CF.mflowBegBalance . head . view CF.cashflowTxn) <$> scheduleFlowM + FutureCurrentPoolBegBalance mPns -> let ltc = getLatestCollectFrame t mPns @@ -333,22 +339,22 @@ queryDeal t@TestDeal{accounts=accMap, bonds=bndMap, fees=feeMap, ledgers=ledgerM FuturePoolScheduleCfPv asOfDay pm mPns -> let pCfTxns = Map.map (maybe [] CF.getTsCashFlowFrame) $ getScheduledCashflow t mPns - txns = concat $ Map.elems pCfTxns - txnsCfs = CF.tsTotalCash <$> txns + txns = cutBy Exc Future asOfDay $ concat $ Map.elems pCfTxns + txnsCfs = CF.tsTotalCash <$> txns -- `debug` ("schedule cf as of "++ show asOfDay ++ ">>" ++ show txns) txnsDs = getDate <$> txns txnsRates = CF.mflowRate <$> txns - scheduleBal = queryDeal t (FutureCurrentSchedulePoolBalance mPns) + scheduleBal = queryDeal t (FutureCurrentSchedulePoolBegBalance mPns) curBal = queryDeal t (FutureCurrentPoolBalance mPns) factor = case scheduleBal of 0.00 -> 0 - _ -> curBal / scheduleBal - cfForPv = (factor *) <$> txnsCfs + _ -> curBal / scheduleBal -- `debug` ("cur Bal"++show curBal ++">> sheduleBal"++ show scheduleBal) + cfForPv = (factor *) <$> txnsCfs -- `debug` (">>> factor"++ show factor) pvs = case pm of PvRate r -> uncurry (A.pv2 (fromRational r) asOfDay) <$> zip txnsDs cfForPv PvByRef ds -> uncurry (A.pv2 (queryDealRate t ds) asOfDay) <$> zip txnsDs cfForPv _ -> error $ "Failed to use pricing method on pool" ++ show pm ++"on pool id"++ show mPns in - sum pvs -- `debug` ("Done with schedule pool pv") + sum pvs -- `debug` ("pvs"++ show pvs) CurrentBondBalanceOf bns -> sum $ L.bndBalance . (bndMap Map.!) <$> bns diff --git a/src/Types.hs b/src/Types.hs index 578ca5f4..604d414e 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -464,6 +464,7 @@ data DealStats = CurrentBondBalance | ReserveExcessAt Date [AccName] | FutureCurrentPoolBalance (Maybe [PoolId]) | FutureCurrentSchedulePoolBalance (Maybe [PoolId]) + | FutureCurrentSchedulePoolBegBalance (Maybe [PoolId]) | PoolScheduleCfPv PricingMethod (Maybe [PoolId]) | FuturePoolScheduleCfPv Date PricingMethod (Maybe [PoolId]) | FutureWaCurrentPoolBalance Date Date (Maybe [PoolId])