Skip to content

Commit

Permalink
refactor: rename calculations package to calculate
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Jul 17, 2024
1 parent 54e9939 commit d371021
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions allocation/equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

"github.com/portfoliotree/portfolio/calculations"
"github.com/portfoliotree/portfolio/calculate"
"github.com/portfoliotree/portfolio/returns"
)

Expand All @@ -15,6 +15,6 @@ type EqualWeights struct{}
func (*EqualWeights) Name() string { return EqualWeightsAlgorithmName }

func (*EqualWeights) PolicyWeights(_ context.Context, _ time.Time, _ returns.Table, ws []float64) ([]float64, error) {
calculations.EqualWeights(ws)
calculate.EqualWeights(ws)
return ws, nil
}
4 changes: 2 additions & 2 deletions allocation/inverse_variance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

"github.com/portfoliotree/portfolio/calculations"
"github.com/portfoliotree/portfolio/calculate"
"github.com/portfoliotree/portfolio/returns"
)

Expand All @@ -26,6 +26,6 @@ func (*EqualInverseVariance) PolicyWeights(_ context.Context, _ time.Time, asset
}

vols := assetReturns.RisksFromStdDev()
calculations.InverseVarianceWeights(ws, vols)
calculate.InverseVarianceWeights(ws, vols)
return ws, nil
}
4 changes: 2 additions & 2 deletions allocation/risk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

"github.com/portfoliotree/portfolio/calculations"
"github.com/portfoliotree/portfolio/calculate"
"github.com/portfoliotree/portfolio/returns"
)

Expand All @@ -25,6 +25,6 @@ func (*EqualRiskContribution) PolicyWeights(ctx context.Context, _ time.Time, as
return ws, err
}

err = calculations.EqualRiskContributionWeights(ctx, ws, assetReturns.RisksFromStdDev(), assetReturns.CorrelationMatrix())
err = calculate.EqualRiskContributionWeights(ctx, ws, assetReturns.RisksFromStdDev(), assetReturns.CorrelationMatrix())
return ws, err
}
6 changes: 3 additions & 3 deletions allocation/volatility.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

"github.com/portfoliotree/portfolio/calculations"
"github.com/portfoliotree/portfolio/calculate"
"github.com/portfoliotree/portfolio/returns"
)

Expand All @@ -26,7 +26,7 @@ func (*EqualVolatility) PolicyWeights(_ context.Context, _ time.Time, assetRetur
}

assetRisks := assetReturns.RisksFromStdDev()
calculations.EqualVolatilityWeights(ws, assetRisks)
calculate.EqualVolatilityWeights(ws, assetRisks)
return ws, nil
}

Expand All @@ -48,6 +48,6 @@ func (*EqualInverseVolatility) PolicyWeights(_ context.Context, _ time.Time, ass
}

vols := assetReturns.RisksFromStdDev()
calculations.EqualInverseVolatilityWeights(ws, vols)
calculate.EqualInverseVolatilityWeights(ws, vols)
return ws, nil
}
2 changes: 1 addition & 1 deletion calculations/annualize.go → calculate/annualize.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculations
package calculate

import (
"math"
Expand Down
2 changes: 1 addition & 1 deletion calculations/correlation.go → calculate/correlation.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculations
package calculate

import (
"gonum.org/v1/gonum/stat"
Expand Down
2 changes: 1 addition & 1 deletion calculations/docs.go → calculate/docs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package calculations implements various finance equations used in the rest of the package.
//
// Please remember, investing carries inherent risks including but not limited to the potential loss of principal. Past performance is no guarantee of future results. The data, equations, and calculations in these docs and code are for informational purposes only and should not be considered financial advice. It is important to carefully consider your own financial situation before making any investment decisions. You should seek the advice of a licensed financial professional before making any investment decisions. You should seek code review of an experienced software developer before consulting this library (or any library that imports it) to make investment decisions.
package calculations
package calculate
2 changes: 1 addition & 1 deletion calculations/optimizer.go → calculate/optimizer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculations
package calculate

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion calculations/returns.go → calculate/returns.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculations
package calculate

// HoldingPeriodReturns calculates the holding period returns between the given quotes
// The Return's Time field remains the zero value.
Expand Down
18 changes: 18 additions & 0 deletions calculate/returns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package calculate_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/portfoliotree/portfolio/calculate"
)

func TestDiscreteReturns(t *testing.T) {
assert.Equal(t, calculate.HoldingPeriodReturns([]float64{100, 50}), []float64{1})
assert.Equal(t, calculate.HoldingPeriodReturns([]float64{50, 50}), []float64{0})
assert.Equal(t, calculate.HoldingPeriodReturns([]float64{50, 100}), []float64{-0.5})
assert.Equal(t, calculate.HoldingPeriodReturns([]float64{50, 100, 100}), []float64{-0.5, 0})
assert.Len(t, calculate.HoldingPeriodReturns(nil), 0)
assert.Equal(t, calculate.HoldingPeriodReturns([]float64{50, 100, 100}), []float64{-0.5, 0})
}
2 changes: 1 addition & 1 deletion calculations/risk.go → calculate/risk.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculations
package calculate

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion calculations/risk_test.go → calculate/risk_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculations
package calculate

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion calculations/weights.go → calculate/weights.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculations
package calculate

import (
"context"
Expand Down
18 changes: 0 additions & 18 deletions calculations/returns_test.go

This file was deleted.

14 changes: 7 additions & 7 deletions returns/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sort"
"time"

"github.com/portfoliotree/portfolio/calculations"
"github.com/portfoliotree/portfolio/calculate"
)

type List []Return
Expand All @@ -19,7 +19,7 @@ func (list List) Swap(i, j int) { list[i], list[j] = list[j], list[i] }

func (list List) AddSpread(annualizedSpread float64) List {
result := slices.Clone(list)
s := math.Pow(1.0+annualizedSpread, 1.0/calculations.PeriodsPerYear) - 1
s := math.Pow(1.0+annualizedSpread, 1.0/calculate.PeriodsPerYear) - 1
for i := range result {
result[i].Value = result[i].Value + s
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (list List) Excess(other List) List {
}

func (list List) TimeWeightedReturn() float64 {
return calculations.TimeWeightedReturn(list.Values())
return calculate.TimeWeightedReturn(list.Values())
}

func (list List) EndAndStartDate() (end, start time.Time, _ error) {
Expand All @@ -104,21 +104,21 @@ func (list List) EndAndStartDate() (end, start time.Time, _ error) {

// Risk calls calculations.RiskFromStdDev
func (list List) Risk() float64 {
return calculations.RiskFromStdDev(list.Values())
return calculate.RiskFromStdDev(list.Values())
}

// AnnualizedRisk must receive at least 2 returns otherwise it returns 0
func (list List) AnnualizedRisk() float64 {
return calculations.AnnualizeRisk(list.Risk(), calculations.PeriodsPerYear)
return calculate.AnnualizeRisk(list.Risk(), calculate.PeriodsPerYear)
}

// AnnualizedTimeWeightedReturn must receive at least 2 returns otherwise it returns 0
func (list List) AnnualizedTimeWeightedReturn() float64 {
return calculations.AnnualizedTimeWeightedReturn(list.Values(), calculations.PeriodsPerYear)
return calculate.AnnualizedTimeWeightedReturn(list.Values(), calculate.PeriodsPerYear)
}

func (list List) AnnualizedArithmeticReturn() float64 {
return calculations.AnnualizedArithmeticReturn(list.Values(), calculations.PeriodsPerYear)
return calculate.AnnualizedArithmeticReturn(list.Values(), calculate.PeriodsPerYear)
}

func compareTimes(x, y time.Time) int {
Expand Down
14 changes: 7 additions & 7 deletions returns/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

"github.com/portfoliotree/round"

"github.com/portfoliotree/portfolio/calculations"
"github.com/portfoliotree/portfolio/calculate"
)

type Table struct {
Expand Down Expand Up @@ -257,17 +257,17 @@ func (table Table) Lists() []List {
}

func (table Table) CorrelationMatrix() [][]float64 {
return calculations.CorrelationMatrix(table.values)
return calculate.CorrelationMatrix(table.values)
}

func (table Table) ExpectedRisk(weights []float64) float64 {
risks := table.RisksFromStdDev()
r, _ := calculations.PortfolioVolatility(risks, weights, table.CorrelationMatrix())
r, _ := calculate.PortfolioVolatility(risks, weights, table.CorrelationMatrix())
return r
}

func (table Table) RiskFromStdDev(column int) float64 {
return calculations.RiskFromStdDev(table.values[column])
return calculate.RiskFromStdDev(table.values[column])
}

func (table Table) RisksFromStdDev() []float64 {
Expand All @@ -279,7 +279,7 @@ func (table Table) RisksFromStdDev() []float64 {
}

func (table Table) AnnualizedRisk(column int) float64 {
return calculations.AnnualizeRisk(table.RiskFromStdDev(column), calculations.PeriodsPerYear)
return calculate.AnnualizeRisk(table.RiskFromStdDev(column), calculate.PeriodsPerYear)
}

func (table Table) AnnualizedRisks() []float64 {
Expand All @@ -291,7 +291,7 @@ func (table Table) AnnualizedRisks() []float64 {
}

func (table Table) TimeWeightedReturn(column int) float64 {
return calculations.AnnualizedTimeWeightedReturn(table.values[column], calculations.PeriodsPerYear)
return calculate.AnnualizedTimeWeightedReturn(table.values[column], calculate.PeriodsPerYear)
}

func (table Table) TimeWeightedReturns() []float64 {
Expand All @@ -303,7 +303,7 @@ func (table Table) TimeWeightedReturns() []float64 {
}

func (table Table) AnnualizedArithmeticReturn(column int) float64 {
return calculations.AnnualizedArithmeticReturn(table.values[column], calculations.PeriodsPerYear)
return calculate.AnnualizedArithmeticReturn(table.values[column], calculate.PeriodsPerYear)
}

func (table Table) AnnualizedArithmeticReturns() []float64 {
Expand Down

0 comments on commit d371021

Please sign in to comment.