101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package ana
|
|
|
|
import (
|
|
"maps"
|
|
"math"
|
|
"regexp"
|
|
"time"
|
|
|
|
"gogs.inhome.blapointe.com/ana-ledger/ledger"
|
|
)
|
|
|
|
type Predictor func(ledger.Balances, time.Duration) ledger.Balances
|
|
|
|
func NewInterestPredictor(namePattern string, currencyPattern string, apy float64) Predictor {
|
|
nameMatcher := regexp.MustCompile(namePattern)
|
|
currencyMatcher := regexp.MustCompile(currencyPattern)
|
|
return func(given ledger.Balances, delta time.Duration) ledger.Balances {
|
|
result := maps.Clone(given)
|
|
for k, v := range result {
|
|
result[k] = maps.Clone(v)
|
|
}
|
|
|
|
monthsPassed := float64(delta) / float64(time.Hour*24.0*365.0/12.0)
|
|
scalar := math.Pow(1.0+apy/12.0, monthsPassed)
|
|
for name := range result {
|
|
if !nameMatcher.MatchString(name) {
|
|
continue
|
|
}
|
|
for currency := range result[name] {
|
|
if !currencyMatcher.MatchString(string(currency)) {
|
|
continue
|
|
}
|
|
result[name][currency] *= scalar
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
}
|
|
|
|
func NewContributionPredictor(reg ledger.Register) Predictor {
|
|
monthlyRate := getMonthlyContributionRates(reg)
|
|
_ = monthlyRate
|
|
return func(given ledger.Balances, delta time.Duration) ledger.Balances {
|
|
panic(nil)
|
|
}
|
|
}
|
|
|
|
func getMonthlyContributionRates(reg ledger.Register) map[string]ledger.Balance {
|
|
window := time.Hour * 24 * 365 / 2
|
|
contributions := getRecentContributions(reg, window)
|
|
result := make(map[string]ledger.Balance)
|
|
for name := range contributions {
|
|
result[name] = getMonthlyContributionRate(contributions[name], window)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// TODO better than average
|
|
func getMonthlyContributionRate(contributions []ledger.Balance, window time.Duration) ledger.Balance {
|
|
sumPerCurrency := map[ledger.Currency]float64{}
|
|
for _, balance := range contributions {
|
|
for currency, v := range balance {
|
|
sumPerCurrency[currency] += v
|
|
}
|
|
}
|
|
result := make(ledger.Balance)
|
|
for currency, summed := range sumPerCurrency {
|
|
result[currency] = summed / (float64(window) / float64(time.Hour*24*365/12))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func getRecentContributions(reg ledger.Register, window time.Duration) map[string][]ledger.Balance {
|
|
return getContributions(reg.Between(time.Now().Add(-1*window), time.Now()))
|
|
}
|
|
|
|
func getContributions(reg ledger.Register) map[string][]ledger.Balance {
|
|
contributions := make(map[string][]ledger.Balance)
|
|
for _, date := range reg.Dates() {
|
|
for name := range reg[date] {
|
|
contributions[name] = append(contributions[name], make(ledger.Balance))
|
|
if len(contributions[name]) > 1 {
|
|
for k := range contributions[name][len(contributions[name])-2] {
|
|
contributions[name][len(contributions[name])-1][k] = 0
|
|
}
|
|
}
|
|
balance := contributions[name][len(contributions[name])-1]
|
|
for currency, value := range reg[date][name] {
|
|
balance[currency] = value
|
|
}
|
|
if forName := contributions[name]; len(forName) > 1 {
|
|
lastBalance := forName[len(forName)-2]
|
|
for currency := range lastBalance {
|
|
balance[currency] -= lastBalance[currency]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return contributions
|
|
}
|