Arithmetics
The following arithmetic operations are supported:
Symbol |
Name |
---|---|
|
Addition |
|
Subtraction |
|
Multiplication |
|
Division |
|
Exponentiation |
These operations are used as ordinary arithmetical operators:
signal_1 + signal_2
Note on missing values: When performing arithmetic operations between signals, if any of the signals is missing data for certain dates, the result will not include data points on those dates.
Alternative approach: To handle missing data more gracefully, you can use the concat function followed by aggregation instead of direct arithmetic operations:
# Instead of:
signal1 + signal2
# Use:
concat([signal1, signal2]).sum()
# Instead of:
signal1 - signal2
# Use:
concat([signal1, -signal2]).sum()
This approach concatenates the signals and then applies an aggregation function (like sum()) that can handle missing values by skipping them rather than producing missing values.
- weighted_sum(signal_1, signal_2, ..., signal_n, weights=[w_1, w_2,...w_3], nan_when_missing=False, normalize=True)
This function provides a method for handling weighted sums with possible missing values. When the signals have numerical values for a given time the value of weighted_sum is
w_1*signal_1 + w_2*signal_2 + … + w_n*signal_n.
When some signals are not numbers, the weighted sum is taken over only the signals with numerical values.
- Parameters:
signal_j – For j=1, 2, …, n, the signals which are combined.
weights – When a list of n numerical weights is supplied this is the weights in the sum. When no weights are supplied it is assumed that w_j=1.
nan_when_missing – If one of the signals have a missing value (NaN) the value of the sum is set to missing (NaN) if nan_when_missing=True, otherwise the missing values are skipped in the sum.
normalize – If normalize=True the weights w_j are normalized so that the active weights sum to 1.
Example:
Combining card transaction data from multiple countries as a weighted average:
weighted_sum(UK_Spend, Germany_Spend, France_Spend, Italy_Spend, Spain_Spend, weights=[0.35, 0.35, 0.15, 0.1, 0.05])
- signal.log()
Calculate the natural logarithm of the signal.
- signal.exp()
Apply the exponential function to the signal.
For each value in the original signal, the result is calculated as the base of the natural logarithm, e ≈ 2.71, raised to the power of the original value.
- signal.abs()
Calculate the absolute values of the signal.
- signal.sign()
Calculate the sign of the signal values.
For each value in the original signal, the sign is +1 if the original value is positive, -1 if the original value is negative, and 0 if the original value is 0.