rm bad code
parent
3681d19cfd
commit
f49308d4d5
|
|
@ -3,97 +3,11 @@ package ledger
|
|||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"maps"
|
||||
"slices"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RegisterWithContributionPrediction(reg map[string]Balances, windowAsPercentOfTotalDuration float64) (map[string]Balances, error) {
|
||||
times, err := registerTimesInUnix(reg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
datesToPredict := func() []string {
|
||||
result := make([]string, 0)
|
||||
windowDuration := time.Second * time.Duration(float64(slices.Max(times)-slices.Min(times))*windowAsPercentOfTotalDuration)
|
||||
|
||||
lastReal := time.Unix(slices.Max(times), 0)
|
||||
lastPredicted := lastReal
|
||||
for lastPredicted.Before(lastReal.Add(windowDuration)) {
|
||||
lastPredicted = lastPredicted.Add(time.Hour * 24 * time.Duration((45 - lastPredicted.Day())))
|
||||
result = append(result, lastPredicted.Format("2006-01"))
|
||||
}
|
||||
return result
|
||||
}()
|
||||
log.Print(datesToPredict)
|
||||
|
||||
namesDatesBalance := func() map[string]map[string]Balance {
|
||||
result := make(map[string]map[string]Balance)
|
||||
for date, balances := range reg {
|
||||
for name, balance := range balances {
|
||||
if _, ok := result[name]; !ok {
|
||||
result[name] = make(map[string]Balance)
|
||||
}
|
||||
result[name][date] = balance
|
||||
}
|
||||
}
|
||||
return result
|
||||
}()
|
||||
|
||||
result := maps.Clone(reg)
|
||||
for name, datesBalance := range namesDatesBalance {
|
||||
dates := func() []int64 {
|
||||
result := make([]int64, 0)
|
||||
for k := range datesBalance {
|
||||
v, _ := registerTime(k)
|
||||
result = append(result, v.Unix())
|
||||
}
|
||||
slices.Sort(result)
|
||||
return result
|
||||
}()
|
||||
weightedAverageContribution := func() map[Currency]float64 {
|
||||
half := dates[len(dates)/2]
|
||||
threeQuarter := dates[int(3.0*(len(dates)/4.0))]
|
||||
sevenEighths := dates[int(7.0*(len(dates)/8.0))]
|
||||
get := func(a, b int64) []Balance {
|
||||
result := make([]Balance, 0)
|
||||
for date, balance := range datesBalance {
|
||||
v, _ := registerTime(date)
|
||||
if v2 := v.Unix(); a <= v2 && v2 <= b {
|
||||
result = append(result, balance)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
halfUp := get(half, threeQuarter)
|
||||
threeQuarterUp := get(threeQuarter, sevenEighths)
|
||||
sevenEighthsUp := get(sevenEighths, time.Now().Add(time.Hour*24*365).Unix())
|
||||
weightedSum := make(map[Currency]float64, 0)
|
||||
pushes := 0
|
||||
pushWithWeight := func(b []Balance, weight int) {
|
||||
for i, b2 := range b[1:] {
|
||||
for c := range b2 {
|
||||
v := b2[c] - b[i][c]
|
||||
for i := 0; i < weight; i++ {
|
||||
weightedSum[c] += v
|
||||
pushes += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pushWithWeight(halfUp, 1)
|
||||
pushWithWeight(threeQuarterUp, 2)
|
||||
pushWithWeight(sevenEighthsUp, 4)
|
||||
for k, v := range weightedSum {
|
||||
weightedSum[k] = v / float64(pushes)
|
||||
}
|
||||
return weightedSum
|
||||
}()
|
||||
panic(fmt.Sprintf("%s: %v", name, weightedAverageContribution))
|
||||
}
|
||||
return result, nil
|
||||
return reg, io.EOF
|
||||
}
|
||||
|
||||
func RegisterWithCompoundingInterestPrediction(reg map[string]Balances, windowAsPercentOfTotalDuration float64, name string, rate float64) (map[string]Balances, error) {
|
||||
|
|
@ -101,19 +15,7 @@ func RegisterWithCompoundingInterestPrediction(reg map[string]Balances, windowAs
|
|||
return result, io.EOF
|
||||
}
|
||||
|
||||
func registerTimesInUnix(reg map[string]Balances) ([]int64, error) {
|
||||
result := make([]int64, 0, len(reg))
|
||||
for k := range reg {
|
||||
v, err := registerTime(k)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, v.Unix())
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func registerTime(s string) (time.Time, error) {
|
||||
func dateToTime(s string) (time.Time, error) {
|
||||
for _, layout := range []string{
|
||||
"2006-01-02",
|
||||
"2006-01",
|
||||
|
|
|
|||
|
|
@ -5,9 +5,6 @@ import (
|
|||
)
|
||||
|
||||
func TestRegisterPredictContribution(t *testing.T) {
|
||||
// 1n t0 w weight 4
|
||||
// 2n t1 w weight 2
|
||||
// 4n t2 w weight 1
|
||||
input := map[string]Balances{
|
||||
"2001-01": Balances{"X": Balance{USD: 1}},
|
||||
"2001-02": Balances{"X": Balance{USD: 2}},
|
||||
|
|
@ -20,10 +17,6 @@ func TestRegisterPredictContribution(t *testing.T) {
|
|||
"2001-09": Balances{"X": Balance{USD: 12}},
|
||||
"2001-10": Balances{"X": Balance{USD: 16}},
|
||||
}
|
||||
// 1x4 weight 4 = 16
|
||||
// 2x2 weight 2 = 8
|
||||
// 4x1 weight 1 = 4
|
||||
// (16+8+4) / (4+2+1) = 4
|
||||
got, err := RegisterWithContributionPrediction(input, .12)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -31,9 +24,8 @@ func TestRegisterPredictContribution(t *testing.T) {
|
|||
if len(got) != len(input)+1 {
|
||||
t.Error(len(got))
|
||||
}
|
||||
if balance, ok := got["2001-11"]; !ok {
|
||||
if _, ok := got["2001-11"]; !ok {
|
||||
t.Error(ok)
|
||||
} else if balance["X"][USD] != 20.0 {
|
||||
t.Error(balance)
|
||||
}
|
||||
t.Logf("%+v", got)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue