# lmlib.statespace.cost.CompositeCost#

class lmlib.statespace.cost.CompositeCost(alssms, segments, F, **kwargs)#

Quadratic cost function defined by a conjunction one or multiple of Alssm and Segment

A composite costs combines multiple ALSSM models and multiple Segments in form of a grid, where each row is a model and each column a Segment. The segments define the temporal relations of the models. Mapping matrix F enables or disables each ALSSM/Segment pair in each grid node; multiple active ALSSMs in one column are superimposed.

A CompositeCost is an implementation of [Wildhaber2019] PDF (Composite Cost)

====================
Class: CompositeCost (= M ALSSM's + P Segment's)
====================

M : number of ALSSM models
^
|                Segment[0]   Segment[1]    ...   Segment[P-1]
|              +------------+------------+--//--+--------------+
|  alssm[0]    |  F[0,0]    |            |      | F[0,P-1]     |
|              +------------+------------+--//--+--------------+
|    .         |     .      |     ...    |      |    .         |
|    .         |     .      |     ...    |      |    .         |
|              +------------+------------+--//--+--------------+
|  alssm[M-1]  |  F[M-1,0]  |            |      | F[M-1, P-1]  |
|              +------------+------------+--//--+--------------+

|------------|-------|----|------|--------------|--> k (time)
0
(0: common relative reference
index for all segments)

P : number of segments

F[m,p] in R+, scalar on alssm's output. (i.e., 0 for inactive grid node,
1 or any other scalar factor for active grid node).


This figure shows a graphical representation of a composite cost, depicting the internal relationships between Segments, ALSSMs, and the mapping matrix F. F[m,p] is implemented as a scalar factor multiplied on the alssm’s output signal.

For more details, seen Chapter 9 in [Wildhaber2019]. The cost function of a composite cost is defined as

$J(k, x, \Theta) = \sum_{p = 1}^{P}\beta_p J_{a_p}^{b_p}(k, x, \theta_p) \ ,$

where, $$\Theta = (\theta_1, \theta_2,\dots, \theta_P)$$ and the segment scalars $$\beta_p \in \mathbb{R}_+$$.

Parameters
• alssms (tuple, shape=(M,)) – Set of ALSSMs

• segments (tuple, shape=(P,)) – Set of Segments

• F (array_like of shape=(M, P)) – Mapping matrix $$F$$, maps models to segment

• **kwargs – Forwarded to CostBase

M : number of ALSSMs
P : number of segments

Examples

>>> alssm_spike = lm.AlssmPoly(poly_degree=3, label='spike')
>>> alssm_baseline = lm.AlssmPoly(poly_degree=2, label='baseline')
>>>
>>> segment_left = lm.Segment(a=-50, b=-1, direction=lm.FORWARD, g=20, label="finite left")
>>> segment_middle = lm.Segment(a=0, b=10, direction=lm.FORWARD, g=100, label="finite middle")
>>> segment_right = lm.Segment(a=10, b=50, direction=lm.FORWARD, g=20, delta=10, label="finite right")
>>>
>>> F = [[0, 1, 0], [1, 1, 1]]
>>> cost = lm.CompositeCost((alssm_spike, alssm_baseline), (segment_left, segment_middle, segment_right), F, label='spike_baseline')
>>> print(cost)
CostSegment : label: spike_baseline
└- ['Alssm : polynomial, A: (4, 4), C: (1, 4), label: spike', 'Alssm : polynomial, A: (3, 3), C: (1, 3), label: baseline'],
└- [Segment : a: -50, b: -1, direction: fw, g: 20, delta: 0, label: finite left , Segment : a: 0, b: 10, direction: fw, g: 100, delta: 0, label: finite middle , Segment : a: 10, b: 50, direction: fw, g: 20, delta: 10, label: finite right ]


Methods

 __init__(alssms, segments, F, **kwargs) eval_alssm_output(xs, alssm_weights) Evaluation of the ALSSM for multiple state vectors xs. int : Order of the (stacked) Alssm Model Returns the state indices for a specified label of the stacked internal ALSSM get_steady_state_W([method]) Returns Steady State Matrix W trajectories(xs[, F, thd]) Returns the CompositeCost's ALSSM's trajectories (=output sequences for a fixed initial states) for multiple initial state vectors xs windows(segment_indices[, thd]) Returns for each selected segment its window generated by CostSegment.windows().

Attributes

 F Mapping matrix $$F$$, maps models to segments alssms Set of ALSSM label Label of the Cost Model segments Set of Segment
property F#

Mapping matrix $$F$$, maps models to segments

Type

ndarray

property alssms#

Set of ALSSM

Type

tuple

eval_alssm_output(xs, alssm_weights)#

Evaluation of the ALSSM for multiple state vectors xs.

See: eval()

Parameters
• xs (array_like of shape=(XS, N [,S]) of floats) – List of state vectors $$x$$

• alssm_selection (array_like, shape=(M,) of bool) – Each element enables (True, 1) or disables (False, 0) the m-th ALSSM in CompositeCost.alssms.

Returns

s – ALSSM outputs

Return type

ndarray of shape=(XS,[J,]L[,S]) of floats

N : ALSSM system order, corresponding to the number of state variables
L : output order / number of signal channels
M : number of ALSSMs
S : number of signal sets
XS : number of state vectors in a list

Examples

get_model_order()#

int : Order of the (stacked) Alssm Model

get_state_var_indices(label)#

Returns the state indices for a specified label of the stacked internal ALSSM

Parameters

label (str) – state label

Returns

out – state indices of the label

Return type

list of int

Parameters

method (str, optional) – if ‘closed_form’ is used the steady state matrix will be calculated in a close form. This method can be critical, as it can produce badly conditioned matrices internally. if ‘limited_sum’ is used, the steady state matrix will be calculated brute force, with a stop condition on a minimum change.

Returns

Wss = class – Steady State Matrix W

Return type

numpy.ndarray

property label#

Label of the Cost Model

Type

str, None

property segments#

Set of Segment

Type

tuple

trajectories(xs, F=None, thd=1e-06)#

Returns the CompositeCost’s ALSSM’s trajectories (=output sequences for a fixed initial states) for multiple initial state vectors xs

Evaluates the CompositeCost’s ALSSM with the state vectors xs over the time indices defined by CompositeCost.segments. The segment’s interval boundaries limit the evaluation interval (samples outside this interval are set to 0). In particular for segments with infinite interval borders, the threshold option thds additionally limits the evaluation boundaries by defining a minimal window height of the CompositeCost.segments.

Parameters
• xs (array_like of shape=(XS, N [,S]) of floats) – List of initial state vectors $$x$$

• F (array_like, shape(M, P) of int, shape(M,) of int) – Mapping matrix. If not set to None, the given matrix overloads the CompositeCost’s internal mapping matrix as provided by the class constructor for CompositeCost. If F is only of shape(M,), the vector gets enlarged to size shape(M, P) by repeating the vector P-times. (This is a shortcut to select a single ALSSM over all segments.)

• thds (list of shape(P) of floats, scalar, None, optional) – Per segment threshold limiting the evaluation boundaries by setting a minimum window height of the associated segments Scalar to use the same threshold for all available segments.

Returns

trajectories – Each element in trajectories is a tuple with

• range of length JR: relative index range of trajectory with respect to semgent’s boundaries

• array of shape(JR, L, [S]): trajectory values over reported range.

Dimension S is only present in the output, if dimension S is also present in xs (i.e., if multiple signal sets are used)

Return type

list of shape=(XS) of tuples of shape=(P) of tuples (range, array)

JR : index range length
XS : number of state vectors in a list
N : ALSSM system order, corresponding to the number of state variables
S : number of signal sets
M : number of ALSSMs
P : number of segments

windows(segment_indices, thd=1e-06)#

Returns for each selected segment its window generated by CostSegment.windows().

The segments are selected by segment_selection.

Parameters
• segment_indices (array_like, shape=(P,) of Boolean) – Enables (True, 1) or disables (False, 0) the evaluation of the p-th Segment in CompositeCost.segments using CostSegment.window()

• thds (list of shape=(P,) of floats, scalar, optional) – List of per-segment threshold values or scalar to use the same threshold for all segments Evaluation is stopped for window weights below the given threshold. Set list element to None to disable the threshold for a segment.

Returns

Each element is a tuple with

• range of length JR: relative index range of window with respect to segment’s boundaries.

• array of shape=(JR) of floats: per-index window weight over the reported index range

Return type

list of shape=(P) of tuples (range, array)

JR : index range length
P : number of segments