|
| 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 sys\n", |
| 17 | + "sys.path.append('..')\n", |
| 18 | + "\n", |
| 19 | + "from fracdiff import fracdiff\n", |
| 20 | + "import numpy as np\n", |
| 21 | + "import scipy.special\n", |
| 22 | + "\n", |
| 23 | + "import matplotlib.pyplot as plt\n", |
| 24 | + "%matplotlib inline\n", |
| 25 | + "\n", |
| 26 | + "#!pip install memory_profiler\n", |
| 27 | + "import line_profiler\n", |
| 28 | + "%load_ext line_profiler\n", |
| 29 | + "\n", |
| 30 | + "#!pip install memory_profiler\n", |
| 31 | + "import memory_profiler\n", |
| 32 | + "%load_ext memory_profiler" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "markdown", |
| 37 | + "metadata": {}, |
| 38 | + "source": [ |
| 39 | + "# Load Demo Data" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 2, |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [], |
| 47 | + "source": [ |
| 48 | + "with np.load('data/demo1.npz') as data:\n", |
| 49 | + " x = data['px'][:, 0]" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "# Modeling\n", |
| 57 | + "Let $x_t$ a time series, \n", |
| 58 | + "$t\\in\\mathbb{N}$ the time step,\n", |
| 59 | + "$\\Delta^d$ the difference operator of fractional order $d\\in\\mathbb{R}^+$,\n", |
| 60 | + "and $m$ the truncation order" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "markdown", |
| 65 | + "metadata": {}, |
| 66 | + "source": [ |
| 67 | + "**Formula 1** (see Schoffer, 2003, p.50)\n", |
| 68 | + "\n", |
| 69 | + "$$\n", |
| 70 | + "(\\Delta^d x)_t = \\sum_{k=0}^\\infty \\frac{\\Gamma(k - d)}{\\Gamma(k + 1) \\, \\Gamma(-d)} \\, x_{t-k} \\\\\n", |
| 71 | + "(\\Delta^d x)_t \\approx \\sum_{k=0}^m \\frac{\\Gamma(k - d)}{\\Gamma(k + 1) \\, \\Gamma(-d)} \\, x_{t-k}\n", |
| 72 | + "$$\n", |
| 73 | + "\n" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "markdown", |
| 78 | + "metadata": {}, |
| 79 | + "source": [ |
| 80 | + "**Formula 2** (see Balisarsingh, 2013, pp.9737-9742)\n", |
| 81 | + "\n", |
| 82 | + "$$\n", |
| 83 | + "(\\Delta^d x)_t = \\sum_{k=0}^\\infty (-1)^k \\frac{\\Gamma(d + 1)}{k! \\, \\Gamma(d - k + 1)} \\, x_{t-k} \\\\\n", |
| 84 | + "(\\Delta^d x)_t \\approx \\sum_{k=0}^m (-1)^k \\frac{\\Gamma(d + 1)}{k! \\, \\Gamma(d - k + 1)} \\, x_{t-k}\n", |
| 85 | + "$$\n" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "**Formula 3/4** (see Lopez, 2018, p.78, from the 'iterative estimation' formula; Jensen and Nielsen, 2014)\n", |
| 93 | + "\n", |
| 94 | + "$$\n", |
| 95 | + "(\\Delta^d x)_t = x_t + \\sum_{k=1}^\\infty \\left(\\prod_{i=1}^k \\frac{d - i + 1}{i} \\right) x_{t-k} \\\\\n", |
| 96 | + "(\\Delta^d x)_t \\approx x_t + \\sum_{k=1}^m \\left(\\prod_{i=1}^k \\frac{d - i + 1}{i} \\right) x_{t-k}\n", |
| 97 | + "$$" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": 3, |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [], |
| 105 | + "source": [ |
| 106 | + "def frac_weights_1(d: float, m: int) -> np.ndarray:\n", |
| 107 | + " w = np.empty((m + 1,))\n", |
| 108 | + " for k in range(m + 1):\n", |
| 109 | + " w[k] = scipy.special.gamma(k - d) \\\n", |
| 110 | + " / (scipy.special.gamma(k + 1) * scipy.special.gamma(-d))\n", |
| 111 | + " return w" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": 4, |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [ |
| 120 | + "def frac_weights_2(d: float, m: int) -> np.ndarray:\n", |
| 121 | + " w = np.empty((m + 1,))\n", |
| 122 | + " for k in range(m + 1):\n", |
| 123 | + " w[k] = np.power(-1, k) * scipy.special.gamma(d + 1) \\\n", |
| 124 | + " / (scipy.special.factorial(k) * scipy.special.gamma(d - k + 1))\n", |
| 125 | + " return w" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": 5, |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [ |
| 134 | + "def frac_weights_3(d: float, m: int) -> np.ndarray:\n", |
| 135 | + " w = np.empty((m + 1,))\n", |
| 136 | + " w[0] = 1\n", |
| 137 | + " for j in range(1, m + 1):\n", |
| 138 | + " w[j] = -w[j - 1] * ((d - j + 1) / j)\n", |
| 139 | + " return w" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": 6, |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [ |
| 148 | + "def frac_weights_4(d: float, m: int) -> np.ndarray:\n", |
| 149 | + " w = [1]\n", |
| 150 | + " for k in range(1, m + 1):\n", |
| 151 | + " w.append(-w[-1] * ((d - k + 1) / k))\n", |
| 152 | + " return np.array(w)" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": 7, |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [ |
| 160 | + { |
| 161 | + "name": "stdout", |
| 162 | + "output_type": "stream", |
| 163 | + "text": [ |
| 164 | + "0.003395536381106888\n", |
| 165 | + "0.003395536381106581\n", |
| 166 | + "0.003395536381106833\n", |
| 167 | + "0.003395536381106833\n" |
| 168 | + ] |
| 169 | + } |
| 170 | + ], |
| 171 | + "source": [ |
| 172 | + "d = 0.845\n", |
| 173 | + "m = 100 # truncation order\n", |
| 174 | + "\n", |
| 175 | + "w = frac_weights_1(d, m)\n", |
| 176 | + "print(np.nansum(w))\n", |
| 177 | + "\n", |
| 178 | + "w = frac_weights_2(d, m)\n", |
| 179 | + "print(np.nansum(w))\n", |
| 180 | + "\n", |
| 181 | + "w = frac_weights_3(d, m)\n", |
| 182 | + "print(np.nansum(w))\n", |
| 183 | + "\n", |
| 184 | + "w = frac_weights_4(d, m)\n", |
| 185 | + "print(np.nansum(w))" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "markdown", |
| 190 | + "metadata": {}, |
| 191 | + "source": [ |
| 192 | + "# Speed\n", |
| 193 | + "The list version of the iterative estimation (formula 4) is by far the fastest" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": 8, |
| 199 | + "metadata": {}, |
| 200 | + "outputs": [ |
| 201 | + { |
| 202 | + "name": "stdout", |
| 203 | + "output_type": "stream", |
| 204 | + "text": [ |
| 205 | + "669 µs ± 104 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", |
| 206 | + "2.73 ms ± 20 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n", |
| 207 | + "79 µs ± 6.34 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n", |
| 208 | + "38.2 µs ± 783 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" |
| 209 | + ] |
| 210 | + } |
| 211 | + ], |
| 212 | + "source": [ |
| 213 | + "%timeit w = frac_weights_1(d, m)\n", |
| 214 | + "%timeit w = frac_weights_2(d, m)\n", |
| 215 | + "%timeit w = frac_weights_3(d, m)\n", |
| 216 | + "%timeit w = frac_weights_4(d, m)" |
| 217 | + ] |
| 218 | + }, |
| 219 | + { |
| 220 | + "cell_type": "markdown", |
| 221 | + "metadata": {}, |
| 222 | + "source": [ |
| 223 | + "# Memory" |
| 224 | + ] |
| 225 | + }, |
| 226 | + { |
| 227 | + "cell_type": "code", |
| 228 | + "execution_count": 9, |
| 229 | + "metadata": {}, |
| 230 | + "outputs": [ |
| 231 | + { |
| 232 | + "name": "stdout", |
| 233 | + "output_type": "stream", |
| 234 | + "text": [ |
| 235 | + "peak memory: 87.59 MiB, increment: -0.13 MiB\n", |
| 236 | + "peak memory: 87.59 MiB, increment: 0.00 MiB\n", |
| 237 | + "peak memory: 87.60 MiB, increment: 0.01 MiB\n", |
| 238 | + "peak memory: 87.61 MiB, increment: 0.02 MiB\n" |
| 239 | + ] |
| 240 | + } |
| 241 | + ], |
| 242 | + "source": [ |
| 243 | + "%memit w = frac_weights_1(d, m)\n", |
| 244 | + "%memit w = frac_weights_2(d, m)\n", |
| 245 | + "%memit w = frac_weights_3(d, m)\n", |
| 246 | + "%memit w = frac_weights_4(d, m)" |
| 247 | + ] |
| 248 | + }, |
| 249 | + { |
| 250 | + "cell_type": "markdown", |
| 251 | + "metadata": {}, |
| 252 | + "source": [ |
| 253 | + "# References\n", |
| 254 | + "* Baliarsingh, P., 2013. Some new difference sequence spaces of fractional order and their dual spaces. Applied Mathematics and Computation 219, 9737–9742. https://doi.org/10.1016/j.amc.2013.03.073\n", |
| 255 | + "* Jensen, A.N., Nielsen, M.Ø., 2014. A Fast Fractional Difference Algorithm. Journal of Time Series Analysis 35, 428–436. https://doi.org/10.1111/jtsa.12074\n", |
| 256 | + "* Prado, M.L. de, 2018. Advances in Financial Machine Learning, 1st ed. Wiley.\n", |
| 257 | + "* Schoffer, O., n.d. Modellierung von Kapitalmarktrenditen mittels asymmetrischer GARCH-Modelle. Universitaet Dortmund.\n" |
| 258 | + ] |
| 259 | + } |
| 260 | + ], |
| 261 | + "metadata": { |
| 262 | + "kernelspec": { |
| 263 | + "display_name": "Python 3", |
| 264 | + "language": "python", |
| 265 | + "name": "python3" |
| 266 | + }, |
| 267 | + "language_info": { |
| 268 | + "codemirror_mode": { |
| 269 | + "name": "ipython", |
| 270 | + "version": 3 |
| 271 | + }, |
| 272 | + "file_extension": ".py", |
| 273 | + "mimetype": "text/x-python", |
| 274 | + "name": "python", |
| 275 | + "nbconvert_exporter": "python", |
| 276 | + "pygments_lexer": "ipython3", |
| 277 | + "version": "3.7.1" |
| 278 | + } |
| 279 | + }, |
| 280 | + "nbformat": 4, |
| 281 | + "nbformat_minor": 4 |
| 282 | +} |
0 commit comments