-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_annualized_volatility.py
More file actions
61 lines (55 loc) · 1.6 KB
/
test_annualized_volatility.py
File metadata and controls
61 lines (55 loc) · 1.6 KB
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
import numpy as np
import torch
from qfeval_data import Data
from .util import timestamps
def test_annualized_volatility_basic() -> None:
ts = timestamps(4)
x = torch.tensor(
[
[101.0, 200.0],
[102.0, 205.0],
[100.0, 220.0],
[101.0, 210.0],
],
dtype=torch.float64,
)
data = Data.from_tensors({"price": x}, ts, np.array(["A", "B"]))
actual = data.annualized_volatility()
expected = np.nanstd(
np.array(
[
[102.0 / 101.0 - 1.0, 205.0 / 200.0 - 1.0],
[100.0 / 102.0 - 1.0, 220.0 / 205.0 - 1.0],
[101.0 / 100.0 - 1.0, 210.0 / 220.0 - 1.0],
],
dtype=np.float64,
),
axis=0,
ddof=0,
) * np.sqrt(365.25)
np.testing.assert_allclose(actual.price.array, expected, atol=1e-5)
def test_annualized_volatility_with_nans() -> None:
ts = timestamps(4)
x = torch.tensor(
[
[float("nan"), 200.0],
[100.0, float("nan")],
[102.0, 210.0],
[101.0, 205.0],
],
dtype=torch.float64,
)
data = Data.from_tensors({"price": x}, ts, np.array(["A", "B"]))
actual = data.annualized_volatility()
expected = np.nanstd(
np.array(
[
[102.0 / 100.0 - 1.0, 210.0 / 200.0 - 1.0],
[101.0 / 102.0 - 1.0, 205.0 / 210.0 - 1.0],
],
dtype=np.float64,
),
axis=0,
ddof=0,
) * np.sqrt(365.25)
np.testing.assert_allclose(actual.price.array, expected, atol=1e-5)