-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfxsim_test.go
86 lines (72 loc) · 2.68 KB
/
fxsim_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"math"
"testing"
)
type simParameters struct {
expectedFwdRate float64
volatility float64
timeToMaturity uint
lengthOfSimulation uint
}
var (
epsilon = 0.005
standardCase = simParameters{1.5, .05, 10, 1000}
zeroValueInputsCase = simParameters{0, 0, 0, 0}
)
func TestFxForwardCorrectness(t *testing.T) {
t.Run("Must have mean = ln(F0)/T-.5*vol^2", func(t *testing.T) {
samples := produceModifiedSimDataForTesting(standardCase)
mean := CalculateMean(samples)
target := math.Log(standardCase.expectedFwdRate)/float64(standardCase.timeToMaturity) -
0.5*math.Pow(standardCase.volatility, 2)
lowerBoundTarget := target - epsilon
upperBoundTarget := target + epsilon
if mean < lowerBoundTarget || mean > upperBoundTarget {
t.Errorf("mean = %v instead of %v, ± %v", mean, target, epsilon)
}
})
t.Run("Must have standardDeviation = volatility", func(t *testing.T) {
samples := produceModifiedSimDataForTesting(standardCase)
standardDeviation := CalculateStandardDeviation(samples)
lowerBoundTarget := standardCase.volatility - epsilon
upperBoundTarget := standardCase.volatility + epsilon
if standardDeviation < lowerBoundTarget || standardDeviation > upperBoundTarget {
t.Errorf("standardDeviation = %v instead of %v, ± %v", standardDeviation,
standardCase.volatility, epsilon)
}
})
t.Run("Zeros as input produce zeros as output", func(t *testing.T) {
samples := produceModifiedSimDataForTesting(zeroValueInputsCase)
lowerBoundTarget := -epsilon
upperBoundTarget := epsilon
t.Run("Must have mean = 0", func(t *testing.T) {
mean := CalculateMean(samples)
if mean < lowerBoundTarget || mean > upperBoundTarget {
t.Error("mean =", mean, "instead of 0, within range of", epsilon)
}
})
t.Run("Must have standardDeviation = 0", func(t *testing.T) {
standardDeviation := CalculateStandardDeviation(samples)
if standardDeviation < lowerBoundTarget || standardDeviation > upperBoundTarget {
t.Error("standardDeviation =", standardDeviation, "instead of 0, within range of",
epsilon)
}
})
})
}
func produceModifiedSimDataForTesting(simParms simParameters) []float64 {
samples := FxForward(simParms.expectedFwdRate, simParms.volatility,
simParms.timeToMaturity, simParms.lengthOfSimulation)
modifyFxForward(samples, standardCase.timeToMaturity)
return samples
}
// modifyFxForward takes each sample s of the output of FxForward and applies ln(s)/T, where
// T is the time-to-maturity parameter.
func modifyFxForward(forwardSim []float64, timeToMaturity uint) {
for i, sample := range forwardSim {
if sample != 0.0 {
forwardSim[i] = math.Log(sample) / float64(timeToMaturity)
}
}
}