Skip to content

Commit 77b7405

Browse files
committed
profile truncation order
1 parent 0485c5e commit 77b7405

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Load Packages"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import numpy as np\n",
17+
"\n",
18+
"#!pip install memory_profiler\n",
19+
"import memory_profiler\n",
20+
"%load_ext memory_profiler"
21+
]
22+
},
23+
{
24+
"cell_type": "markdown",
25+
"metadata": {},
26+
"source": [
27+
"# Load Data"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 2,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"with np.load('data/demo1.npz') as data:\n",
37+
" x = data['px'][:, 0]"
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"metadata": {},
43+
"source": [
44+
"# Find truncation order m\n",
45+
"The task is to find the first $m^*$ whereas \n",
46+
"\n",
47+
"$$\n",
48+
"|w_{m^*}| \\geq \\tau \\; \\wedge \\; |w_{m^*+1}| < \\tau\n",
49+
"$$\n",
50+
"\n",
51+
"(see \"Fixed-Width Window Fracdiff\" in Lopez, 2018, pp. 80--84)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 3,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"# numpy version\n",
61+
"def find_truncation_order1(d, tau=1e-5, mmax=20000):\n",
62+
" w = np.empty((mmax+1,))\n",
63+
" w[0] = 1\n",
64+
" for k in range(1, mmax+1):\n",
65+
" w[k] = -w[k-1] * ((d - k + 1) / k)\n",
66+
" if np.abs(w[k]) < tau:\n",
67+
" break\n",
68+
" return k-1, w[:k]"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 4,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"# list version\n",
78+
"def find_truncation_order2(d, tau=1e-5, mmax=20000):\n",
79+
" w = [1]\n",
80+
" for k in range(1, mmax+1):\n",
81+
" wk = -w[-1] * ((d - k + 1) / k)\n",
82+
" if abs(wk) < tau:\n",
83+
" break\n",
84+
" w.append(wk)\n",
85+
" return k-1, w"
86+
]
87+
},
88+
{
89+
"cell_type": "markdown",
90+
"metadata": {},
91+
"source": [
92+
"# Speed\n",
93+
"The `list` version is much faster"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": 5,
99+
"metadata": {},
100+
"outputs": [
101+
{
102+
"name": "stdout",
103+
"output_type": "stream",
104+
"text": [
105+
"8.01 ms ± 1.97 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
106+
"936 µs ± 129 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
107+
]
108+
}
109+
],
110+
"source": [
111+
"d = 0.345\n",
112+
"%timeit mstar, w = find_truncation_order1(d)\n",
113+
"%timeit mstar, w = find_truncation_order2(d)"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"metadata": {},
119+
"source": [
120+
"# Memory"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 6,
126+
"metadata": {},
127+
"outputs": [
128+
{
129+
"name": "stdout",
130+
"output_type": "stream",
131+
"text": [
132+
"peak memory: 62.33 MiB, increment: -0.14 MiB\n",
133+
"peak memory: 62.11 MiB, increment: 0.01 MiB\n"
134+
]
135+
}
136+
],
137+
"source": [
138+
"d = 0.345\n",
139+
"%memit mstar, w = find_truncation_order1(d)\n",
140+
"%memit mstar, w = find_truncation_order2(d)"
141+
]
142+
},
143+
{
144+
"cell_type": "markdown",
145+
"metadata": {},
146+
"source": [
147+
"# References\n",
148+
"* Prado, M.L. de, 2018. Advances in Financial Machine Learning, 1st ed. Wiley."
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": null,
154+
"metadata": {},
155+
"outputs": [],
156+
"source": []
157+
}
158+
],
159+
"metadata": {
160+
"kernelspec": {
161+
"display_name": "Python 3",
162+
"language": "python",
163+
"name": "python3"
164+
},
165+
"language_info": {
166+
"codemirror_mode": {
167+
"name": "ipython",
168+
"version": 3
169+
},
170+
"file_extension": ".py",
171+
"mimetype": "text/x-python",
172+
"name": "python",
173+
"nbconvert_exporter": "python",
174+
"pygments_lexer": "ipython3",
175+
"version": "3.7.1"
176+
}
177+
},
178+
"nbformat": 4,
179+
"nbformat_minor": 4
180+
}

0 commit comments

Comments
 (0)