Power

Price Forward Curves

Shifting

class rivapy.marketdata_tools.PFCShifter(shape: DataFrame, contracts: List[EnergyFutureSpecifications])[source]

Bases: FactoryObject

A shifting methodology for PFC shapes. This class gets a PFC shape as an input and shifts it in such a way, that the resulting PFC contains the future prices defined in the contracts dictionary. We follow the methodology described here: https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2706366

Parameters:
  • shape (pd.DataFrame) – PFC shape, where the DataFrame index are datetime objects.

  • contracts (Dict[str, EnergyFutureSpecifications]) – Dictionary containing the future contracts (EnergyFutureSpecifications objects)

Usage:

# iterative usage
pfc_shifter = PFCShifter(shape=shape, contracts=contracts)
transition_matrix = pfc_shifter.generate_transition_matrix()
transition_matrix = pfc_shifter.detect_redundant_contracts(transition_matrix)
transition_matrix = pfc_shifter.generate_synthetic_contracts(transition_matrix)
pfc = pfc_shifter.shift(transition_matrix)

# direct call
pfc_shifter = PFCShifter(shape=shape, contracts=contracts)
pfc = pfc_shifter.compute()
compute() DataFrame[source]

Compute method to directly call all the individual steps involved for the shifting

Returns:

Shifted PFC shape

Return type:

pd.DataFrame

detect_redundant_contracts(transition_matrix: DataFrame) DataFrame[source]

In order to obtain an invertable matrix, the matrix must be of full rank. Linear dependent contracts will yield linear dependent row vectors. This is the case if e.g. a Cal Base and all four quarter contracts are provided. This method finds all redundant (linear dependent) contracts and omits the last found linear dependent contract in order to make sure that the row vectors are linearly independent.

Parameters:

transition_matrix (pd.DataFrame) – Transition matrix generated by the generate_transition_matrix method.

Returns:

Transition matrix without linearly dependent row vectors.

Return type:

pd.DataFrame

generate_synthetic_contracts(transition_matrix: DataFrame) DataFrame[source]

In order to fulfill the requirement of an invertable transition matrix, not only the row vectors but also the column vectors must generate a basis. In cases where m > n, we need to additionally generate synthetic contracts. The delivery period for the synthetic contracts are chosen in such a way that the column vectors become linearly independent. The forward price for each synthetic contract is computed based on the rations of the average shape values over the corresponding delivery period of the synthetic contract and a reference contract. The shape ratio is multiplied with the forward price of the reference contract in order to obtain a forward price for the synthetic contract. The reference contract is implemented to be always the first contract in the contracts dictionary.

Parameters:

transition_matrix (pd.DataFrame) – Transition matrix generated by the detect_redundant_contracts method.

Returns:

Full rank transition matrix

Return type:

pd.DataFrame

generate_transition_matrix() DataFrame[source]

The transition matrix is the basis of the shifting algorithm. This method generates a (n x m) matrix with zero and one entries, where n is the number of contracts and m are start and end dates for the delivery periods. Hence, the matrix row vectors indicate the delivery periods of each contract. Note that the latest delivery end date is not displayed in the transition matrix.

Returns:

Transition matrix containing zeros and ones indicating delivery periods of individual contracts.

Return type:

pd.DataFrame

shift(transition_matrix: DataFrame) DataFrame[source]

This method is the final step in the shifting algorithm. The transition matrix is inversed and multiplied with the forward price vector to obtain a non overlapping forward price vector.

\[f^{no} = T^{-1}\cdot f\]

where:

\(f^{no}\) is the Non-overlapping forward price vector

\(T\) is the Transition matrix

\(f\) is the Forward price vector

Afterwards the PFC \(S(t)\) is obtained from the shape \(s(t)\) by the follwoing formular:

\[S(t) = s(t)\cdot \frac{\sum_{u=T_s}^{T_e} f^{no}(u)}{\sum_{u=T_s}^{T_e} s(u)}\]

with \(T_s\) and \(T_e\) being the start and end dates of the individual delivery periods.

Parameters:

transition_matrix (pd.DataFrame) – Full rank transition matrix generated by the generate_synthetic_contracts method

Returns:

Shifted shape.

Return type:

pd.DataFrame

Shaping

class rivapy.marketdata_tools.PFCShaper(spot_prices: DataFrame, holiday_calendar: HolidayBase, normalization_config: Dict[Literal['D', 'W', 'ME'], int | None] | None = None)[source]

Bases: FactoryObject

PFCShaper interface. Each shaping model for energy price forward curves must inherit from this base class.

Parameters:
  • spot_prices (pd.DataFrame) – Data used to calibrate the shaping model.

  • holiday_calendar (holidays.HolidayBase) – Calendar object to obtain country specific holidays.

  • normalization_config (Optional[Dict[Literal["D", "W", "ME"], Optional[int]]], optional) – A dictionary configurating the shape normalization periods. Here D defines the number of days at the beginning of the shape over which the individual mean is normalized to one. W defines the number of weeks at the beginning of the shape over which the individual mean is normalized to one. ME defines the number of months at the beginning of the shape over which the individual mean is normalized to one. The remaining shape is then normalized over the individual years.Defaults to None.

abstract apply(apply_schedule: SimpleSchedule)[source]

Applies the model on a schedule in order to generate a shape for future dates.

Parameters:

apply_schedule (SimpleSchedule) – Schedule object in order to generate a shape for future dates.

abstract calibrate() ndarray[source]

Calibration of the shaping model

Returns:

Numpy array containing the fit.

Return type:

np.ndarray

normalize_shape(shape: DataFrame) DataFrame[source]

Normalizes the shape based on normalization_config.

D defines the number of days at the beginning of the shape over which the individual mean is normalized to one.

W defines the number of weeks at the beginning of the shape over which the individual mean is normalized to one.

ME defines the number of months at the beginning of the shape over which the individual mean is normalized to one. The remaining shape is then normalized over the individual years.

Example: D is 2, W is 2 and ME is 1. The shape starts at 03.03.2025 (monday). Since D is 2, the shape is normalized for 03.03.2025 and 04.03.2025 individually.

The weeks are normalized from 05.03.2025 to 09.03.2025 and from 10.03.2025 to 16.03.2025.

The month is then normalized from 17.03.2025 to 31.03.2025. The remaining shape (starting from 01.04.2025) is normalized on a yearly level.

Parameters:

shape (pd.DataFrame) – Shape which should be normalized

Returns:

Normalized shape

Return type:

pd.DataFrame

class rivapy.marketdata_tools.CategoricalRegression(spot_prices: DataFrame, holiday_calendar: HolidayBase, normalization_config: Dict[Literal['D', 'W', 'M'], int | None] | None = None)[source]

Bases: PFCShaper

Linear regression model using categorical predictor variables to construct a PFC shape.

\[S(t) = S_0 + \sum^{23}_{i=1}\beta^h_i\cdot\mathbb{I}_{h(t)=i} + \beta^d\cdot\mathbb{I}_{d(t)=1} + \beta^H\cdot\mathbb{I}_{H(t)=1} + \sum^{12}_{i=2}\beta^m_i\cdot\mathbb{I}_{m(t)=i}\]

where:

\(S_0\): Spot price level

\(\mathbb{I}_x = \begin{cases} 1, & \text{if the } x \text{ expression renders true} \\ 0, & \text{if the } x \text{ expression renders false} \end{cases}\)

\(h(t)\): Hour of t

\(d(t) = \begin{cases} 1, & \text{if t is a weekday} \\ 0, & \text{if t is a day on a weekend} \end{cases}\)

\(H(t) = \begin{cases} 1, & \text{if t public holidy} \\ 0, & \text{if t is not a public holiday} \end{cases}\)

\(m(t)\): Month of t

Parameters:
  • spot_prices (pd.DataFrame) – Data used to calibrate the shaping model.

  • holiday_calendar (holidays.HolidayBase) – Calendar object to obtain country specific holidays.

  • normalization_config (Optional[Dict[Literal["D", "W", "ME"], Optional[int]]], optional) – A dictionary configurating the shape normalization periods. Here D defines the number of days at the beginning of the shape over which the individual mean is normalized to one. W defines the number of weeks at the beginning of the shape over which the individual mean is normalized to one. ME defines the number of months at the beginning of the shape over which the individual mean is normalized to one. The remaining shape is then normalized over the individual years.Defaults to None.

apply(apply_schedule: SimpleSchedule) DataFrame[source]

Applies the model on a schedule in order to generate a shape for future dates.

Parameters:

apply_schedule (SimpleSchedule) – Schedule object in order to generate a shape for future dates.

calibrate() ndarray[source]

Calibration of the shaping model

Returns:

Numpy array containing the fit.

Return type:

np.ndarray