Quickstart

This tutorial serves as an overview of the main functionality of the Garpar system.

Imports

There are two important subpackages in Garpar:

  • optimize allows applying optimization models.

  • datasets contains tools for market simulation.

You would generally import them as:

[1]:
from garpar import datasets, optimize

The StocksSet class

Most of the time the system will interact between modules with instances of the StocksSet class. This has the following representation:

[2]:
datasets.make_risso_normal()

[2]:
Stocks S0[W 1.0, H 0.5] S1[W 1.0, H 0.5] S2[W 1.0, H 0.5] S3[W 1.0, H 0.5] S4[W 1.0, H 0.5] S5[W 1.0, H 0.5] S6[W 1.0, H 0.5] S7[W 1.0, H 0.5] S8[W 1.0, H 0.5] S9[W 1.0, H 0.5]
Days
0 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000
1 99.065999 98.943010 98.870946 101.020970 99.144085 101.165333 100.866787 101.098415 100.781085 99.161770
2 98.495978 98.033529 99.746861 100.088921 99.734814 100.046732 99.814953 102.157073 99.962743 98.466345
3 99.401361 96.992479 100.755250 101.299145 101.105087 101.115051 98.820358 103.449262 98.913343 97.287550
4 100.430859 95.998145 99.918503 102.195130 99.761927 102.341743 97.500216 104.694716 97.856405 98.331657
... ... ... ... ... ... ... ... ... ... ...
361 94.799009 123.512099 71.397879 96.163404 113.464423 97.507722 122.805756 103.926471 122.691534 88.212256
362 93.898891 124.178566 72.088034 97.353707 114.864078 98.429802 121.481688 104.825842 124.081521 89.298662
363 92.943748 125.339489 71.425678 96.341400 116.104265 97.428438 122.796321 103.741489 125.213111 90.260746
364 93.782885 126.384495 70.908587 97.303383 115.042431 96.523343 124.425271 104.939925 123.481412 88.998644
365 93.140639 125.191927 70.124121 96.375587 113.796693 95.445881 122.959811 103.892369 124.733861 89.972342
366 days x 10 stocks - W.Size 5

A StocksSet instance unifies the representation of a market and a portfolio, simplifying stock tracking and weight (W) management. It streamlines data handling and serves as a shared concept across all system modules. A market reflects the current stock values, while a portfolio consists (in this context) of assigned weights for each stock. We will explore these components in more detail throughout the tutorials. For now, let’s start with a basic example.

In this tutorial, we will simulate a market and apply an optimization model to demonstrate basic interactions with the system, providing an introduction to its functionality.

We can simulate a market by calling the make_risso_normal function from the datasets subpackage. This function will create an instance of StocksSet through a simulation. For consistency, we’ll specify just one parameter: random_state, which acts as the seed.

[3]:
ss = datasets.make_risso_normal(random_state=23)
ss

[3]:
Stocks S0[W 1.0, H 0.5] S1[W 1.0, H 0.5] S2[W 1.0, H 0.5] S3[W 1.0, H 0.5] S4[W 1.0, H 0.5] S5[W 1.0, H 0.5] S6[W 1.0, H 0.5] S7[W 1.0, H 0.5] S8[W 1.0, H 0.5] S9[W 1.0, H 0.5]
Days
0 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000
1 100.766064 101.126354 101.056170 100.520483 99.278068 101.231624 101.454794 101.363444 100.618480 98.850608
2 101.767974 100.260879 100.115244 99.516454 98.228567 100.214519 102.472212 100.074498 101.245075 98.005358
3 103.171863 101.187614 99.261945 100.561238 97.054380 99.457204 101.534985 100.942186 100.124496 97.030506
4 104.240403 102.423134 98.378702 101.551601 95.927476 100.246909 102.232905 100.196595 98.964000 96.428484
... ... ... ... ... ... ... ... ... ... ...
361 104.845268 121.219172 131.868621 104.611151 142.212483 106.411823 100.923496 92.259337 120.218776 90.228541
362 105.897108 122.295360 130.769070 103.710214 143.863287 107.679350 102.327765 93.440659 121.197158 91.080514
363 107.368403 123.454928 132.064844 104.970441 142.355648 108.778114 103.399001 92.368845 120.270729 90.469203
364 108.435637 122.318475 133.215786 105.868644 140.662842 107.785096 104.275516 91.656621 121.732478 91.496238
365 107.026179 121.488850 134.581393 104.821875 139.648724 109.153324 105.619600 90.699211 123.443885 92.091881
366 days x 10 stocks - W.Size 5

Now that we have a StocksSet instance, let’s apply an optimization model and observe how the weights change. To analyze the effect, focus on the Ws in the top section of the StocksSet representation and compare them with their updated values after applying the following model:

[4]:
mk = optimize.mean_variance.Markowitz(target_risk=0.1)
mk.optimize(ss)

[4]:
Stocks S0[W 3.711507e-09, H 0.5] S1[W 7.229484e-03, H 0.5] S2[W 3.709488e-01, H 0.5] S3[W 2.995300e-09, H 0.5] S4[W 5.030012e-01, H 0.5] S5[W 4.103622e-09, H 0.5] S6[W 3.458309e-09, H 0.5] S7[W 1.305153e-09, H 0.5] S8[W 1.188205e-01, H 0.5] S9[W 1.288363e-09, H 0.5]
Days
0 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000
1 100.766064 101.126354 101.056170 100.520483 99.278068 101.231624 101.454794 101.363444 100.618480 98.850608
2 101.767974 100.260879 100.115244 99.516454 98.228567 100.214519 102.472212 100.074498 101.245075 98.005358
3 103.171863 101.187614 99.261945 100.561238 97.054380 99.457204 101.534985 100.942186 100.124496 97.030506
4 104.240403 102.423134 98.378702 101.551601 95.927476 100.246909 102.232905 100.196595 98.964000 96.428484
... ... ... ... ... ... ... ... ... ... ...
361 104.845268 121.219172 131.868621 104.611151 142.212483 106.411823 100.923496 92.259337 120.218776 90.228541
362 105.897108 122.295360 130.769070 103.710214 143.863287 107.679350 102.327765 93.440659 121.197158 91.080514
363 107.368403 123.454928 132.064844 104.970441 142.355648 108.778114 103.399001 92.368845 120.270729 90.469203
364 108.435637 122.318475 133.215786 105.868644 140.662842 107.785096 104.275516 91.656621 121.732478 91.496238
365 107.026179 121.488850 134.581393 104.821875 139.648724 109.153324 105.619600 90.699211 123.443885 92.091881
366 days x 10 stocks - W.Size 5

The change in weights is what we aim for when applying optimization models.

This concludes the quickstart. We recommend continuing with this tutorial.