skfolio.population.Population#

class skfolio.population.Population(iterable)[source]#

Population Class.

A Population is a list of Portfolio or MultiPeriodPortfolio or both.

Parameters:
iterablelist[BasePortfolio]

The list of portfolios. Each item can be of type Portfolio and/or MultiPeriodPortfolio. Empty list are accepted.

Methods

append(item)

Append portfolio to the end of the population list.

clear(/)

Remove all items from list.

composition([display_sub_ptf_name])

Composition of each portfolio in the population.

contribution(measure[, spacing, ...])

Contribution of each asset to a given measure of each portfolio 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)

Returns the portfolio with the maximum measure.

measures(measure)

Vector of portfolios measures for each portfolio from the population.

measures_mean(measure)

Mean of portfolios measures for each portfolio from the population.

measures_std(measure)

Standard-deviation of portfolios measures for each portfolio from the population.

min_measure(measure)

Returns the portfolio with the minimum measure.

non_denominated_sort([first_front_only])

Fast non-dominated sorting.

plot_composition([display_sub_ptf_name])

Plot the compositions of the portfolios in the population.

plot_contribution(measure[, spacing, ...])

Plot the contribution of each asset to a given measure 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.

plot_rolling_measure([measure, window])

Plot the measure over a rolling window for each portfolio in the population.

pop([index])

Remove and return item at index (default last).

quantile(measure, q)

Returns the portfolio corresponding to the q quantile for a given portfolio measure.

remove(value, /)

Remove first occurrence of value.

reverse(/)

Reverse IN PLACE.

rolling_measure([measure, window])

Compute the measure over a rolling window for each portfolio in the

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])

Sort the population by a given portfolio measure.

summary([formatted])

Summary of the portfolios in the population

append(item)[source]#

Append portfolio to the end of the population list.

clear(/)#

Remove all items from list.

composition(display_sub_ptf_name=True)[source]#

Composition of each portfolio in the population.

Parameters:
display_sub_ptf_namebool, default=True

If this is set to True, each sub-portfolio name composing a multi-period portfolio is displayed.

Returns:
dfDataFrame

Composition of the portfolios in the population.

contribution(measure, spacing=None, display_sub_ptf_name=True)[source]#

Contribution of each asset to a given measure of each portfolio in the population.

Parameters:
measureMeasure

The measure used for the contribution computation.

spacingfloat, optional

Spacing “h” of the finite difference: \(contribution(wi)= \frac{measure(wi-h) - measure(wi+h)}{2h}\).

display_sub_ptf_namebool, default=True

If this is set to True, each sub-portfolio name composing a multi-period portfolio is displayed.

Returns:
dfDataFrame

Contribution of each asset to a given measure of each portfolio in the population.

copy(/)#

Return a shallow copy of the list.

count(value, /)#

Return number of occurrences of value.

extend(other)[source]#

Extend population list by appending elements from the iterable.

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.

insert(index, item)[source]#

Insert portfolio before index.

max_measure(measure)[source]#

Returns the portfolio with the maximum measure.

Parameters:
measure: Measure

The portfolio measure.

Returns:
valuesBasePortfolio

The portfolio with maximum measure.

measures(measure)[source]#

Vector of portfolios measures for each portfolio from the population.

Parameters:
measureMeasure

The portfolio measure.

Returns:
valuesndarray

The vector of portfolios measures.

measures_mean(measure)[source]#

Mean of portfolios measures for each portfolio from the population.

Parameters:
measureMeasure

The portfolio measure.

Returns:
valuefloat

The mean of portfolios measures.

measures_std(measure)[source]#

Standard-deviation of portfolios measures for each portfolio from the population.

Parameters:
measureMeasure

The portfolio measure.

Returns:
valuefloat

The standard-deviation of portfolios measures.

min_measure(measure)[source]#

Returns the portfolio with the minimum measure.

Parameters:
measureMeasure

The portfolio measure.

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(display_sub_ptf_name=True)[source]#

Plot the compositions of the portfolios in the population.

Parameters:
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_contribution(measure, spacing=None, display_sub_ptf_name=True)[source]#

Plot the contribution of each asset to a given measure of the portfolios in the population.

Parameters:
measureMeasure

The measure used for the contribution computation.

spacingfloat, optional

Spacing “h” of the finite difference: \(contribution(wi)= \frac{measure(wi-h) - measure(wi+h)}{2h}\)

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)[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.

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, 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”.

Returns:
plotFigure

Returns the plotly Figure object.

plot_rolling_measure(measure=Sharpe Ratio, window=30)[source]#

Plot the measure over a rolling window for each portfolio in the population.

Parameters:
measurect.Measure, default = RatioMeasure.SHARPE_RATIO

The measure.

windowint, default=30

The window size.

Returns:
plotFigure

Returns the plot 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)[source]#

Returns the portfolio corresponding to the q quantile for a given portfolio measure.

Parameters:
measureMeasure

The portfolio measure.

qfloat

The quantile value.

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.

rolling_measure(measure=Sharpe Ratio, window=30)[source]#
Compute the measure over a rolling window for each portfolio in the

population.

Parameters:
measurect.Measure, default=RatioMeasure.SHARPE_RATIO

The measure. The default measure is the Sharpe Ratio.

windowint, default=30

The window size. The default value is 30 observations.

Returns:
dataframepandas DataFrame

The rolling measures.

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)[source]#

Sort the population by a given portfolio measure.

Parameters:
measureMeasure

The portfolio measure.

reversebool, default=False

If this is set to True, the order is reversed.

Returns:
valuesPopulations

The sorted population.

summary(formatted=True)[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.

Returns:
summarypandas DataFrame

The population’s portfolios summary