Single-Segment (CostSegment) Models: Trajectories and Windows [ex103.0]#

Defines a cost segment which consists of an ALSSM and a left-sided, exponentially decaying window.

See also: Cost Function Classes, CostSegment, Segment

costs.trajectories(xs), lm.map_trajectories( costs.trajectories(xs) )

Out:

-- Print --
CostSegment(label: costs segment for polynomial model)
  └- AlssmPoly(A=[[1,1,1,1],[0,1,2,3],[0,0,1,3],[0,0,0,1]], C=[1,0,0,0], label=alssm-polynomial),
  └- Segment(a=-10, b=5, direction=fw, g=8, delta=0, label=left-decaying)
[-1, 2, 0, 0]
[range(-10, 6), array([-21., -19., -17., -15., -13., -11.,  -9.,  -7.,  -5.,  -3.,  -1.,
         1.,   3.,   5.,   7.,   9.])]
[range(-10, 6), array([39. , 29.6, 21.4, 14.4,  8.6,  4. ,  0.6, -1.6, -2.6, -2.4, -1. ,
        1.6,  5.4, 10.4, 16.6, 24. ])]
[range(-10, 6), array([129.  ,  98.72,  73.56,  53.04,  36.68,  24.  ,  14.52,   7.76,
         3.24,   0.48,  -1.  ,  -1.68,  -2.04,  -2.56,  -3.72,  -6.  ])]
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.

import matplotlib.pyplot as plt
import numpy as np
import lmlib as lm


# Defining an second order polynomial ALSSM
alssm_poly = lm.AlssmPoly(poly_degree=3, label="alssm-polynomial")

# Defining a segment with a left-sided, exponentially decaying window
a = -10  # left boundary
b = 5  # right boundary
g = 8  # effective weighted number of sample under the window (controlling the window size)
left_seg = lm.Segment(a, b, lm.FORWARD, g, label="left-decaying")

# creating the cost segment, combining window (segment) and model (ALSSM).
costs = lm.CostSegment(alssm_poly, left_seg, label="costs segment for polynomial model")

# print internal structure
print('-- Print --')
print(costs)


# get trajectory from initial state
xs = [[-1, 2,  0, 0],  # polynomial coefficients of trajectories
      [-1, 2, .6, 0],
      [-1, -1, .4, -.08]]

# --------- Upper Plots ---------
# get window weights
windows = costs.windows()
js, window = windows[0]

trajectories = costs.trajectories(xs)
print(xs[0])

# plot
fig, axs = plt.subplots(5, sharex='all', gridspec_kw={'hspace': 0.1}, figsize=(6,6))
axs[0].plot(js, window, '-', c='k', lw=1.5, label='winodw weights $w_j = \gamma^j$')
axs[0].set_title('costs.trajectories(xs)')
axs[0].axvline(0, color="black", linestyle="--", lw=1.0)
axs[0].axhline(1, color="black", linestyle="--", lw=0.5)
axs[0].axvline(a, color="gray", linestyle="-", lw=0.5)
axs[0].axvline(b, color="gray", linestyle="-", lw=0.5)
axs[0].set(ylim=[0, 2.1])


for n in range(len(trajectories)):
    print(trajectories[n][0])
    js, trajectory = trajectories[n][0]
    axs[1].plot(js, trajectory, lw=1.5, label='trajectory $s_j(x_'+str(n)+') = cA^jx_'+str(n)+'$')
axs[1].set_xlabel('Evaluation Index $j$')
axs[1].axvline(0, color="black", linestyle="--", lw=0.5)
axs[1].axvline(a, color="gray", linestyle="-", lw=0.5)
axs[1].axvline(b, color="gray", linestyle="-", lw=0.5)
axs[1].set_ylim([-40, 80])

axs[1].tick_params(axis='both', which='both',labelbottom=True)

axs[2].set_visible(False) # add spacer

# --------- Lower Plots ---------
# Get localized trajectories
K = 70 # total signal length
K_refs = [20, 40, 60] # trajectory locations (indices)
COLS_W = ['black','gray','lightgray']

wins = lm.map_windows(costs.windows(), K_refs, K, merge_seg=True)
axs[3].set_title('lm.map_trajectories( costs.trajectories(xs) )')
for n, win in enumerate(wins):
    axs[3].plot(win, '-', color=COLS_W[n], lw=1.5, label=r"winodw weights $w_{k-K_{ref}}$")
trajectory = lm.map_trajectories(costs.trajectories(xs), K_refs, K)

for n in range(len(trajectories)):
    axs[4].plot(trajectory[n][0], lw=2, label='y_hat')

for k_ref in K_refs:
    axs[4].axvline(k_ref+a, color="gray", linestyle="-", lw=0.5)
    axs[4].axvline(k_ref+b, color="gray", linestyle="-", lw=0.5)
    axs[4].axvline(k_ref, color="black", linestyle="--", lw=0.5)

axs[4].set_xlabel('Evaluation Index $k$')
axs[4].set_ylim([-40, 80])

for ax in axs:
    ax.legend(fontsize=7)
plt.show()

Total running time of the script: ( 0 minutes 0.133 seconds)

Gallery generated by Sphinx-Gallery