2021-02-27 17:53:00 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-09-22 04:48:01 +00:00
|
|
|
const delta = 1e-9
|
2022-02-09 10:23:35 +00:00
|
|
|
|
2021-02-28 04:12:03 +00:00
|
|
|
func TestExponentialScale(t *testing.T) {
|
2021-02-27 17:53:00 +00:00
|
|
|
// graph see: https://www.desmos.com/calculator/ip0ijbcbbf
|
2021-02-28 03:57:25 +00:00
|
|
|
scale := ExponentialScale{
|
2021-02-27 17:53:00 +00:00
|
|
|
Domain: [2]float64{1000, 2000},
|
|
|
|
Range: [2]float64{0.001, 0.01},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := scale.Solve()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "f(x) = 0.001000 * 1.002305 ^ (x - 1000.000000)", scale.String())
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 0.001, scale.Call(1000.0), delta)
|
|
|
|
assert.InDelta(t, 0.01, scale.Call(2000.0), delta)
|
2021-02-27 17:53:00 +00:00
|
|
|
|
|
|
|
for x := 1000; x <= 2000; x += 100 {
|
|
|
|
y := scale.Call(float64(x))
|
|
|
|
t.Logf("%s = %f", scale.FormulaOf(float64(x)), y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 04:12:03 +00:00
|
|
|
func TestExponentialScale_Reverse(t *testing.T) {
|
|
|
|
scale := ExponentialScale{
|
|
|
|
Domain: [2]float64{1000, 2000},
|
|
|
|
Range: [2]float64{0.1, 0.001},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := scale.Solve()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "f(x) = 0.100000 * 0.995405 ^ (x - 1000.000000)", scale.String())
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 0.1, scale.Call(1000.0), delta)
|
|
|
|
assert.InDelta(t, 0.001, scale.Call(2000.0), delta)
|
2021-02-28 04:12:03 +00:00
|
|
|
|
|
|
|
for x := 1000; x <= 2000; x += 100 {
|
|
|
|
y := scale.Call(float64(x))
|
|
|
|
t.Logf("%s = %f", scale.FormulaOf(float64(x)), y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 17:53:00 +00:00
|
|
|
func TestLogScale(t *testing.T) {
|
2021-02-27 17:55:35 +00:00
|
|
|
// see https://www.desmos.com/calculator/q1ufxx5gry
|
2021-02-28 03:57:25 +00:00
|
|
|
scale := LogarithmicScale{
|
2021-02-27 17:53:00 +00:00
|
|
|
Domain: [2]float64{1000, 2000},
|
|
|
|
Range: [2]float64{0.001, 0.01},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := scale.Solve()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "f(x) = 0.001303 * log(x - 999.000000) + 0.001000", scale.String())
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 0.001, scale.Call(1000.0), delta)
|
|
|
|
assert.InDelta(t, 0.01, scale.Call(2000.0), delta)
|
2021-02-27 17:53:00 +00:00
|
|
|
for x := 1000; x <= 2000; x += 100 {
|
|
|
|
y := scale.Call(float64(x))
|
|
|
|
t.Logf("%s = %f", scale.FormulaOf(float64(x)), y)
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 18:06:33 +00:00
|
|
|
|
2021-02-27 18:20:47 +00:00
|
|
|
func TestLinearScale(t *testing.T) {
|
|
|
|
scale := LinearScale{
|
|
|
|
Domain: [2]float64{1000, 2000},
|
|
|
|
Range: [2]float64{3, 10},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := scale.Solve()
|
|
|
|
assert.NoError(t, err)
|
2023-03-08 09:09:58 +00:00
|
|
|
assert.Equal(t, "f(x) = 3.000000 + (x - 1000.000000) * 0.007000", scale.String())
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 3, scale.Call(1000), delta)
|
2023-03-10 05:51:29 +00:00
|
|
|
assert.InDelta(t, 6.5, scale.Call(1500), delta)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 10, scale.Call(2000), delta)
|
2021-02-27 18:20:47 +00:00
|
|
|
for x := 1000; x <= 2000; x += 100 {
|
|
|
|
y := scale.Call(float64(x))
|
|
|
|
t.Logf("%s = %f", scale.FormulaOf(float64(x)), y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:52:41 +00:00
|
|
|
func TestLinearScale2(t *testing.T) {
|
|
|
|
scale := LinearScale{
|
|
|
|
Domain: [2]float64{1, 3},
|
|
|
|
Range: [2]float64{0.1, 0.4},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := scale.Solve()
|
|
|
|
assert.NoError(t, err)
|
2023-03-08 09:09:58 +00:00
|
|
|
assert.Equal(t, "f(x) = 0.100000 + (x - 1.000000) * 0.150000", scale.String())
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 0.1, scale.Call(1), delta)
|
2023-03-10 05:51:29 +00:00
|
|
|
assert.InDelta(t, 0.25, scale.Call(2), delta)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 0.4, scale.Call(3), delta)
|
2021-05-09 18:52:41 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 09:09:58 +00:00
|
|
|
func TestLinearScaleNegative(t *testing.T) {
|
|
|
|
scale := LinearScale{
|
|
|
|
Domain: [2]float64{-1, 3},
|
|
|
|
Range: [2]float64{0.1, 0.4},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := scale.Solve()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "f(x) = 0.100000 + (x - -1.000000) * 0.075000", scale.String())
|
|
|
|
assert.InDelta(t, 0.1, scale.Call(-1), delta)
|
2023-03-10 05:51:29 +00:00
|
|
|
assert.InDelta(t, 0.25, scale.Call(1), delta)
|
2023-03-08 09:09:58 +00:00
|
|
|
assert.InDelta(t, 0.4, scale.Call(3), delta)
|
|
|
|
}
|
|
|
|
|
2021-02-27 18:06:33 +00:00
|
|
|
func TestQuadraticScale(t *testing.T) {
|
2021-02-27 18:07:48 +00:00
|
|
|
// see https://www.desmos.com/calculator/vfqntrxzpr
|
2021-02-27 18:06:33 +00:00
|
|
|
scale := QuadraticScale{
|
|
|
|
Domain: [3]float64{0, 100, 200},
|
|
|
|
Range: [3]float64{1, 20, 50},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := scale.Solve()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "f(x) = 0.000550 * x ^ 2 + 0.135000 * x + 1.000000", scale.String())
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 1, scale.Call(0), delta)
|
|
|
|
assert.InDelta(t, 20, scale.Call(100.0), delta)
|
|
|
|
assert.InDelta(t, 50.0, scale.Call(200.0), delta)
|
2021-02-27 18:06:33 +00:00
|
|
|
for x := 0; x <= 200; x += 1 {
|
|
|
|
y := scale.Call(float64(x))
|
|
|
|
t.Logf("%s = %f", scale.FormulaOf(float64(x)), y)
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 17:40:54 +00:00
|
|
|
|
|
|
|
func TestPercentageScale(t *testing.T) {
|
|
|
|
t.Run("from 0.0 to 1.0", func(t *testing.T) {
|
|
|
|
s := &PercentageScale{
|
|
|
|
ByPercentage: &SlideRule{
|
|
|
|
ExpScale: &ExponentialScale{
|
|
|
|
Domain: [2]float64{0.0, 1.0},
|
|
|
|
Range: [2]float64{1.0, 100.0},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := s.Scale(0.0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 1.0, v, delta)
|
2022-01-26 17:40:54 +00:00
|
|
|
|
|
|
|
v, err = s.Scale(1.0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 100.0, v, delta)
|
2022-01-26 17:40:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("from -1.0 to 1.0", func(t *testing.T) {
|
|
|
|
s := &PercentageScale{
|
|
|
|
ByPercentage: &SlideRule{
|
|
|
|
ExpScale: &ExponentialScale{
|
|
|
|
Domain: [2]float64{-1.0, 1.0},
|
|
|
|
Range: [2]float64{10.0, 100.0},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := s.Scale(-1.0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 10.0, v, delta)
|
2022-01-26 17:40:54 +00:00
|
|
|
|
|
|
|
v, err = s.Scale(1.0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 100.0, v, delta)
|
2022-01-26 17:40:54 +00:00
|
|
|
})
|
2022-01-26 17:47:01 +00:00
|
|
|
|
2022-01-26 18:32:26 +00:00
|
|
|
t.Run("reverse -1.0 to 1.0", func(t *testing.T) {
|
|
|
|
s := &PercentageScale{
|
|
|
|
ByPercentage: &SlideRule{
|
|
|
|
ExpScale: &ExponentialScale{
|
|
|
|
Domain: [2]float64{-1.0, 1.0},
|
|
|
|
Range: [2]float64{100.0, 10.0},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := s.Scale(-1.0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 100.0, v, delta)
|
2022-01-26 18:32:26 +00:00
|
|
|
|
|
|
|
v, err = s.Scale(1.0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 10.0, v, delta)
|
2022-01-26 18:39:33 +00:00
|
|
|
|
|
|
|
v, err = s.Scale(2.0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 10.0, v, delta)
|
2022-01-26 18:39:33 +00:00
|
|
|
|
|
|
|
v, err = s.Scale(-2.0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 100.0, v, delta)
|
2022-01-26 18:32:26 +00:00
|
|
|
})
|
|
|
|
|
2022-01-26 17:47:01 +00:00
|
|
|
t.Run("negative range", func(t *testing.T) {
|
|
|
|
s := &PercentageScale{
|
|
|
|
ByPercentage: &SlideRule{
|
|
|
|
ExpScale: &ExponentialScale{
|
|
|
|
Domain: [2]float64{0.0, 1.0},
|
|
|
|
Range: [2]float64{-100.0, 100.0},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := s.Scale(0.0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, -100.0, v, delta)
|
2022-01-26 17:47:01 +00:00
|
|
|
|
|
|
|
v, err = s.Scale(1.0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-22 04:48:01 +00:00
|
|
|
assert.InDelta(t, 100.0, v, delta)
|
2022-01-26 17:47:01 +00:00
|
|
|
})
|
2022-01-26 17:40:54 +00:00
|
|
|
}
|