compute compounding interest with an actual formula
This commit is contained in:
@@ -3,6 +3,7 @@ package ledger
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
"maps"
|
||||||
|
"math"
|
||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -159,27 +160,42 @@ func BPIsWithFixedGrowthPrediction(bpis BPIs, window time.Duration, pattern stri
|
|||||||
func RegisterWithCompoundingInterestPrediction(reg Register, window time.Duration, pattern string, apy float64) (Register, error) {
|
func RegisterWithCompoundingInterestPrediction(reg Register, window time.Duration, pattern string, apy float64) (Register, error) {
|
||||||
lastBalances := make(Balances)
|
lastBalances := make(Balances)
|
||||||
p := regexp.MustCompile(pattern)
|
p := regexp.MustCompile(pattern)
|
||||||
for _, date := range reg.Dates() {
|
for _, d := range reg.Dates() {
|
||||||
for name := range reg[date] {
|
if t, _ := dateToTime(d); time.Now().Before(t) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for name := range reg[d] {
|
||||||
if p.MatchString(name) {
|
if p.MatchString(name) {
|
||||||
lastBalances[name] = reg[date][name]
|
lastBalances[name] = reg[d][name]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
predictedTimes := predictionTimes(window)
|
||||||
result := maps.Clone(reg)
|
result := maps.Clone(reg)
|
||||||
for _, predictionTime := range predictionTimes(window) {
|
for _, predictionTime := range predictedTimes {
|
||||||
k := predictionTime.Format("2006-01")
|
k := predictionTime.Format("2006-01")
|
||||||
if _, ok := result[k]; !ok {
|
if _, ok := result[k]; !ok {
|
||||||
result[k] = make(Balances)
|
result[k] = make(Balances)
|
||||||
}
|
}
|
||||||
for name, balance := range lastBalances {
|
for k2, v2 := range lastBalances {
|
||||||
balance2 := maps.Clone(balance)
|
if _, ok := result[k][k2]; !ok {
|
||||||
for k := range balance2 {
|
result[k][k2] = maps.Clone(v2)
|
||||||
balance2[k] *= 1.0 + (apy / 12)
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, predictionTime := range predictedTimes {
|
||||||
|
k := predictionTime.Format("2006-01")
|
||||||
|
for name := range lastBalances {
|
||||||
|
for currency := range result[k][name] {
|
||||||
|
// A = P(1 + r/n)**nt
|
||||||
|
p := result[k][name][currency]
|
||||||
|
r := apy
|
||||||
|
n := 12.0
|
||||||
|
t := float64(predictionTime.Sub(time.Now()) / (time.Hour * 24 * 365))
|
||||||
|
result[k][name][currency] = p * math.Pow(1.0+(r/n), n*t)
|
||||||
}
|
}
|
||||||
result[k][name] = balance2
|
|
||||||
lastBalances[name] = balance2
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user