Contributions to AutoContributions

This commit is contained in:
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": {
prediction: Prediction{
NewContributionPredictor(ledger.Register{
NewAutoContributionPredictor(ledger.Register{
"2001-01": ledger.Balances{"X": ledger.Balance{"X": 100}}, // too old
"2001-02": ledger.Balances{"X": ledger.Balance{"X": 10000}}, // too old
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}},
want: ledger.Register{theseK: ledger.Balances{"X": ledger.Balance{"X": 105}}},
want: ledger.Register{theseK: ledger.Balances{"X": ledger.Balance{"X": 55}}},
},
"interest": {
prediction: Prediction{
@@ -88,24 +88,24 @@ func TestPredictionPredict(t *testing.T) {
"interest, contribution": {
prediction: Prediction{
NewInterestPredictor("X", "X", 12),
NewContributionPredictor(ledger.Register{
NewAutoContributionPredictor(ledger.Register{
monthsAgo2: ledger.Balances{"X": ledger.Balance{"X": 100}},
monthsAgo1: ledger.Balances{"X": ledger.Balance{"X": 700}},
}),
},
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": {
prediction: Prediction{
NewContributionPredictor(ledger.Register{
NewAutoContributionPredictor(ledger.Register{
monthsAgo2: ledger.Balances{"X": ledger.Balance{"X": 100}},
monthsAgo1: ledger.Balances{"X": ledger.Balance{"X": 700}},
}),
NewInterestPredictor("X", "X", 12),
},
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 {
monthlyRate := getMonthlyContributionRates(reg)
return newContributionPredictor(monthlyRate)
func NewAutoContributionPredictor(reg ledger.Register) Predictor {
monthlyRate := getMonthlyAutoContributionRates(reg)
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 {
months := float64(delta) / float64(month)
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 = 12 * month
contributions := getRecentContributions(reg, window)
result := make(map[string]ledger.Balance)
for name := range contributions {
result[name] = getMonthlyContributionRate(contributions[name], window)
result[name] = getMonthlyAutoContributionRate(contributions[name], window)
}
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{}
for _, balance := range contributions {
for currency := range balance {
@@ -86,13 +86,12 @@ func getMonthlyContributionRate(contributions []ledger.Balance, window time.Dura
}
result := make(ledger.Balance)
for currency := range currencies {
result[currency] = getMonthlyContributionRateForCurrency(contributions, window, currency)
result[currency] = getMonthlyAutoContributionRateForCurrency(contributions, window, currency)
}
return result
}
// TODO better than average
func getMonthlyContributionRateForCurrency(contributions []ledger.Balance, window time.Duration, currency ledger.Currency) float64 {
func getMonthlyAutoContributionRateForCurrency(contributions []ledger.Balance, window time.Duration, currency ledger.Currency) float64 {
values := []float64{}
for i := range contributions {
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{
ledger.Balance{"x": 2},
ledger.Balance{"x": 4},
ledger.Balance{"y": 3},
}
got := getMonthlyContributionRate(input, time.Hour*24*365/4)
got := getMonthlyAutoContributionRate(input, time.Hour*24*365/4)
if len(got) != 2 {
t.Error(got)
}
if got["x"] != 2 {
if got["x"] != 4 {
t.Error(got["x"])
}
if got["y"] != 1 {
if got["y"] != 3 {
t.Error(got["y"])
}
}
func TestNewContributionPredictor(t *testing.T) {
func TestNewAutoContributionPredictor(t *testing.T) {
name := "x"
currency := ledger.USD
predictor := newContributionPredictor(map[string]ledger.Balance{
predictor := newAutoContributionPredictor(map[string]ledger.Balance{
name: {currency: 10},
"y": {"XYZ": 3},
})