Portfolio
amltk.metalearning.portfolio
#
Portfolio selection.
portfolio_selection
#
portfolio_selection(
items: dict[K, Series] | DataFrame,
k: int,
*,
row_reducer: Callable[[Series], float] = np.max,
aggregator: Callable[[Series], float] = np.mean,
portfolio_value: (
Callable[[DataFrame], float] | None
) = None,
maximize: bool = True,
scaler: (
TransformerMixin | Literal["minmax"] | None
) = "minmax",
with_replacement: bool = False,
stop_if_worse: bool = False,
seed: Seed | None = None
) -> tuple[DataFrame, Series]
Selects a portfolio of k
items from items
.
A portfolio is a subset of the items, and is selected by maximizing the
portfolio_value
function in a greedy selection approach.
At each iteration 0 <= i < k
, the portfolio_value
function is calculated
for the portfolio obtained by adding the i
th item to the portfolio. The item
that maximizes the portfolio_value
function is then added to the portfolio for
the next iteration.
The portfolio_function
can often be define by a row wise reduction
(row_reducer=
) followed by some aggregation over these reductions (aggregator=
).
You can also supply your own value function if desired (portfolio_value=
).
A Single Iteration
This uses the row_reducer=np.max
and aggregator=np.mean
to calculate the
value of a portfolio.
In this case, we have 4 datasets and our current portfolio
consists of config_1
and config_2
. We are going to calculate the value of
adding config_try
to the current best portfolio.
| config_1 | config_2 | config_try
dataset_1 | 1 | 0 | 0
dataset_2 | 0 | 0.5 | 1
dataset_3 | 0 | 0.5 | 0.5
dataset_4 | 1 | 1 | 0
Apply row_reducer
to each row, in this case np.max
Apply aggregator
to the reduced rows, in this case np.mean
PARAMETER | DESCRIPTION |
---|---|
items |
A dictionary of items to select from. |
k |
The number of items to select.
TYPE:
|
row_reducer |
A function to aggregate the rows of the portfolio. This is applied to a potential portfolio, for example to calculate the max score of all configs, for a given dataset (row). |
aggregator |
A function to take all the single values reduced by |
portfolio_value |
A custom function to calculate the value of a portfolio.
This will take precedence over |
maximize |
Whether to maximize or minimize the portfolio value.
TYPE:
|
scaler |
A scaler to use to scale the portfolio values. Is applied across the rows.
TYPE:
|
with_replacement |
Whether to select items with replacement.
TYPE:
|
stop_if_worse |
Whether to stop if the portfolio value is worse than the current best.
TYPE:
|
seed |
The seed to use for breaking ties.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[DataFrame, Series]
|
The final portfolio The trajectory, where the entry is the value once added to the portfolio. |
Source code in src/amltk/metalearning/portfolio.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
|