pysatl_tsp.implementations.processor.pwma_handler

Module Contents

Classes

PWMAHandler

Pascal Weighted Moving Average (PWMA) handler.

API

class pysatl_tsp.implementations.processor.pwma_handler.PWMAHandler(length: int = 10, asc: bool = True, source: pysatl_tsp.core.Handler[Any, float | None] | None = None)[source]

Bases: pysatl_tsp.core.processor.inductive.weighted_moving_average_handler.WeightedMovingAverageHandler

Pascal Weighted Moving Average (PWMA) handler.

Calculates a weighted moving average using coefficients from Pascal’s triangle as weights. Pascal’s triangle provides a natural weighting scheme where the central values receive more weight than the extremes, creating a balanced but still centered weighting distribution.

This implementation matches the functionality of pandas_ta.pwma.

Inherits parameters from WeightedMovingAverageHandler: :param length: The period for the calculation, defaults to 10 :param asc: Whether weights should be applied in ascending order, defaults to False :param source: Input data source, defaults to None

Example:
# Create a data source with numeric values
data_source = SimpleDataProvider([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])

# Create a PWMA handler with length of 4
pwma_handler = PWMAHandler(length=4)
pwma_handler.set_source(data_source)

# Process the data
for value in pwma_handler:
    print(value)

# First 3 values will be None (not enough data points)
# For length=4, Pascal weights would be [1/8, 3/8, 3/8, 1/8] or [1/8, 3/8, 3/8, 1/8] when asc=False
# The calculation gives more weight to the central values

Initialization

Initialize weighted moving average handler with specified parameters.

_calculate_weights(length: int, asc: bool) list[float][source]

Calculate Pascal’s triangle weights for PWMA.

Generates weights based on a row of Pascal’s triangle and normalizes them to sum to 1.0. The row is determined by (length-1) to match the pandas_ta implementation. The weights order can be reversed based on the asc parameter.

Parameters:
  • length – The number of weights to generate

  • asc – Whether weights should be in ascending order

Returns:

A list of normalized weights summing to 1.0

_combination(n: int, r: int) float[source]

Calculate the binomial coefficient (n choose r).

Computes the binomial coefficient, also known as “n choose r”, which represents the number of ways to choose r items from a set of n items without regard to order.

Uses math.comb when available (Python 3.8+) for efficiency, otherwise falls back to calculation using factorials.

Parameters:
  • n – The total number of items

  • r – The number of items to choose

Returns:

The binomial coefficient

_factorial(n: int) int[source]

Calculate the factorial of n.

Computes n! recursively. This method is used as a fallback when math.comb is not available for binomial coefficient calculation.

Parameters:

n – The number to calculate factorial for

Returns:

The factorial of n