{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Warp layer\n\nThe `layers_warp` allows for arbitrary warping of the input tensor **x** along the time\n(lookback) dimension. One needs to provide element by element transformation with values in [-1, 1].\nNote that this transformation can be either seen as a hyperparameter, collection of learnable parameters\n(one per training set) or predicted for each sample.\n\nTo illustrate how to use this layer, let us assume that we have a single asset. We have observed\nits returns over the :code:`lookback=50` previous days. Below we demonstrate 5 different\ntransformations to the original time series.\n\n- **identity** - no change\n- **zoom** - focusing on the last 25 days\n- **backwards** - swap the time flow\n- **slowdown_start** - slow down the beginning of the time series and speed up the end\n- **slowdown_end** - speed up the beginning of the time series and slow down the end\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\nimport torch\n\nfrom deepdow.data.synthetic import sin_single\nfrom deepdow.layers import Warp\n\nlookback = 50\n\nx_np = (np.linspace(0, 1, num=lookback) * sin_single(lookback, freq=4 / lookback))[None, None, :, None]\nx = torch.as_tensor(x_np)\n\ngrid = torch.linspace(0, end=1, steps=lookback)[None, :].to(dtype=x.dtype)\n\ntransform_dict = {\n    'identity': lambda x: 2 * (x - 0.5),\n    'zoom': lambda x: x,\n    'backwards': lambda x: -2 * (x - 0.5),\n    'slowdown\\_start': lambda x: 2 * (x ** 3 - 0.5),\n    'slowdown\\_end': lambda x: 2 * (x ** (1 / 3) - 0.5),\n}\n\nn_tforms = len(transform_dict)\n\n_, axs = plt.subplots(n_tforms, 2, figsize=(16, 3 * n_tforms), sharex=True, sharey=True)\nlayer = Warp()\n\nfor i, (tform_name, tform_lambda) in enumerate(transform_dict.items()):\n    tform = tform_lambda(grid)\n    x_warped = layer(x, tform)\n\n    axs[i, 0].plot(tform.numpy().squeeze(), linewidth=3, color='red')\n    axs[i, 1].plot(x_warped.numpy().squeeze(), linewidth=3, color='blue')\n    axs[i, 0].set_title(r'$\\bf{}$ tform'.format(tform_name))\n    axs[i, 1].set_title(r'$\\bf{}$ warped'.format(tform_name))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}