skfolio.population
.Population#
- class skfolio.population.Population(iterable)[source]#
Population Class.
A
Population
is a list ofPortfolio
orMultiPeriodPortfolio
or both.- Parameters:
- iterablelist[BasePortfolio]
The list of portfolios. Each item can be of type
Portfolio
and/orMultiPeriodPortfolio
. Empty list are accepted.
Methods
append
(item)Append portfolio to the end of the population list.
clear
(/)Remove all items from list.
composition
([names, tags, display_sub_ptf_name])Composition of the portfolios in the population.
copy
(/)Return a shallow copy of the list.
count
(value, /)Return number of occurrences of value.
extend
(other)Extend population list by appending elements from the iterable.
filter
([names, tags])Filter the Population of portfolios by names and tags.
index
(value[, start, stop])Return first index of value.
insert
(index, item)Insert portfolio before index.
max_measure
(measure[, names, tags])Returns the portfolio with the maximum measure.
measures
(measure[, names, tags])Vector of portfolios measures for each portfolio from the population filtered by names and tags.
measures_mean
(measure[, names, tags])Mean of portfolios measures for each portfolio from the population filtered by names and tags.
measures_std
(measure[, names, tags])Standard-deviation of portfolios measures for each portfolio from the population filtered by names and tags.
min_measure
(measure[, names, tags])Returns the portfolio with the minimum measure.
non_denominated_sort
([first_front_only])Fast non-dominated sorting.
plot_composition
([names, tags, ...])Plot the compositions of the portfolios in the population.
plot_cumulative_returns
([log_scale, idx, ...])Plot the population's portfolios cumulative returns.
plot_distribution
(measure_list[, tag_list, ...])Plot the population's distribution for each measure provided in the measure list.
plot_measures
(x, y[, z, to_surface, ...])Plot the 2D (or 3D) scatter points (or surface) of a given set of measures for each portfolio in the population.
pop
([index])Remove and return item at index (default last).
quantile
(measure, q[, names, tags])Returns the portfolio corresponding to the
q
quantile for a given portfolio measure.remove
(value, /)Remove first occurrence of value.
reverse
(/)Reverse IN PLACE.
set_portfolio_params
(**params)Set the parameters of all the portfolios.
sort
(*[, key, reverse])Sort the list in ascending order and return None.
sort_measure
(measure[, reverse, names, tags])Sort the population by a given portfolio measure and filter the portfolios by names and tags.
summary
([formatted, names, tags])Summary of the portfolios in the population
- clear(/)#
Remove all items from list.
- composition(names=None, tags=None, display_sub_ptf_name=True)[source]#
Composition of the portfolios in the population.
- Parameters:
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- display_sub_ptf_namebool, default=True
If this is set to True, each sub-portfolio name composing a multi-period portfolio is displayed.
- Returns:
- summaryDataFrame
Composition of the portfolios in the population.
- copy(/)#
Return a shallow copy of the list.
- count(value, /)#
Return number of occurrences of value.
- filter(names=None, tags=None)[source]#
Filter the Population of portfolios by names and tags. If both names and tags are provided, the intersection is returned.
- Parameters:
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- populationPopulation
A new population of portfolios filtered by names and tags.
- index(value, start=0, stop=sys.maxsize, /)#
Return first index of value.
Raises ValueError if the value is not present.
- max_measure(measure, names=None, tags=None)[source]#
Returns the portfolio with the maximum measure.
- Parameters:
- measure: Measure
The portfolio measure.
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- valuesBasePortfolio
The portfolio with maximum measure.
- measures(measure, names=None, tags=None)[source]#
Vector of portfolios measures for each portfolio from the population filtered by names and tags.
- Parameters:
- measureMeasure
The portfolio measure.
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- valuesndarray
The vector of portfolios measures.
- measures_mean(measure, names=None, tags=None)[source]#
Mean of portfolios measures for each portfolio from the population filtered by names and tags.
- Parameters:
- measureMeasure
The portfolio measure.
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- valuefloat
The mean of portfolios measures.
- measures_std(measure, names=None, tags=None)[source]#
Standard-deviation of portfolios measures for each portfolio from the population filtered by names and tags.
- Parameters:
- measureMeasure
The portfolio measure.
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- valuefloat
The standard-deviation of portfolios measures.
- min_measure(measure, names=None, tags=None)[source]#
Returns the portfolio with the minimum measure.
- Parameters:
- measureMeasure
The portfolio measure.
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- valuesBasePortfolio
The portfolio with minimum measure.
- non_denominated_sort(first_front_only=False)[source]#
Fast non-dominated sorting. Sort the portfolios into different non-domination levels. Complexity O(MN^2) where M is the number of objectives and N the number of portfolios.
- Parameters:
- first_front_onlybool, default=False
If this is set to True, only the first front is sorted and returned. The default is
False
.
- Returns:
- frontslist[list[int]]
A list of Pareto fronts (lists), the first list includes non-dominated portfolios.
- plot_composition(names=None, tags=None, display_sub_ptf_name=True)[source]#
Plot the compositions of the portfolios in the population.
- Parameters:
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- display_sub_ptf_namebool, default=True
If this is set to True, each sub-portfolio name composing a multi-period portfolio is displayed.
- Returns:
- plotFigure
Returns the plotly Figure object.
- plot_cumulative_returns(log_scale=False, idx=None, names=None, tags=None)[source]#
Plot the population’s portfolios cumulative returns. Non-compounded cumulative returns start at 0. Compounded cumulative returns are rescaled to start at 1000.
- Parameters:
- log_scalebool, default=False
If this is set to True, the cumulative returns are displayed with a logarithm scale on the y-axis and rebased at 1000. The cumulative returns must be compounded otherwise an exception is raise.
- idxslice | array, optional
Indexes or slice of the observations to plot. The default (
None
) is to take all observations.- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- plotFigure
Returns the plot Figure object.
- plot_distribution(measure_list, tag_list=None, n_bins=None, **kwargs)[source]#
Plot the population’s distribution for each measure provided in the measure list.
- Parameters:
- measure_listlist[Measure]
The list of portfolio measures. A different distribution is plotted per measure.
- tag_listlist[str], optional
If this is provided, an additional distribution is plotted per measure for each tag provided.
- n_binsint, optional
Sets the number of bins.
- Returns:
- plotFigure
Returns the plotly Figure object.
- plot_measures(x, y, z=None, to_surface=False, hover_measures=None, show_fronts=False, color_scale=None, names=None, tags=None, title='Portfolios')[source]#
Plot the 2D (or 3D) scatter points (or surface) of a given set of measures for each portfolio in the population.
- Parameters:
- xMeasure
The x-axis measure.
- yMeasure
The y-axis measure.
- zMeasure, optional
The z-axis measure.
- to_surfacebool, default=False
If this is set to True, a surface is estimated.
- hover_measureslist[Measure], optional
The list of measure to show on point hover.
- show_frontsbool, default=False
If this is set to True, the pareto fronts are highlighted. The default is
False
.- color_scaleMeasure | str, optional
If this is provided, a color scale is displayed.
- titlestr, default=”Portfolios”
The graph title. The default value is “Portfolios”.
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- plotFigure
Returns the plotly Figure object.
- pop(index=-1, /)#
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
- quantile(measure, q, names=None, tags=None)[source]#
Returns the portfolio corresponding to the
q
quantile for a given portfolio measure.- Parameters:
- measureMeasure
The portfolio measure.
- qfloat
The quantile value.
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- valuesBasePortfolio
Portfolio corresponding to the
q
quantile for the measure.
- remove(value, /)#
Remove first occurrence of value.
Raises ValueError if the value is not present.
- reverse(/)#
Reverse IN PLACE.
- set_portfolio_params(**params)[source]#
Set the parameters of all the portfolios.
- Parameters:
- **paramsAny
Portfolio parameters.
- Returns:
- selfPopulation
The Population instance.
- sort(*, key=None, reverse=False)#
Sort the list in ascending order and return None.
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).
If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.
The reverse flag can be set to sort in descending order.
- sort_measure(measure, reverse=False, names=None, tags=None)[source]#
Sort the population by a given portfolio measure and filter the portfolios by names and tags.
- Parameters:
- measureMeasure
The portfolio measure.
- reversebool, default=False
If this is set to True, the order is reversed.
- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- valuesPopulations
The sorted population.
- summary(formatted=True, names=None, tags=None)[source]#
Summary of the portfolios in the population
- Parameters:
- formattedbool, default=True
If this is set to True, the measures are formatted into rounded string with units. The default is
True
.- namesstr | list[str], optional
If provided, the population is filtered by portfolio names.
- tagsstr | list[str], optional
If provided, the population is filtered by portfolio tags.
- Returns:
- summarypandas DataFrame
The population’s portfolios summary