garpar.datasets package¶
Datasets subpackage of Garpar project.
This package provides everything related to StocksSets generation. It includes StocksSet makers for simulation of market data. It includes a base class for StocksSet makers and a base class for random entropy-based functions for market simulation. It also includes a base class for multi-sector StocksSet makers. Additionally, it includes a function to create a StocksSet from MERVAL data from January 2012 to August 2022.
- Key Features:
Entropy-based market simulation
Multi-sector StocksSet creation
MERVAL dataset
See also
https//doi.org/10.1016/j.ribaf.2008.02.005.
Subpackages¶
Submodules¶
garpar.datasets.ds_base module¶
Abstract classes for StocksSets makers.
This module provides abstract classes for StocksSet makers. StocksSetsMakerABC is the base class for all StocksSet makers. While RandomEntropyStocksSetMakerABC is the base class for all random entropy-based StocksSet makers.
- Key Features:
Entropy-based portfolio simulation
- class garpar.datasets.ds_base.RandomEntropyStocksSetMakerABC(*, entropy=0.5, random_state=None, n_jobs=None, verbose=0)¶
Bases:
StocksSetMakerABCAbstract class for generating StocksSets with entropy-based prices.
- abstractmethod get_window_loss_probability(window_size, entropy)¶
Abstract method to calculate the loss probability by window size.
- Parameters:
window_size (int) – Window size for StocksSet creation.
entropy (float) – Entropy parameter for StocksSet creation.
- Returns:
Loss probability for the given window size and entropy.
- Return type:
float
- Raises:
NotImplementedError – If the method is not implemented in a subclass.
- abstractmethod make_stock_price(price, loss, random)¶
Abstract method to calculate the stock price.
- Parameters:
price (float) – Initial price of the stock.
loss (bool) – Whether there is a loss on the current day.
random (numpy.random.Generator) – Random number generator.
- Returns:
Updated stock price based on loss and randomness.
- Return type:
float
- Raises:
NotImplementedError – If the method is not implemented in a subclass.
- make_stocks_set(*, window_size=5, days=365, stocks=10, price=100, weights=None)¶
Create a StocksSet instance with random prices.
- Parameters:
window_size (int, optional) – Window size for StocksSet creation (default is 5).
days (int, optional) – Number of days for StocksSet evaluation (default is 365).
stocks (int, optional) – Number of stocks in the StocksSet (default is 10).
price (int, float, or array-like, optional) – Initial price or prices of stocks (default is 100).
weights (array-like or None, optional) – Initial weights of stocks (default is None).
- Returns:
Object representing the generated stocks prices along multiple days.
- Return type:
- class garpar.datasets.ds_base.StocksSetMakerABC¶
Bases:
ModelABCAbstract base class for defining a stocks set maker.
This class provides a base for all stocks set makers in the project and ensures that all subclasses implement a make_stocks_set method.
- abstractmethod make_stocks_set(*, window_size=5, days=365, stocks=10, price=100, weights=None)¶
Abstract factory method to create a stocks set.
- Parameters:
window_size (int, optional) – Window size for stocks set creation (default is 5).
days (int, optional) – Number of days for stocks set evaluation (default is 365).
stocks (int, optional) – Number of stocks in the stocks set (default is 10).
price (float, optional) – Initial price for stocks (default is 100).
weights (array-like or None, optional) – Initial weights of stocks (default is None).
- Raises:
NotImplementedError – If the method is not implemented in a subclass.
garpar.datasets.multisector module¶
Multisector module for Garpar project.
This module provides a class for creating a multi-sector StocksSet using different sector makers.
- Key Features:
Multi-sector StocksSet creation
Example
>>> from garpar.datasets import make_multisector, RissoNormal, RissoUniform
>>> port = make_multisector(
... RissoNormal(),
... RissoUniform(),
... stocks=100,
... days=365,
... window_size=5,
... )
>>> port.as_prices()
- class garpar.datasets.multisector.MultiSector(makers)¶
Bases:
StocksSetMakerABCStocksSet maker for creating a multi-sector StocksSet.
- make_stocks_set(*, window_size=5, days=365, stocks=10, price=100, weights=None)¶
Create a multi-sector StocksSet based on specified parameters.
- Parameters:
window_size (int, optional) – Window size for StocksSet creation (default is 5).
days (int, optional) – Number of days for StocksSet evaluation (default is 365).
stocks (int, optional) – Number of stocks in the StocksSet (default is 10).
price (int, float, or array-like, optional) – Initial price or prices of stocks (default is 100).
weights (array-like or None, optional) – Initial weights of stocks (default is None).
- Returns:
StocksSet object representing the generated multi-sector stocks set.
- Return type:
- garpar.datasets.multisector.make_multisector(*makers, **kwargs)¶
Create a multi-sector StocksSet using specified sector makers.
This function creates a multi-sector stocks set by initializing a MultiSector object with unique names for each sector maker and then calling make_stocks_set with specified parameters.
- Parameters:
*makers (variable-length arguments) – Instances of StocksSetMakerABC representing sector makers.
**kwargs (keyword arguments) – Additional parameters passed to MultiSector.make_stocks_set.
- Returns:
StocksSet object representing the generated multi-sector stocks set.
- Return type:
Example
>>> from mymodule import make_multisector, CustomSectorMaker1, CustomSectorMaker2 >>> port = make_multisector( >>> CustomSectorMaker1(), >>> CustomSectorMaker2(), >>> window_size=7, >>> days=250, >>> stocks=15, >>> price=200, >>> weights=[0.2, 0.3, 0.5] >>> )
garpar.datasets.risso module¶
Risso StocksSet Makers.
This module provides generators that use the Risso informational entropy calculation to determine loss probabilities. Each generator also adheres to a specific distribution for generating stock prices.
- Key Features:
Entropy-based portfolio simulation
Examples
>>> import garpar
>>> ss = garpar.datasets.make_risso_normal(stocks=2, days=20)
>>> ss.as_prices()
>>> ss.as_returns()
or
>>> from garpar.datasets import RissoNormal
>>> maker = RissoNormal(
... mu=10,
... sigma=0.2,
... entropy=0.5,
... random_state=10,
... n_jobs=None,
... verbose=0
... )
>>> maker.make_stocks_set()
References
Risso, W. A. (2008). The informational efficiency and the financial crashes. https://doi.org/10.1016/j.ribaf.2008.02.005.
- class garpar.datasets.risso.RissoLevyStable(*, entropy=0.5, random_state=None, n_jobs=None, verbose=0, alpha=1.6411, beta=-0.0126, mu=0.0005, sigma=0.005)¶
Bases:
RissoMixin,RandomEntropyStocksSetMakerABCImplementation of a stocks set maker using Levy stable distribution.
This class extends RissoABC and implements a stocks set maker using Levy stable distribution model for daily returns.
- make_stock_price(price, loss, random)¶
Generate a new stock price.
This method calculates a new stock price based on the current price, the direction of daily return (loss or gain), and the parameters of the Levy stable distribution (alpha, beta, mu, sigma).
- Parameters:
price (float) – Current price of the stock.
loss (bool) – Flag indicating if it’s a loss day (True) or gain day (False).
random (numpy.random.Generator) – Random number generator instance.
- Returns:
New price of the stock after daily price change.
- Return type:
float
- class garpar.datasets.risso.RissoMixin¶
Bases:
objectImplementation of a stock set maker using Risso’s entropy calculation.
This class extends RandomEntropyStocksSetMakerABC and implements methods for calculating candidate entropies and selecting loss probabilities based on a given window size and target entropy.
- get_window_loss_probability(window_size, entropy, eps=None)¶
Get the loss probability that corresponds to the nearest entropy.
Get the loss probability that corresponds to the nearest candidate entropy value to the target entropy.
- Parameters:
window_size (int) – Size of the sliding window for entropy calculation.
entropy (float) – Target entropy value for portfolio optimization.
- Returns:
Loss probability that corresponds to the nearest candidate entropy value to the target entropy.
- Return type:
float
Example
>>> get_window_loss_probability(window_size=3, entropy=0.99) ... 0.33333333366666673
- class garpar.datasets.risso.RissoNormal(*, entropy=0.5, random_state=None, n_jobs=None, verbose=0, mu=0.01, sigma=0.002)¶
Bases:
RissoMixin,RandomEntropyStocksSetMakerABCImplementation of a stocks set maker using a normal distribution.
This class extends RissoABC and implements a portfolio maker using a normal distribution model for daily returns.
- make_stock_price(price, loss, random)¶
Generate a new stock price.
This method calculates a new stock price based on the current price, the direction of daily return (loss or gain), and the parameters of the normal distribution (mu, sigma).
- Parameters:
price (float) – Current price of the stock.
loss (bool) – Flag indicating if it’s a loss day (True) or gain day (False).
random (numpy.random.Generator) – Random number generator instance.
- Returns:
New price of the stock after daily price change.
- Return type:
float
- class garpar.datasets.risso.RissoUniform(*, entropy=0.5, random_state=None, n_jobs=None, verbose=0, low=0.01, high=0.05)¶
Bases:
RissoMixin,RandomEntropyStocksSetMakerABCImplementation of a StocksSets maker using a uniform distribution.
This class extends RissoABC and implements a portfolio maker using a uniform distribution model for daily returns.
- make_stock_price(price, loss, random)¶
Calculate a new stock price.
This method calculates a new stock price based on the current price, the direction of daily return (loss or gain), and the parameters of the normal distribution (low, high).
- Parameters:
price (float) – Current price of the stock.
loss (bool) – Flag indicating if it’s a loss day (True) or a gain day (False).
random (numpy.random.Generator) – Random number generator instance.
- Returns:
New price of the stock after simulating the daily return.
- Return type:
float
- garpar.datasets.risso.argnearest(arr, v)¶
Find the index of the element in the array arr that is closest to v.
- Parameters:
arr (array_like) – Input array.
v (scalar) – Value to which the elements of arr will be compared.
- Returns:
idx – Index of the element in arr that is closest to v.
- Return type:
int
Notes
If there are multiple elements at the same distance from v, the index of the first occurrence is returned.
Examples
>>> arr = np.array([1, 3, 5, 7, 9]) >>> argnearest(arr, 4) 1 >>> argnearest(arr, 6) 2 >>> argnearest(arr, 8) 4
- garpar.datasets.risso.make_risso_levy_stable(alpha=1.6411, beta=-0.0126, mu=0.0005, sigma=0.005, *, entropy=0.5, random_state=None, n_jobs=None, verbose=0, **kwargs)¶
Create a StocksSet instance using the RissoLevyStable maker.
This function is intended to create a StocksSet instance using the RissoLevyStable class and the make_stocks_set method, without directly instantiating the class.
- Parameters:
alpha (float, optional) – Shape parameter of the Levy stable distribution. Default is 1.6411.
beta (float, optional) – Scale parameter of the Levy stable distribution. Default is -0.0126.
mu (float, optional) – Location parameter (mean) of the Levy stable distribution. Default is 0.0005.
sigma (float, optional) – Scale parameter (spread) of the Levy stable distribution. Default is 0.005.
entropy (float, optional) – Entropy parameter controlling the randomness in stocks set creation. Default is 0.5.
random_state ({None, int, numpy.random.Generator}, optional) – Seed or Generator for the random number generator. Default is None.
n_jobs (int, optional) – Number of parallel jobs to run. Default is None.
verbose (int, optional) – Verbosity level. Default is 0.
- Returns:
StocksSet object representing the created stocks set.
- Return type:
- garpar.datasets.risso.make_risso_normal(mu=0.01, sigma=0.002, *, entropy=0.5, random_state=None, n_jobs=None, verbose=0, **kwargs)¶
Create a StocksSet instance using RissoNormal maker.
This function is intended to create a StocksSet instance using the RissoNormal class and the make_stocks_set method, without directly instantiating the class.
- Parameters:
mu (float, optional) – Mean of the normal distribution for daily returns. Default is 0.01.
sigma (float, optional) – Standard deviation of the normal distribution for daily returns. Default is 0.002.
entropy (float, optional) – Entropy parameter controlling the randomness in stocks set creation. Default is 0.5.
random_state ({None, int, numpy.random.Generator}, optional) – Seed or Generator for the random number generator. Default is None.
n_jobs (int, optional) – Number of parallel jobs to run. Default is None.
verbose (int, optional) – Verbosity level. Default is 0.
**kwargs – Additional keyword arguments passed to the make_stocks_set method of RissoNormal.
- Returns:
Generated stocks set instance.
- Return type:
- garpar.datasets.risso.make_risso_uniform(low=0.01, high=0.05, *, entropy=0.5, random_state=None, n_jobs=None, verbose=0, **kwargs)¶
Create a StocksSet instance using RissoUniform maker.
This function is intended to create a StocksSet instance using the RissoUniform class and the make_stocks_set method, without directly instantiating the class.
- Parameters:
low (float, optional) – Lower bound of the uniform distribution for daily returns. Default is 0.01.
high (float, optional) – Upper bound of the uniform distribution for daily returns. Default is 0.05.
entropy (float, optional) – Entropy parameter controlling the randomness in portfolio creation. Default is 0.5.
random_state ({None, int, numpy.random.Generator}, optional) – Seed or Generator for the random number generator. Default is None.
n_jobs (int, optional) – Number of parallel jobs to run. Default is None.
verbose (int, optional) – Verbosity level. Default is 0.
**kwargs – Additional keyword arguments passed to the make_stocks_set method of RissoUniform.
- Returns:
Generated portfolio instance.
- Return type: