{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Optimizer creation\n", "\n", "Suppose we want to create a custom optimizer for a `StocksSet`. How can we do it? In this tutorial the structure of the optimizers will be shown, naming every definition needed for an optimizer to work. The tutorial then will finish with a creation of a dummy optimizer.\n", "\n", "Lets see then how does an optimizer works." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first thing to consider its the base class of every optimizer. There is a class named `OptimizerABC` that has the following method:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def optimize(self, ss):\n", " weights, metadata = self._calculate_weights(ss)\n", " return ss.copy(weights=weights, optimizer=metadata)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given this, we determine the need to implement a `_calculate_weights` function that provides the necessary data to create a new StocksSet instance with updated weights. That method is the core method of every optimizer. The weights definition.\n", "\n", "Say for example that we want to create a new optimizer that only returns a normalized vector of weights. We can do that by making something like this." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from garpar.optimize.opt_base import OptimizerABC\n", "import numpy as np\n", "\n", "class MyOptimizer(OptimizerABC):\n", " family = \"MyFamily\"\n", "\n", " def _calculate_weights(self, ss):\n", " return np.ones(len(ss.weights))/len(ss.weights), {\"name\": \"my_model\"}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we have defined an attribute named `family`, which represents the set of models it belongs to. For example, in this context, mean-variance is a family.\n", "\n", "Now that we have defined an optimizer, let's examine its output for a `StocksSet` instance." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
StocksS0[W 0.2, H 0.5]S1[W 0.2, H 0.5]S2[W 0.2, H 0.5]S3[W 0.2, H 0.5]S4[W 0.2, H 0.5]
Days
0100.000000100.000000100.000000100.000000100.000000
198.805864100.93576999.326480100.900464100.649803
299.72307399.70907398.30067199.884634101.809271
3100.48106898.84552599.57094398.878556100.721910
499.87067497.846148100.46128199.78330899.528206
5101.12433796.897283101.31069998.637364100.581171
6102.00693695.770467100.07450499.746865101.208367
7103.33209894.844957101.280570100.450735100.356336
8104.51800194.306958102.21798799.473017101.303750
9105.45885993.176509100.824345100.715559100.136298
10106.64151792.403867101.86296299.49753699.235579
\n", "
11 days x 5 stocks - W.Size 5\n", "
" ], "text/plain": [ "Stocks S0[W 0.2, H 0.5] S1[W 0.2, H 0.5] S2[W 0.2, H 0.5] \\\n", "Days \n", "0 100.000000 100.000000 100.000000 \n", "1 98.805864 100.935769 99.326480 \n", "2 99.723073 99.709073 98.300671 \n", "3 100.481068 98.845525 99.570943 \n", "4 99.870674 97.846148 100.461281 \n", "5 101.124337 96.897283 101.310699 \n", "6 102.006936 95.770467 100.074504 \n", "7 103.332098 94.844957 101.280570 \n", "8 104.518001 94.306958 102.217987 \n", "9 105.458859 93.176509 100.824345 \n", "10 106.641517 92.403867 101.862962 \n", "\n", "Stocks S3[W 0.2, H 0.5] S4[W 0.2, H 0.5] \n", "Days \n", "0 100.000000 100.000000 \n", "1 100.900464 100.649803 \n", "2 99.884634 101.809271 \n", "3 98.878556 100.721910 \n", "4 99.783308 99.528206 \n", "5 98.637364 100.581171 \n", "6 99.746865 101.208367 \n", "7 100.450735 100.356336 \n", "8 99.473017 101.303750 \n", "9 100.715559 100.136298 \n", "10 99.497536 99.235579 \n", "StocksSet [11 days x 5 stocks - W.Size 5]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from garpar.datasets import make_risso_normal\n", "\n", "opt = MyOptimizer()\n", "ss = make_risso_normal(random_state=42, stocks=5, days=10)\n", "opt.optimize(ss)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As expected, this optimizer assings weights with equal values.\n", "\n", "Thank you for visiting the tutorials for **Garpar**! We recommend to look in the [**API section**](../api/garpar.rst) for any documentation you might need." ] } ], "metadata": { "kernelspec": { "display_name": "garpar", "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.13.2" } }, "nbformat": 4, "nbformat_minor": 2 }