forked from domokane/FinancePy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestFinInterpolate.py
81 lines (55 loc) · 2.16 KB
/
TestFinInterpolate.py
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
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################
import numpy as np
import math
import matplotlib.pyplot as plt
import sys
sys.path.append("..")
from financepy.market.curves.FinInterpolator import FinInterpolator, FinInterpTypes
from FinTestCases import FinTestCases, globalTestCaseMode
testCases = FinTestCases(__file__, globalTestCaseMode)
PLOT_GRAPHS = False
###############################################################################
def test_FinInterpolate():
import time
xValues = np.array([0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 5.0, 10.0])
a = -0.1
b = 0.002
yValues = []
for x in xValues:
y = math.exp(a * x + b * x * x)
yValues.append(y)
yValues = np.array(yValues)
xInterpolateValues = np.linspace(0.0, 10.0, 20)
testCases.header("METHOD", "X", "Y_INTERPOLATED")
for interpType in FinInterpTypes:
yInterpValues = []
start = time.time()
interpolator = FinInterpolator(interpType)
interpolator.fit(xValues, yValues)
for x in xInterpolateValues:
y_int = interpolator.interpolate(x)
testCases.print(interpType, x, y_int)
yInterpValues.append(y_int)
end = time.time()
if PLOT_GRAPHS:
plt.figure(figsize=(12, 10))
plt.plot(xValues, yValues, color='r', marker='o')
plt.plot(xInterpolateValues, yInterpValues, color='b',
label=str(interpType))
plt.legend()
xp = np.array([0.2, 0.4, 0.45, 0.6, 0.82, 0.93, 0.99])
yp = np.array([0.4, 0.9, 0.32, 0.2, 0.22, 0.10, 0.28])
n = 10000
testCases.header("LABEL", "TIME")
interpolator = FinInterpolator(interpType)
interpolator.fit(xp, yp)
start = time.time()
for i in range(0, n):
interpolator.interpolate(0.8)
end = time.time()
testCases.print("10000 Interpolations", end - start)
###############################################################################
test_FinInterpolate()
testCases.compareTestCases()