diff --git a/allocation/equal.go b/allocation/equal.go index bccf3dc..1fe1eab 100644 --- a/allocation/equal.go +++ b/allocation/equal.go @@ -4,7 +4,7 @@ import ( "context" "time" - "github.com/portfoliotree/portfolio/calculations" + "github.com/portfoliotree/portfolio/calculate" "github.com/portfoliotree/portfolio/returns" ) @@ -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 } diff --git a/allocation/inverse_variance.go b/allocation/inverse_variance.go index ee0d96f..ef224dd 100644 --- a/allocation/inverse_variance.go +++ b/allocation/inverse_variance.go @@ -4,7 +4,7 @@ import ( "context" "time" - "github.com/portfoliotree/portfolio/calculations" + "github.com/portfoliotree/portfolio/calculate" "github.com/portfoliotree/portfolio/returns" ) @@ -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 } diff --git a/allocation/risk.go b/allocation/risk.go index 0d9cd94..01400ce 100644 --- a/allocation/risk.go +++ b/allocation/risk.go @@ -4,7 +4,7 @@ import ( "context" "time" - "github.com/portfoliotree/portfolio/calculations" + "github.com/portfoliotree/portfolio/calculate" "github.com/portfoliotree/portfolio/returns" ) @@ -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 } diff --git a/allocation/volatility.go b/allocation/volatility.go index de49474..76938d3 100644 --- a/allocation/volatility.go +++ b/allocation/volatility.go @@ -4,7 +4,7 @@ import ( "context" "time" - "github.com/portfoliotree/portfolio/calculations" + "github.com/portfoliotree/portfolio/calculate" "github.com/portfoliotree/portfolio/returns" ) @@ -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 } @@ -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 } diff --git a/calculations/annualize.go b/calculate/annualize.go similarity index 97% rename from calculations/annualize.go rename to calculate/annualize.go index dddd777..8177199 100644 --- a/calculations/annualize.go +++ b/calculate/annualize.go @@ -1,4 +1,4 @@ -package calculations +package calculate import ( "math" diff --git a/calculations/correlation.go b/calculate/correlation.go similarity index 95% rename from calculations/correlation.go rename to calculate/correlation.go index ce3b2fc..24b4af5 100644 --- a/calculations/correlation.go +++ b/calculate/correlation.go @@ -1,4 +1,4 @@ -package calculations +package calculate import ( "gonum.org/v1/gonum/stat" diff --git a/calculations/docs.go b/calculate/docs.go similarity index 97% rename from calculations/docs.go rename to calculate/docs.go index bf9213e..05b4e4a 100644 --- a/calculations/docs.go +++ b/calculate/docs.go @@ -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 diff --git a/calculations/optimizer.go b/calculate/optimizer.go similarity index 98% rename from calculations/optimizer.go rename to calculate/optimizer.go index 97d4066..321595a 100644 --- a/calculations/optimizer.go +++ b/calculate/optimizer.go @@ -1,4 +1,4 @@ -package calculations +package calculate import ( "context" diff --git a/calculations/returns.go b/calculate/returns.go similarity index 98% rename from calculations/returns.go rename to calculate/returns.go index a32bba8..4d71020 100644 --- a/calculations/returns.go +++ b/calculate/returns.go @@ -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. diff --git a/calculate/returns_test.go b/calculate/returns_test.go new file mode 100644 index 0000000..6bd4be1 --- /dev/null +++ b/calculate/returns_test.go @@ -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}) +} diff --git a/calculations/risk.go b/calculate/risk.go similarity index 98% rename from calculations/risk.go rename to calculate/risk.go index 1d20285..2ad89c8 100644 --- a/calculations/risk.go +++ b/calculate/risk.go @@ -1,4 +1,4 @@ -package calculations +package calculate import ( "errors" diff --git a/calculations/risk_test.go b/calculate/risk_test.go similarity index 99% rename from calculations/risk_test.go rename to calculate/risk_test.go index 61a8ccc..aab4a1a 100644 --- a/calculations/risk_test.go +++ b/calculate/risk_test.go @@ -1,4 +1,4 @@ -package calculations +package calculate import ( "fmt" diff --git a/calculations/weights.go b/calculate/weights.go similarity index 98% rename from calculations/weights.go rename to calculate/weights.go index bcadbdd..2e7a39b 100644 --- a/calculations/weights.go +++ b/calculate/weights.go @@ -1,4 +1,4 @@ -package calculations +package calculate import ( "context" diff --git a/calculations/returns_test.go b/calculations/returns_test.go deleted file mode 100644 index ade308d..0000000 --- a/calculations/returns_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package calculations_test - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/portfoliotree/portfolio/calculations" -) - -func TestDiscreteReturns(t *testing.T) { - assert.Equal(t, calculations.HoldingPeriodReturns([]float64{100, 50}), []float64{1}) - assert.Equal(t, calculations.HoldingPeriodReturns([]float64{50, 50}), []float64{0}) - assert.Equal(t, calculations.HoldingPeriodReturns([]float64{50, 100}), []float64{-0.5}) - assert.Equal(t, calculations.HoldingPeriodReturns([]float64{50, 100, 100}), []float64{-0.5, 0}) - assert.Len(t, calculations.HoldingPeriodReturns(nil), 0) - assert.Equal(t, calculations.HoldingPeriodReturns([]float64{50, 100, 100}), []float64{-0.5, 0}) -} diff --git a/returns/list.go b/returns/list.go index 7da4479..bd14e18 100644 --- a/returns/list.go +++ b/returns/list.go @@ -6,7 +6,7 @@ import ( "sort" "time" - "github.com/portfoliotree/portfolio/calculations" + "github.com/portfoliotree/portfolio/calculate" ) type List []Return @@ -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 } @@ -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) { @@ -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 { diff --git a/returns/table.go b/returns/table.go index c67993d..ace1aba 100644 --- a/returns/table.go +++ b/returns/table.go @@ -15,7 +15,7 @@ import ( "github.com/portfoliotree/round" - "github.com/portfoliotree/portfolio/calculations" + "github.com/portfoliotree/portfolio/calculate" ) type Table struct { @@ -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 { @@ -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 { @@ -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 { @@ -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 {