API Reference
Here you can find detailed documentation for the public Python API of the lqg package.
Core Systems & LQG Specification
lqg.system.System
An agent acting in an environment, both described as linear-Gaussian systems.
The environment (dynamics) evolves as
x[t+1] = A x[t] + B u[t] + V eps[t], eps[t] ~ N(0, I)
y[t+1] = F x[t+1] + W eta[t], eta[t] ~ N(0, I)
The agent (actor) has its own internal model of the environment, with
its own matrices A, B, F, V, W and a quadratic cost (Q, R). It
computes optimal control gains for that cost (LQR) and tracks its belief
xhat about the state with a Kalman filter. Its actions
u[t] = L[t] xhat[t] + k[t] then drive the true dynamics.
When the agent's model matches the environment, use LQG. Keeping the two separate allows models of agents with subjective or mismatched beliefs about the environment.
Shapes in this class use these names:
n: number of trialsT: number of time steps, so trajectories haveT + 1statesxdim: state dimensionality of the environmentbdim: state dimensionality of the agent's internal modelydim: observation dimensionalityudim: action dimensionality
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actor
|
LQGSpec
|
the agent's internal model and cost function, e.g. from Actor. |
required |
dynamics
|
LQGSpec
|
the true dynamics of the environment, e.g. from Dynamics. Its cost terms are ignored. |
required |
Source code in lqg/system.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |
simulate(rng_key, n=1, x0=None, xhat0=None, Sigma0=None, return_all=False)
Simulate n trials
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng_key
|
PRNGKey
|
random number generator key |
required |
n
|
int
|
number of trials |
1
|
x0
|
array
|
initial state of shape (xdim,). Defaults to zeros. |
None
|
xhat0
|
array
|
initial belief of shape (bdim,). Defaults to zeros. |
None
|
Sigma0
|
array
|
initial belief covariance of shape
(bdim, bdim), used to compute the Kalman gains. Defaults to
|
None
|
return_all
|
bool
|
return beliefs, observations and actions as well |
False
|
Returns:
| Type | Description |
|---|---|
array or tuple
|
If |
Source code in lqg/system.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
simulate_actions(x, action_dim_mask, rng_key, xhat0=None, Sigma0=None, return_all=False)
Simulate cursor actions given pre-existing experimental target trajectories.
Args:
The target dimensions of the state are taken from the data at each time
step, while the dimensions selected by action_dim_mask are simulated
from the agent's actions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
Experimental state data of shape (n, T+1, xdim) |
required |
action_dim_mask
|
array
|
Boolean mask of shape (xdim,) that selects the state dimensions to simulate (e.g. cursor position) |
required |
rng_key
|
PRNGKey
|
Random number generator key |
required |
xhat0
|
array
|
Initial belief of shape (bdim,). Defaults to zeros. |
None
|
Sigma0
|
array
|
initial belief covariance of shape
(bdim, bdim). Defaults to |
None
|
return_all
|
bool
|
Whether to return beliefs, observations and actions as well |
False
|
Returns:
| Type | Description |
|---|---|
array or tuple
|
If |
Source code in lqg/system.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
conditional_moments(x, x0=None, xhat0=None, Sigma0=None)
Moments of the joint state and belief, given past observed states.
For each time step t, computes the mean and covariance of the
Gaussian p(x[t+1], xhat[t+1] | x[0:t+1]) for a single trial, where
the agent's belief xhat is marginalized out over the past.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
one trial of observed states, of shape (T+1, d). If
|
required |
x0
|
array
|
initial state. Defaults to |
None
|
xhat0
|
array
|
initial belief. Defaults to |
None
|
Sigma0
|
array
|
initial belief covariance of shape
(bdim, bdim), used to compute the Kalman gains. Defaults to
|
None
|
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in lqg/system.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
conditional_distribution(x, x0=None, xhat0=None, Sigma0=None)
Distribution of each state given all previous states.
This is p(x[t+1] | x[0:t+1]) for every trial and time step, with the
agent's beliefs marginalized out. See
conditional_moments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
observed states of shape (n, T+1, d) |
required |
x0
|
array
|
initial states of shape (n, d). Defaults
to |
None
|
xhat0
|
array
|
initial beliefs of shape (n, bdim).
Defaults to |
None
|
Sigma0
|
array
|
initial belief covariance of shape (bdim, bdim) |
None
|
Returns:
| Type | Description |
|---|---|
Distribution
|
a multivariate normal with batch
shape (n,) and event shape (T, d), to be evaluated on
|
Source code in lqg/system.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
log_likelihood(x, x0=None, xhat0=None, Sigma0=None)
Log likelihood of observed trajectories.
Evaluates log p(x[1:] | x[0]), factorized over time steps as in
conditional_distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
observed states of shape (n, T+1, d) |
required |
x0
|
array
|
initial states of shape (n, d) |
None
|
xhat0
|
array
|
initial beliefs of shape (n, bdim) |
None
|
Sigma0
|
array
|
initial belief covariance of shape (bdim, bdim) |
None
|
Returns:
| Type | Description |
|---|---|
array
|
log likelihood of each trial, of shape (n,) |
Source code in lqg/system.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
belief_tracking_distribution(x, Sigma0=None)
Distribution of the agent's beliefs given the observed states.
This is p(xhat[t+1] | x[0:t+1]) for every trial and time step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
observed states of shape (n, T+1, xdim) |
required |
Sigma0
|
array
|
initial belief covariance of shape (bdim, bdim) |
None
|
Returns:
| Type | Description |
|---|---|
MultivariateNormal
|
a distribution with batch shape (n, T) and event shape (bdim,) |
Source code in lqg/system.py
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
to_numpyro(Sigma0=None, xdim=None)
Wrap the system as a numpyro distribution over trajectories.
The distribution can be used with numpyro.sample in a model, e.g.
numpyro.sample("x", system.to_numpyro(), obs=x).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Sigma0
|
array
|
initial belief covariance of shape (bdim, bdim) |
None
|
xdim
|
int
|
number of observed state dimensions. Defaults to all of them. |
None
|
Returns:
| Type | Description |
|---|---|
Distribution
|
a distribution with event shape (T+1, xdim) |
Source code in lqg/system.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
lqg.system.LQG
Bases: System
A system in which the agent's internal model matches the environment.
The same matrices are used for the actor and the dynamics. See Actor for their meaning and shapes.
Source code in lqg/system.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | |
__init__(A, B, F, V, W, Q, R, T=1000)
Create an LQG system from time-invariant matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
array
|
state transition matrix of shape (xdim, xdim) |
required |
B
|
array
|
control matrix of shape (xdim, udim) |
required |
F
|
array
|
observation matrix of shape (ydim, xdim) |
required |
V
|
array
|
process noise Cholesky factor of shape (xdim, xdim) |
required |
W
|
array
|
observation noise Cholesky factor of shape (ydim, ydim) |
required |
Q
|
array
|
state cost matrix of shape (xdim, xdim) |
required |
R
|
array
|
action cost matrix of shape (udim, udim) |
required |
T
|
int
|
number of time steps |
1000
|
Source code in lqg/system.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | |
lqg.system.Dynamics(A, B, F, V, W, T=1000)
Specification of an environment with time-invariant dynamics.
x[t+1] = A x[t] + B u[t] + V eps[t], eps[t] ~ N(0, I)
y[t+1] = F x[t+1] + W eta[t], eta[t] ~ N(0, I)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
array
|
state transition matrix of shape (xdim, xdim) |
required |
B
|
array
|
control matrix of shape (xdim, udim) |
required |
F
|
array
|
observation matrix of shape (ydim, xdim) |
required |
V
|
array
|
process noise Cholesky factor of shape (xdim, xdim) |
required |
W
|
array
|
observation noise Cholesky factor of shape (ydim, ydim) |
required |
T
|
int
|
number of time steps |
1000
|
Returns:
| Type | Description |
|---|---|
LQGSpec
|
the matrices repeated over |
Source code in lqg/system.py
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | |
lqg.system.Actor(A, B, F, V, W, Q, R, T=1000)
Specification of an agent's internal model and cost function.
The agent assumes the same linear-Gaussian dynamics as in
Dynamics and minimizes the cost
sum_t x[t]^T Q x[t] + u[t]^T R u[t] (with constant factors dropped).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
array
|
state transition matrix of shape (bdim, bdim) |
required |
B
|
array
|
control matrix of shape (bdim, udim) |
required |
F
|
array
|
observation matrix of shape (ydim, bdim) |
required |
V
|
array
|
process noise Cholesky factor of shape (bdim, bdim) |
required |
W
|
array
|
observation noise Cholesky factor of shape (ydim, ydim) |
required |
Q
|
array
|
state cost matrix of shape (bdim, bdim) |
required |
R
|
array
|
action cost matrix of shape (udim, udim) |
required |
T
|
int
|
number of time steps |
1000
|
Returns:
| Type | Description |
|---|---|
LQGSpec
|
the matrices repeated over |
Source code in lqg/system.py
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | |
lqg.spec.LQGSpec
Bases: NamedTuple
Time-varying specification of a (generalized) LQG problem.
Dynamics and observations:
x[t+1] = A[t] x[t] + B[t] u[t] + V[t] eps[t], eps[t] ~ N(0, I)
y[t+1] = F[t] x[t+1] + W[t] eta[t], eta[t] ~ N(0, I)
Cost, up to a constant:
sum_t ( 1/2 x[t]^T Q[t] x[t] + q[t]^T x[t]
+ u[t]^T P[t] x[t]
+ 1/2 u[t]^T R[t] u[t] + r[t]^T u[t] )
+ 1/2 x[T]^T Qf x[T] + qf^T x[T]
All time-varying fields have a leading time axis of length T. Use
Actor or Dynamics to build a
specification from time-invariant matrices.
Attributes:
| Name | Type | Description |
|---|---|---|
Q |
ndarray
|
quadratic state cost, shape (T, xdim, xdim) |
q |
ndarray
|
linear state cost, shape (T, xdim) |
Qf |
ndarray
|
quadratic final state cost, shape (xdim, xdim) |
qf |
ndarray
|
linear final state cost, shape (xdim,) |
P |
ndarray
|
action-state cross cost, shape (T, udim, xdim) |
R |
ndarray
|
quadratic action cost, shape (T, udim, udim) |
r |
ndarray
|
linear action cost, shape (T, udim) |
A |
ndarray
|
state transition matrices, shape (T, xdim, xdim) |
B |
ndarray
|
control matrices, shape (T, xdim, udim) |
V |
ndarray
|
process noise Cholesky factors, shape (T, xdim, xdim) |
F |
ndarray
|
observation matrices, shape (T, ydim, xdim) |
W |
ndarray
|
observation noise Cholesky factors, shape (T, ydim, ydim) |
Source code in lqg/spec.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
Tracking Models
lqg.tracking.basic.TrackingTask
Bases: System
Continuous tracking of a target with a cursor whose velocity is controlled.
For each spatial dimension, the state is [target, cursor]. The target
follows a Gaussian random walk, the action sets the cursor's velocity, and
both positions are observed with independent Gaussian noise. The cost is
the squared distance between target and cursor plus
action_cost * u^2. The agent's internal model matches the dynamics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
number of spatial dimensions (e.g. 2 for tracking in x and y) |
1
|
process_noise
|
float
|
standard deviation of the target's random walk |
1.0
|
action_variability
|
float
|
standard deviation of the noise on the cursor's movement |
0.5
|
sigma_target
|
float
|
standard deviation of the target observation noise |
6.0
|
sigma_cursor
|
float
|
standard deviation of the cursor observation noise |
6.0
|
action_cost
|
float
|
weight of the action cost relative to the distance cost |
1.0
|
dt
|
float
|
duration of a time step in seconds |
1.0 / 60.0
|
T
|
int
|
number of time steps |
1000
|
Source code in lqg/tracking/basic.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
lqg.tracking.basic.BoundedActor
Bases: TrackingTask
Boundedly rational actor in a tracking task, with an action cost.
The same model as TrackingTask; the name emphasizes that the action cost makes the actor deviate from optimal tracking. See there for the arguments.
Source code in lqg/tracking/basic.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
lqg.tracking.basic.OptimalActor
Bases: TrackingTask
Actor in a tracking task with a negligible action cost.
The same model as BoundedActor, but
with action_cost fixed to 1e-3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
number of spatial dimensions (e.g. 2 for tracking in x and y) |
1
|
process_noise
|
float
|
standard deviation of the target's random walk |
1.0
|
action_variability
|
float
|
standard deviation of the noise on the cursor's movement |
0.5
|
sigma_target
|
float
|
standard deviation of the target observation noise |
6.0
|
sigma_cursor
|
float
|
standard deviation of the cursor observation noise |
6.0
|
dt
|
float
|
duration of a time step in seconds |
1.0 / 60
|
T
|
int
|
number of time steps |
1000
|
Source code in lqg/tracking/basic.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
lqg.tracking.basic.RelativeObservationBoundedActor
Bases: System
Bounded actor that only observes the target position relative to the cursor.
The same model as BoundedActor, except
that the observation is target - cursor (one per spatial dimension)
instead of both positions separately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
number of spatial dimensions (e.g. 2 for tracking in x and y) |
1
|
process_noise
|
float
|
standard deviation of the target's random walk |
1.0
|
action_variability
|
float
|
standard deviation of the noise on the cursor's movement |
0.5
|
sigma
|
float
|
standard deviation of the observation noise |
6.0
|
action_cost
|
float
|
weight of the action cost relative to the distance cost |
1.0
|
dt
|
float
|
duration of a time step in seconds |
1.0 / 60.0
|
T
|
int
|
number of time steps |
1000
|
Source code in lqg/tracking/basic.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
lqg.tracking.subjective.SubjectiveActor
Bases: System
Bounded actor with a subjective model of the target dynamics.
The true dynamics are the same as in BoundedActor. The actor, however, assumes that the target has an additional latent velocity, i.e. it believes in the model
target[t+1] = target[t] + dt * target_vel[t] + subj_noise * eps
target_vel[t+1] = target_vel[t] + subj_vel_noise * eps
The actor's state is [target, cursor] for each spatial dimension,
followed by target_vel for each spatial dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
number of spatial dimensions (e.g. 2 for tracking in x and y) |
1
|
process_noise
|
float
|
standard deviation of the target's random walk |
1.0
|
action_variability
|
float
|
standard deviation of the noise on the cursor's movement |
0.5
|
action_cost
|
float
|
weight of the action cost relative to the distance cost |
1.0
|
subj_noise
|
float
|
standard deviation of the target noise that the actor assumes |
1.0
|
subj_vel_noise
|
float
|
standard deviation of the target velocity noise that the actor assumes |
0.5
|
sigma_target
|
float
|
standard deviation of the target observation noise |
6.0
|
sigma_cursor
|
float
|
standard deviation of the cursor observation noise |
6.0
|
dt
|
float
|
duration of a time step in seconds |
1.0 / 60
|
T
|
int
|
number of time steps |
1000
|
Source code in lqg/tracking/subjective.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
lqg.tracking.point_mass.PointMassBoundedActor
Bases: System
Bounded actor in a tracking task with point-mass hand dynamics.
Unlike in BoundedActor, the action does
not set the cursor velocity directly. It drives a first-order muscle
activation with time constant tau, whose force accelerates the cursor
as a point mass. The state is [target, cursor_pos, cursor_vel, force],
and target and cursor position are observed with Gaussian noise. Only one
spatial dimension is supported.
The cost is the squared distance between target and cursor, plus
velocity_cost * cursor_vel^2, force_cost * force^2 and
action_cost * u^2. The agent's internal model matches the dynamics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process_noise
|
float
|
standard deviation of the target's random walk |
1.0
|
action_variability
|
float
|
standard deviation of the noise on the muscle activation |
0.001
|
sigma_target
|
float
|
standard deviation of the target observation noise |
6.0
|
sigma_cursor
|
float
|
standard deviation of the cursor observation noise |
12.5
|
action_cost
|
float
|
weight of the action cost |
0.01
|
velocity_cost
|
float
|
weight of the cost on cursor velocity |
0.01
|
force_cost
|
float
|
weight of the cost on muscle force |
0.01
|
dt
|
float
|
duration of a time step in seconds |
1.0 / 60.0
|
T
|
int
|
number of time steps |
1000
|
tau
|
float
|
time constant of the muscle activation in seconds |
0.066
|
Source code in lqg/tracking/point_mass.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
lqg.tracking.point_mass.PointMassSubjectiveActor
Bases: System
Point-mass hand dynamics with a subjective model of the target dynamics.
The true dynamics are the same as in PointMassBoundedActor (random-walk target, point-mass cursor with first-order muscle activation). The actor, however, assumes that the target has an additional latent velocity, i.e. it believes in the model
target_{t+1} = target_t + dt * target_vel_t + subj_noise * eps
target_vel_{t+1} = target_vel_t + subj_vel_noise * eps
Actor state: [target, cursor_pos, cursor_vel, force, target_vel]. The first four dimensions coincide with the state of the true dynamics.
With subj_noise == process_noise and subj_vel_noise == 0, the model reduces to PointMassBoundedActor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process_noise
|
float
|
standard deviation of the target's random walk |
1.0
|
action_variability
|
float
|
standard deviation of the noise on the muscle activation |
0.001
|
subj_noise
|
float
|
standard deviation of the target noise that the actor assumes |
1.0
|
subj_vel_noise
|
float
|
standard deviation of the target velocity noise that the actor assumes |
0.5
|
sigma_target
|
float
|
standard deviation of the target observation noise |
6.0
|
sigma_cursor
|
float
|
standard deviation of the cursor observation noise |
12.5
|
action_cost
|
float
|
weight of the action cost |
0.01
|
velocity_cost
|
float
|
weight of the cost on cursor velocity |
0.01
|
force_cost
|
float
|
weight of the cost on muscle force |
0.01
|
dt
|
float
|
duration of a time step in seconds |
1.0 / 60.0
|
T
|
int
|
number of time steps |
1000
|
tau
|
float
|
time constant of the muscle activation in seconds |
0.066
|
Source code in lqg/tracking/point_mass.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
Control & Estimation
lqg.control.lqr
Gains
Bases: NamedTuple
LQR control gains, giving the optimal action u[t] = L[t] x[t] + k[t].
Attributes:
| Name | Type | Description |
|---|---|---|
L |
ndarray
|
feedback gains of shape (T, udim, xdim) |
k |
ndarray
|
feedforward terms of shape (T, udim) |
H |
ndarray
|
regularized Hessians of the cost-to-go with respect to the action, of shape (T, udim, udim) |
Source code in lqg/control/lqr.py
8 9 10 11 12 13 14 15 16 17 18 19 20 | |
backward(spec, eps=1e-08)
Compute optimal control gains with a backward pass (Riccati recursion).
Solves the linear-quadratic regulator for the dynamics and cost in
spec, ignoring noise. By the separation principle, these gains are
also optimal for the noisy, partially observed problem when applied to
the Kalman filter estimate of the state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
LQGSpec
|
problem specification |
required |
eps
|
float
|
minimum eigenvalue of |
1e-08
|
Returns:
| Type | Description |
|---|---|
Gains
|
the control gains for each time step |
Source code in lqg/control/lqr.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
lqg.belief.kf
forward(spec, Sigma0)
Compute Kalman gains with a forward pass.
The belief is then updated as
x_pred = A[t] xhat[t] + B[t] u[t]
xhat[t+1] = x_pred + K[t] (y[t+1] - F[t] x_pred)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
LQGSpec
|
problem specification, of which |
required |
Sigma0
|
ndarray
|
initial covariance of the state estimate, of shape (xdim, xdim) |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Kalman gains of shape (T, xdim, ydim) |
Source code in lqg/belief/kf.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
Inference & Models
lqg.infer.utils.infer(x, num_samples, num_warmup, model=BoundedActor, numpyro_fn=lqg_model, process_noise=1.0, dt=1.0 / 60, method='nuts', progress_bar=True, num_chains=1, seed=0, **fixed)
Bayesian inference of model parameters with MCMC.
Samples from the posterior over the parameters of model given observed
trajectories, using NUTS. With method="neutra", a normalizing flow is
first fit to the posterior with SVI (10,000 steps) and used to
reparametrize the model, which can help with difficult posterior geometry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
observed trajectories of shape (n, T+1, d) |
required |
num_samples
|
int
|
number of posterior samples per chain |
required |
num_warmup
|
int
|
number of warmup steps per chain |
required |
model
|
type
|
model class, e.g. BoundedActor |
BoundedActor
|
numpyro_fn
|
callable
|
numpyro model function defining the priors and
the likelihood. Defaults to the model in |
lifted_model
|
process_noise
|
float
|
target random walk standard deviation, which is not estimated |
1.0
|
dt
|
float
|
duration of a time step in seconds |
1.0 / 60
|
method
|
str
|
|
'nuts'
|
progress_bar
|
bool
|
whether to show a progress bar |
True
|
num_chains
|
int
|
number of MCMC chains |
1
|
seed
|
int
|
random seed |
0
|
**fixed
|
parameters that are fixed to the given values (not estimated) |
{}
|
Returns:
| Type | Description |
|---|---|
MCMC
|
the MCMC object after running, e.g. use
|
Source code in lqg/infer/utils.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
lqg.infer.mle
max_likelihood(key, x, model=BoundedActor, numpyro_fn=lqg_model, max_steps=2000, constraints=None, num_restarts=5, **fixed)
Maximum likelihood estimation of the parameters of an LQG model.
The optimization is performed in an unconstrained space and mapped to the
parameter space via the bijectors in constraints. It is repeated from
num_restarts random initializations, and the solution with the lowest
negative log likelihood is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
jax.random.PRNGKey for sampling initial parameters |
required | |
x
|
observed data of shape (n_trials, T, d) |
required | |
model
|
model class (e.g. BoundedActor) |
BoundedActor
|
|
numpyro_fn
|
numpyro model function defining the likelihood |
lqg_model
|
|
max_steps
|
maximum number of optimization steps per restart |
2000
|
|
constraints
|
dict mapping parameter names to bijectors (e.g. Box, LogBox).
Parameters not in this dict use the bounds in |
None
|
|
num_restarts
|
number of random restarts of the optimization |
5
|
|
**fixed
|
parameters that are fixed to the given values (not estimated) |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
estimated parameters |
Source code in lqg/infer/mle.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
parameter_recovery(key, model, num_datasets=100, T=500, num_trials=20, obs_dim=2, constraints=None, max_steps=1000, num_restarts=10, progress=True, **fixed)
Parameter recovery analysis for a model class.
For each of num_datasets repetitions, true parameters are sampled within the
bounds of constraints (see sample_within_bounds), data are simulated from
the model, and the parameters are estimated with max_likelihood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
jax.random.PRNGKey |
required | |
model
|
model class (e.g. BoundedActor) |
required | |
num_datasets
|
number of simulated data sets |
100
|
|
T
|
number of time steps per trial |
500
|
|
num_trials
|
number of trials per data set |
20
|
|
obs_dim
|
number of state dimensions used for fitting (as in experimental data, where only target and cursor position are observed) |
2
|
|
constraints
|
dict mapping parameter names to bijectors (e.g. Box, LogBox).
Parameters not in this dict use the bounds in |
None
|
|
max_steps
|
maximum number of optimization steps per restart |
1000
|
|
num_restarts
|
number of random restarts of the optimization |
10
|
|
progress
|
show a progress bar |
True
|
|
**fixed
|
parameters that are fixed to the given values (not estimated) |
{}
|
Returns:
| Type | Description |
|---|---|
|
dict with entries "true" and "estimate": dicts mapping parameter names to arrays of shape (num_datasets,), "ll_true" and "ll_estimate": log likelihood of the data at the true and estimated parameters. An estimate that is far from the true value but has a higher likelihood points to a non-identifiable parameter, a lower likelihood to a failed optimization. |
Source code in lqg/infer/mle.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
plot_parameter_recovery(results, constraints=None)
Scatter plots of true vs. estimated parameters from parameter_recovery.
Parameters with a LogBox constraint are shown on a log scale.
Source code in lqg/infer/mle.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
lqg.infer.models
shared_params_lqg_model(x, model_type, process_noise=1.0, dt=1.0 / 60.0, priors=None, shared_params=None, dim=1, **fixed_params)
numpyro model for LQG models with shared parameter
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
data |
required | |
process_noise
|
target random walk standard deviation |
1.0
|
|
dt
|
time step duration |
1.0 / 60.0
|
|
priors
|
dict of prio distributions |
None
|
|
shared_params
|
list of parameter names that are shared across conditions |
None
|
|
dim
|
dimensionality of the state |
1
|
|
**fixed_params
|
any parameter that should be fixed (i.e. not estimated from the data) |
{}
|
Source code in lqg/infer/models.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
lqg.infer.prior
Utilities & I/O
lqg.io
load_tracking_data(delay=12, clip=120, subtract_mean=True, data_path='data/')
Load tracking data from Bonnen et al. (2015)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delay
|
temporal delay between target and response |
12
|
|
clip
|
clip the first n time steps |
120
|
|
subtract_mean
|
subtract the mean of both trajectories? |
True
|
Returns:
| Type | Description |
|---|---|
array
|
data from tracking task |
Source code in lqg/io.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
loadmat(filename)
this function should be called instead of direct spio.loadmat as it cures the problem of not properly recovering python dictionaries from mat files. It calls the function check keys to cure all entries which are still mat-objects
Source code in lqg/io.py
10 11 12 13 14 15 16 17 18 | |
lqg.optim
A collection of helper functions for optimization with JAX.
minimize(fun, x0, method=None, args=(), bounds=None, constraints=(), tol=None, callback=None, options=None)
A simple wrapper for scipy.optimize.minimize using JAX.
The objective is differentiated automatically with JAX, and parameters can
be arbitrary PyTrees. The remaining keyword arguments are passed through to
scipy.optimize.minimize;
see its documentation for details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
callable
|
The objective function to be minimized, written in JAX
code so that it is automatically differentiable. Its signature is
|
required |
x0
|
Initial guess represented as a JAX PyTree. |
required | |
method
|
str or callable
|
Type of solver, e.g. |
None
|
args
|
tuple
|
Extra arguments passed to the objective function and its derivative. Must consist of valid JAX types; e.g. the leaves of the PyTree must be floats. |
()
|
bounds
|
sequence or Bounds
|
Bounds on variables for
L-BFGS-B, TNC, SLSQP, Powell, and trust-constr methods. Bounds must
be flattened manually in the same order as the inputs |
None
|
constraints
|
Constraint, dict or list thereof
|
Constraints
definition (only for COBYLA, SLSQP and trust-constr). Constraints
must be flattened manually in the same order as the inputs |
()
|
tol
|
float
|
Tolerance for termination. |
None
|
callback
|
callable
|
Called after each iteration as
|
None
|
options
|
dict
|
A dictionary of solver options, e.g.
|
None
|
Returns:
| Type | Description |
|---|---|
OptimizeResult
|
The optimization result. Its |
Source code in lqg/optim.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |