Contributions to AutoContributions

main
Bel LaPointe 2023-10-27 18:50:44 -06:00
parent 7d872ca92a
commit 6d97ce773b
5 changed files with 26 additions and 25 deletions

View File

@ -68,7 +68,7 @@ func TestPredictionPredict(t *testing.T) {
}, },
"contribution": { "contribution": {
prediction: Prediction{ prediction: Prediction{
NewContributionPredictor(ledger.Register{ NewAutoContributionPredictor(ledger.Register{
"2001-01": ledger.Balances{"X": ledger.Balance{"X": 100}}, // too old "2001-01": ledger.Balances{"X": ledger.Balance{"X": 100}}, // too old
"2001-02": ledger.Balances{"X": ledger.Balance{"X": 10000}}, // too old "2001-02": ledger.Balances{"X": ledger.Balance{"X": 10000}}, // too old
monthsAgo2: ledger.Balances{"X": ledger.Balance{"X": 100}}, monthsAgo2: ledger.Balances{"X": ledger.Balance{"X": 100}},
@ -76,7 +76,7 @@ func TestPredictionPredict(t *testing.T) {
}), }),
}, },
given: ledger.Balances{"X": ledger.Balance{"X": 5}}, given: ledger.Balances{"X": ledger.Balance{"X": 5}},
want: ledger.Register{theseK: ledger.Balances{"X": ledger.Balance{"X": 105}}}, want: ledger.Register{theseK: ledger.Balances{"X": ledger.Balance{"X": 55}}},
}, },
"interest": { "interest": {
prediction: Prediction{ prediction: Prediction{
@ -88,24 +88,24 @@ func TestPredictionPredict(t *testing.T) {
"interest, contribution": { "interest, contribution": {
prediction: Prediction{ prediction: Prediction{
NewInterestPredictor("X", "X", 12), NewInterestPredictor("X", "X", 12),
NewContributionPredictor(ledger.Register{ NewAutoContributionPredictor(ledger.Register{
monthsAgo2: ledger.Balances{"X": ledger.Balance{"X": 100}}, monthsAgo2: ledger.Balances{"X": ledger.Balance{"X": 100}},
monthsAgo1: ledger.Balances{"X": ledger.Balance{"X": 700}}, monthsAgo1: ledger.Balances{"X": ledger.Balance{"X": 700}},
}), }),
}, },
given: ledger.Balances{"X": ledger.Balance{"X": 5}}, given: ledger.Balances{"X": ledger.Balance{"X": 5}},
want: ledger.Register{theseK: ledger.Balances{"X": ledger.Balance{"X": 110}}}, want: ledger.Register{theseK: ledger.Balances{"X": ledger.Balance{"X": 60}}},
}, },
"contribution, interest": { "contribution, interest": {
prediction: Prediction{ prediction: Prediction{
NewContributionPredictor(ledger.Register{ NewAutoContributionPredictor(ledger.Register{
monthsAgo2: ledger.Balances{"X": ledger.Balance{"X": 100}}, monthsAgo2: ledger.Balances{"X": ledger.Balance{"X": 100}},
monthsAgo1: ledger.Balances{"X": ledger.Balance{"X": 700}}, monthsAgo1: ledger.Balances{"X": ledger.Balance{"X": 700}},
}), }),
NewInterestPredictor("X", "X", 12), NewInterestPredictor("X", "X", 12),
}, },
given: ledger.Balances{"X": ledger.Balance{"X": 5}}, given: ledger.Balances{"X": ledger.Balance{"X": 5}},
want: ledger.Register{theseK: ledger.Balances{"X": ledger.Balance{"X": 210}}}, want: ledger.Register{theseK: ledger.Balances{"X": ledger.Balance{"X": 110}}},
}, },
} }

View File

@ -42,12 +42,12 @@ func NewInterestPredictor(namePattern string, currencyPattern string, apy float6
} }
} }
func NewContributionPredictor(reg ledger.Register) Predictor { func NewAutoContributionPredictor(reg ledger.Register) Predictor {
monthlyRate := getMonthlyContributionRates(reg) monthlyRate := getMonthlyAutoContributionRates(reg)
return newContributionPredictor(monthlyRate) return newAutoContributionPredictor(monthlyRate)
} }
func newContributionPredictor(monthlyRate map[string]ledger.Balance) Predictor { func newAutoContributionPredictor(monthlyRate map[string]ledger.Balance) Predictor {
return func(given ledger.Balances, delta time.Duration) ledger.Balances { return func(given ledger.Balances, delta time.Duration) ledger.Balances {
months := float64(delta) / float64(month) months := float64(delta) / float64(month)
result := make(ledger.Balances) result := make(ledger.Balances)
@ -66,18 +66,18 @@ func newContributionPredictor(monthlyRate map[string]ledger.Balance) Predictor {
} }
} }
func getMonthlyContributionRates(reg ledger.Register) map[string]ledger.Balance { func getMonthlyAutoContributionRates(reg ledger.Register) map[string]ledger.Balance {
window := 6 * month window := 6 * month
window = 12 * month window = 12 * month
contributions := getRecentContributions(reg, window) contributions := getRecentContributions(reg, window)
result := make(map[string]ledger.Balance) result := make(map[string]ledger.Balance)
for name := range contributions { for name := range contributions {
result[name] = getMonthlyContributionRate(contributions[name], window) result[name] = getMonthlyAutoContributionRate(contributions[name], window)
} }
return result return result
} }
func getMonthlyContributionRate(contributions []ledger.Balance, window time.Duration) ledger.Balance { func getMonthlyAutoContributionRate(contributions []ledger.Balance, window time.Duration) ledger.Balance {
currencies := map[ledger.Currency]int{} currencies := map[ledger.Currency]int{}
for _, balance := range contributions { for _, balance := range contributions {
for currency := range balance { for currency := range balance {
@ -86,13 +86,12 @@ func getMonthlyContributionRate(contributions []ledger.Balance, window time.Dura
} }
result := make(ledger.Balance) result := make(ledger.Balance)
for currency := range currencies { for currency := range currencies {
result[currency] = getMonthlyContributionRateForCurrency(contributions, window, currency) result[currency] = getMonthlyAutoContributionRateForCurrency(contributions, window, currency)
} }
return result return result
} }
// TODO better than average func getMonthlyAutoContributionRateForCurrency(contributions []ledger.Balance, window time.Duration, currency ledger.Currency) float64 {
func getMonthlyContributionRateForCurrency(contributions []ledger.Balance, window time.Duration, currency ledger.Currency) float64 {
values := []float64{} values := []float64{}
for i := range contributions { for i := range contributions {
if v, ok := contributions[i][currency]; ok { if v, ok := contributions[i][currency]; ok {

View File

@ -80,28 +80,28 @@ func TestGetContributions(t *testing.T) {
} }
} }
func TestGetMonthlyContributionRate(t *testing.T) { func TestGetMonthlyAutoContributionRate(t *testing.T) {
input := []ledger.Balance{ input := []ledger.Balance{
ledger.Balance{"x": 2}, ledger.Balance{"x": 2},
ledger.Balance{"x": 4}, ledger.Balance{"x": 4},
ledger.Balance{"y": 3}, ledger.Balance{"y": 3},
} }
got := getMonthlyContributionRate(input, time.Hour*24*365/4) got := getMonthlyAutoContributionRate(input, time.Hour*24*365/4)
if len(got) != 2 { if len(got) != 2 {
t.Error(got) t.Error(got)
} }
if got["x"] != 2 { if got["x"] != 4 {
t.Error(got["x"]) t.Error(got["x"])
} }
if got["y"] != 1 { if got["y"] != 3 {
t.Error(got["y"]) t.Error(got["y"])
} }
} }
func TestNewContributionPredictor(t *testing.T) { func TestNewAutoContributionPredictor(t *testing.T) {
name := "x" name := "x"
currency := ledger.USD currency := ledger.USD
predictor := newContributionPredictor(map[string]ledger.Balance{ predictor := newAutoContributionPredictor(map[string]ledger.Balance{
name: {currency: 10}, name: {currency: 10},
"y": {"XYZ": 3}, "y": {"XYZ": 3},
}) })

View File

@ -114,7 +114,7 @@ func main() {
} }
prediction = append(prediction, ana.NewInterestPredictor(fields[0], fields[1], apy)) prediction = append(prediction, ana.NewInterestPredictor(fields[0], fields[1], apy))
case "autoContributions": case "autoContributions":
prediction = append(prediction, ana.NewContributionPredictor(register)) prediction = append(prediction, ana.NewAutoContributionPredictor(register))
default: default:
panic(k) panic(k)
} }

View File

@ -1,8 +1,6 @@
todo: todo:
- require at least N contributions in N months before autoContributions!=0 - require at least N contributions in N months before autoContributions!=0
- override contribution prediction
- ?prediction=contributions=x y z&prediction=contribution=x y z - ?prediction=contributions=x y z&prediction=contribution=x y z
- ana/legacy_* drop
- ana/predictor for bpi or de-legacy - ana/predictor for bpi or de-legacy
- predictWhatIf=2023-10=AssetAccount:Cash=$=-7000 changes too much when removing 7k - predictWhatIf=2023-10=AssetAccount:Cash=$=-7000 changes too much when removing 7k
from prinicple from prinicple
@ -59,3 +57,7 @@ done:
ts: Fri Oct 27 17:24:16 MDT 2023 ts: Fri Oct 27 17:24:16 MDT 2023
- todo: ana/predictor.go contributions better than plain average - todo: ana/predictor.go contributions better than plain average
ts: Fri Oct 27 18:06:12 MDT 2023 ts: Fri Oct 27 18:06:12 MDT 2023
- todo: override contribution prediction
ts: Fri Oct 27 18:49:37 MDT 2023
- todo: ana/legacy_* drop
ts: Fri Oct 27 18:50:08 MDT 2023