{ "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": [ "
| Stocks | \n", "S0[W 0.2, H 0.5] | \n", "S1[W 0.2, H 0.5] | \n", "S2[W 0.2, H 0.5] | \n", "S3[W 0.2, H 0.5] | \n", "S4[W 0.2, H 0.5] | \n", "
|---|---|---|---|---|---|
| Days | \n", "\n", " | \n", " | \n", " | \n", " | \n", " |
| 0 | \n", "100.000000 | \n", "100.000000 | \n", "100.000000 | \n", "100.000000 | \n", "100.000000 | \n", "
| 1 | \n", "98.805864 | \n", "100.935769 | \n", "99.326480 | \n", "100.900464 | \n", "100.649803 | \n", "
| 2 | \n", "99.723073 | \n", "99.709073 | \n", "98.300671 | \n", "99.884634 | \n", "101.809271 | \n", "
| 3 | \n", "100.481068 | \n", "98.845525 | \n", "99.570943 | \n", "98.878556 | \n", "100.721910 | \n", "
| 4 | \n", "99.870674 | \n", "97.846148 | \n", "100.461281 | \n", "99.783308 | \n", "99.528206 | \n", "
| 5 | \n", "101.124337 | \n", "96.897283 | \n", "101.310699 | \n", "98.637364 | \n", "100.581171 | \n", "
| 6 | \n", "102.006936 | \n", "95.770467 | \n", "100.074504 | \n", "99.746865 | \n", "101.208367 | \n", "
| 7 | \n", "103.332098 | \n", "94.844957 | \n", "101.280570 | \n", "100.450735 | \n", "100.356336 | \n", "
| 8 | \n", "104.518001 | \n", "94.306958 | \n", "102.217987 | \n", "99.473017 | \n", "101.303750 | \n", "
| 9 | \n", "105.458859 | \n", "93.176509 | \n", "100.824345 | \n", "100.715559 | \n", "100.136298 | \n", "
| 10 | \n", "106.641517 | \n", "92.403867 | \n", "101.862962 | \n", "99.497536 | \n", "99.235579 | \n", "