bpi growth is also just compounding interest but ignore for now
This commit is contained in:
@@ -3,56 +3,86 @@ package ledger
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"maps"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RegisterWithContributionPrediction(reg map[string]Balances, windowAsPercentOfTotalDuration float64) (map[string]Balances, error) {
|
||||
func RegisterWithContributionPrediction(reg Register, window time.Duration) (Register, error) {
|
||||
return reg, io.EOF
|
||||
}
|
||||
|
||||
func BPIWithFixedGrowthPrediction(reg map[string]Balances, windowAsPercentOfTotalDuration float64, pattern string, rate float64) (map[string]Balances, error) {
|
||||
result := make(map[string]Balances)
|
||||
return result, io.EOF
|
||||
func BPIsWithFixedGrowthPrediction(bpis BPIs, window time.Duration, pattern string, apy float64) (BPIs, error) {
|
||||
last := map[Currency]struct {
|
||||
t string
|
||||
v float64
|
||||
}{}
|
||||
for currency, bpi := range bpis {
|
||||
for date, value := range bpi {
|
||||
if date > last[currency].t {
|
||||
was := last[currency]
|
||||
was.t = date
|
||||
was.v = value
|
||||
last[currency] = was
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result := make(BPIs)
|
||||
p := regexp.MustCompile(pattern)
|
||||
for currency, v := range bpis {
|
||||
result[currency] = maps.Clone(v)
|
||||
if p.MatchString(string(currency)) {
|
||||
for _, predictionTime := range predictionTimes(window) {
|
||||
k2 := predictionTime.Format("2006-01")
|
||||
was := last[currency]
|
||||
was.v *= 1.0 + apy
|
||||
result[currency][k2] = was.v
|
||||
last[currency] = was
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func RegisterWithCompoundingInterestPrediction(reg map[string]Balances, windowAsPercentOfTotalDuration float64, pattern string, rate float64) (map[string]Balances, error) {
|
||||
nameToLastDate := func() map[string]string {
|
||||
result := map[string]string{}
|
||||
for date, balances := range reg {
|
||||
for name := range balances {
|
||||
if result[name] < date {
|
||||
result[name] = date
|
||||
}
|
||||
func RegisterWithCompoundingInterestPrediction(reg Register, window time.Duration, pattern string, apy float64) (Register, error) {
|
||||
lastBalances := make(Balances)
|
||||
p := regexp.MustCompile(pattern)
|
||||
for _, date := range reg.Dates() {
|
||||
for name := range reg[date] {
|
||||
if p.MatchString(name) {
|
||||
lastBalances[name] = reg[date][name]
|
||||
}
|
||||
}
|
||||
return result
|
||||
}()
|
||||
firstTime, lastTime := func() (time.Time, time.Time) {
|
||||
var latest time.Time
|
||||
first := time.Now()
|
||||
for _, v := range nameToLastDate {
|
||||
v2, _ := dateToTime(v)
|
||||
if latest.Before(v2) {
|
||||
latest = v2
|
||||
}
|
||||
if first.Before(v2) {
|
||||
first = v2
|
||||
}
|
||||
}
|
||||
return first, latest
|
||||
}()
|
||||
log.Println("first", firstTime, "last time", lastTime, "nameToDate", nameToLastDate)
|
||||
|
||||
lastPrediction := lastTime
|
||||
for lastPrediction.Before(lastTime.Add(lastTime.Sub(firstTime) * time.Duration(windowAsPercentOfTotalDuration))) {
|
||||
lastPrediction = lastPrediction.Add(time.Hour * 24 * (45 - lastPrediction.Day()))
|
||||
TODO ^
|
||||
}
|
||||
|
||||
result := maps.Clone(reg)
|
||||
return result, io.EOF
|
||||
for _, predictionTime := range predictionTimes(window) {
|
||||
k := predictionTime.Format("2006-01")
|
||||
if _, ok := result[k]; !ok {
|
||||
result[k] = make(Balances)
|
||||
}
|
||||
for name, balance := range lastBalances {
|
||||
balance2 := maps.Clone(balance)
|
||||
for k := range balance2 {
|
||||
balance2[k] *= 1.0 + (apy / 12)
|
||||
}
|
||||
result[k][name] = balance2
|
||||
lastBalances[name] = balance2
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func predictionTimes(window time.Duration) []time.Time {
|
||||
result := []time.Time{}
|
||||
last := time.Now()
|
||||
for last.Before(time.Now().Add(window)) {
|
||||
last = last.Add(-1 * time.Hour * 24 * time.Duration(last.Day())).Add(time.Hour * 24 * 33)
|
||||
result = append(result, last)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func dateToTime(s string) (time.Time, error) {
|
||||
|
||||
Reference in New Issue
Block a user