cogant.simulate
The cogant.simulate package runs forward simulations of the compiled state-space model: sample trajectories, compute variational free energy, and visualize outcomes.
Package
cogant.simulate
COGANT Simulate: Model validation and state space simulation with Active Inference.
CategoricalDistribution
Categorical probability distribution over discrete states.
Source code in py/cogant/simulate/distributions.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 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 | |
__init__(categories, probabilities=None)
Initialize a categorical distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
categories
|
list[str]
|
List of category names. |
required |
probabilities
|
list[float] | None
|
Optional list of probabilities (must sum to 1). If None, uses uniform distribution. |
None
|
Source code in py/cogant/simulate/distributions.py
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 | |
__repr__()
String representation.
Source code in py/cogant/simulate/distributions.py
140 141 142 143 | |
entropy()
Compute Shannon entropy of the distribution.
Returns:
| Type | Description |
|---|---|
float
|
Entropy in nats. |
Source code in py/cogant/simulate/distributions.py
74 75 76 77 78 79 80 81 82 83 84 | |
kl_divergence(other)
Compute KL divergence to another distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
CategoricalDistribution
|
Another CategoricalDistribution with same categories. |
required |
Returns:
| Type | Description |
|---|---|
float
|
KL(self || other) in nats. |
Source code in py/cogant/simulate/distributions.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
log_prob(category)
Get log probability of a category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
str
|
Category name. |
required |
Returns:
| Type | Description |
|---|---|
float
|
log(P(category)). Returns -inf if category has 0 probability. |
Source code in py/cogant/simulate/distributions.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
sample()
Sample a category from the distribution.
Returns:
| Type | Description |
|---|---|
str
|
A sampled category name. |
Source code in py/cogant/simulate/distributions.py
50 51 52 53 54 55 56 | |
update(observation, likelihood)
Bayesian update: posterior ∝ likelihood × prior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observation
|
str
|
The observed category. |
required |
likelihood
|
CategoricalDistribution
|
P(observation | category) for each category. |
required |
Returns:
| Type | Description |
|---|---|
CategoricalDistribution
|
Updated posterior distribution. |
Source code in py/cogant/simulate/distributions.py
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 | |
FreeEnergyCalculator
Calculate from compiled matrices, preserving the state's declared axis order.
Source code in py/cogant/simulate/free_energy.py
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 | |
accuracy(observation, beliefs, likelihood_model=None)
Return expected log likelihood for the observed outcome.
Source code in py/cogant/simulate/free_energy.py
131 132 133 134 135 136 137 138 139 140 | |
complexity(posterior, prior=None)
Return KL(q || prior), using the model D when no prior is supplied.
Source code in py/cogant/simulate/free_energy.py
122 123 124 125 126 127 128 129 | |
expected_free_energy(beliefs, policy, horizon=3, likelihood_model=None)
Sum the specified candidate policy up to a positive finite horizon.
Source code in py/cogant/simulate/free_energy.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
policy_ranking(beliefs, available_actions, horizon=3, likelihood_model=None)
Rank candidate one-action policies by risk plus ambiguity, stably.
Source code in py/cogant/simulate/free_energy.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
surprisal(observation, beliefs, likelihood_model=None)
Return negative log marginal evidence of the observation.
Source code in py/cogant/simulate/free_energy.py
109 110 111 112 113 114 115 116 117 118 119 120 | |
variational_free_energy(beliefs, observation, likelihood_model=None)
Evaluate posterior free energy against the model's initial prior D.
Source code in py/cogant/simulate/free_energy.py
79 80 81 82 83 84 85 86 87 88 89 90 | |
ModelRunner
Bind structural axis names to complete A/B/C/D categorical matrices.
Source code in py/cogant/simulate/runner.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 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 | |
validate_model(state_space)
Check that model is well-formed.
Validates: - All transitions reference valid states and actions - All actions reference valid variables - State space is connected - No dangling references
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_space
|
StateSpaceModel
|
StateSpaceModel to validate. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with "valid" (bool), "errors" (list), "warnings" (list). |
Source code in py/cogant/simulate/runner.py
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 | |
SimulationVisualizer
Generate visualizations of simulation traces.
Source code in py/cogant/simulate/visualization.py
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 | |
__init__()
Initialize the visualizer.
Source code in py/cogant/simulate/visualization.py
16 17 18 | |
generate_html_report(trace, state_space)
Generate full HTML report of simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
list[dict[str, Any]]
|
List of simulation steps. |
required |
state_space
|
Any
|
The state space model (for metadata). |
required |
Returns:
| Type | Description |
|---|---|
str
|
HTML string. |
Source code in py/cogant/simulate/visualization.py
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 | |
generate_mermaid_trajectory(trace)
Generate Mermaid sequence diagram of state transitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
list[dict[str, Any]]
|
List of simulation steps. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Mermaid diagram string. |
Source code in py/cogant/simulate/visualization.py
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 | |
plot_action_distribution(trace)
Generate SVG bar chart of actions taken.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
list[dict[str, Any]]
|
List of simulation steps, each with 'action' key. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SVG string. |
Source code in py/cogant/simulate/visualization.py
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 | |
plot_belief_evolution(trace)
Generate SVG showing belief evolution over time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
list[dict[str, Any]]
|
List of simulation steps, each with 'beliefs' key (dict of state -> prob). |
required |
Returns:
| Type | Description |
|---|---|
str
|
SVG string. |
Source code in py/cogant/simulate/visualization.py
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 | |
plot_free_energy_trajectory(trace)
Generate SVG plot of free energy over time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
list[dict[str, Any]]
|
List of simulation steps, each with 'free_energy' key. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SVG string. |
Source code in py/cogant/simulate/visualization.py
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 | |
TransitionMatrix
Transition probabilities between states under actions.
Source code in py/cogant/simulate/distributions.py
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 | |
__init__(states, actions)
Initialize a transition matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
list[str]
|
List of state names. |
required |
actions
|
list[str]
|
List of action names. |
required |
Source code in py/cogant/simulate/distributions.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
__repr__()
String representation.
Source code in py/cogant/simulate/distributions.py
278 279 280 | |
from_state_space(state_space)
classmethod
Build a transition matrix from a StateSpaceModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_space
|
StateSpaceModel
|
The compiled state space model. |
required |
Returns:
| Type | Description |
|---|---|
TransitionMatrix
|
TransitionMatrix with inferred probabilities from transitions. |
Source code in py/cogant/simulate/distributions.py
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 | |
get_next_state_dist(state, action)
Get the distribution over next states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
Current state. |
required |
action
|
str
|
Action taken. |
required |
Returns:
| Type | Description |
|---|---|
CategoricalDistribution
|
CategoricalDistribution over next states. |
Source code in py/cogant/simulate/distributions.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
set_transition(state, action, next_state, prob)
Set a single transition probability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
Current state. |
required |
action
|
str
|
Action taken. |
required |
next_state
|
str
|
Resulting state. |
required |
prob
|
float
|
Probability of transition. |
required |
Source code in py/cogant/simulate/distributions.py
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 | |
Runner
cogant.simulate.runner
Simulation of explicitly supplied categorical generative matrices.
ModelRunner
Bind structural axis names to complete A/B/C/D categorical matrices.
Source code in py/cogant/simulate/runner.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 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 | |
validate_model(state_space)
Check that model is well-formed.
Validates: - All transitions reference valid states and actions - All actions reference valid variables - State space is connected - No dangling references
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_space
|
StateSpaceModel
|
StateSpaceModel to validate. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with "valid" (bool), "errors" (list), "warnings" (list). |
Source code in py/cogant/simulate/runner.py
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 | |
Distributions
cogant.simulate.distributions
Probability distributions for Active Inference simulations.
Provides categorical distributions, transition matrices, and belief updates.
CategoricalDistribution
Categorical probability distribution over discrete states.
Source code in py/cogant/simulate/distributions.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 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 | |
__init__(categories, probabilities=None)
Initialize a categorical distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
categories
|
list[str]
|
List of category names. |
required |
probabilities
|
list[float] | None
|
Optional list of probabilities (must sum to 1). If None, uses uniform distribution. |
None
|
Source code in py/cogant/simulate/distributions.py
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 | |
__repr__()
String representation.
Source code in py/cogant/simulate/distributions.py
140 141 142 143 | |
entropy()
Compute Shannon entropy of the distribution.
Returns:
| Type | Description |
|---|---|
float
|
Entropy in nats. |
Source code in py/cogant/simulate/distributions.py
74 75 76 77 78 79 80 81 82 83 84 | |
kl_divergence(other)
Compute KL divergence to another distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
CategoricalDistribution
|
Another CategoricalDistribution with same categories. |
required |
Returns:
| Type | Description |
|---|---|
float
|
KL(self || other) in nats. |
Source code in py/cogant/simulate/distributions.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
log_prob(category)
Get log probability of a category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
str
|
Category name. |
required |
Returns:
| Type | Description |
|---|---|
float
|
log(P(category)). Returns -inf if category has 0 probability. |
Source code in py/cogant/simulate/distributions.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
sample()
Sample a category from the distribution.
Returns:
| Type | Description |
|---|---|
str
|
A sampled category name. |
Source code in py/cogant/simulate/distributions.py
50 51 52 53 54 55 56 | |
update(observation, likelihood)
Bayesian update: posterior ∝ likelihood × prior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observation
|
str
|
The observed category. |
required |
likelihood
|
CategoricalDistribution
|
P(observation | category) for each category. |
required |
Returns:
| Type | Description |
|---|---|
CategoricalDistribution
|
Updated posterior distribution. |
Source code in py/cogant/simulate/distributions.py
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 | |
TransitionMatrix
Transition probabilities between states under actions.
Source code in py/cogant/simulate/distributions.py
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 | |
__init__(states, actions)
Initialize a transition matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
list[str]
|
List of state names. |
required |
actions
|
list[str]
|
List of action names. |
required |
Source code in py/cogant/simulate/distributions.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
__repr__()
String representation.
Source code in py/cogant/simulate/distributions.py
278 279 280 | |
from_state_space(state_space)
classmethod
Build a transition matrix from a StateSpaceModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_space
|
StateSpaceModel
|
The compiled state space model. |
required |
Returns:
| Type | Description |
|---|---|
TransitionMatrix
|
TransitionMatrix with inferred probabilities from transitions. |
Source code in py/cogant/simulate/distributions.py
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 | |
get_next_state_dist(state, action)
Get the distribution over next states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
Current state. |
required |
action
|
str
|
Action taken. |
required |
Returns:
| Type | Description |
|---|---|
CategoricalDistribution
|
CategoricalDistribution over next states. |
Source code in py/cogant/simulate/distributions.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
set_transition(state, action, next_state, prob)
Set a single transition probability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
Current state. |
required |
action
|
str
|
Action taken. |
required |
next_state
|
str
|
Resulting state. |
required |
prob
|
float
|
Probability of transition. |
required |
Source code in py/cogant/simulate/distributions.py
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 | |
Free energy
cogant.simulate.free_energy
Categorical free-energy API backed by the shared dependency-free kernel.
FreeEnergyCalculator
Calculate from compiled matrices, preserving the state's declared axis order.
Source code in py/cogant/simulate/free_energy.py
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 | |
accuracy(observation, beliefs, likelihood_model=None)
Return expected log likelihood for the observed outcome.
Source code in py/cogant/simulate/free_energy.py
131 132 133 134 135 136 137 138 139 140 | |
complexity(posterior, prior=None)
Return KL(q || prior), using the model D when no prior is supplied.
Source code in py/cogant/simulate/free_energy.py
122 123 124 125 126 127 128 129 | |
expected_free_energy(beliefs, policy, horizon=3, likelihood_model=None)
Sum the specified candidate policy up to a positive finite horizon.
Source code in py/cogant/simulate/free_energy.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
policy_ranking(beliefs, available_actions, horizon=3, likelihood_model=None)
Rank candidate one-action policies by risk plus ambiguity, stably.
Source code in py/cogant/simulate/free_energy.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
surprisal(observation, beliefs, likelihood_model=None)
Return negative log marginal evidence of the observation.
Source code in py/cogant/simulate/free_energy.py
109 110 111 112 113 114 115 116 117 118 119 120 | |
variational_free_energy(beliefs, observation, likelihood_model=None)
Evaluate posterior free energy against the model's initial prior D.
Source code in py/cogant/simulate/free_energy.py
79 80 81 82 83 84 85 86 87 88 89 90 | |
bayesian_belief_update(prior, likelihood_matrix, observation_index)
Condition on an observed index; reject observations with zero evidence.
Source code in py/cogant/simulate/free_energy.py
25 26 27 28 29 30 31 | |
expected_free_energy(policy_action_sequence, beliefs, likelihood_matrix, transition_tensor, log_preferences)
Sum risk KL(predicted outcomes || softmax(C)) and expected ambiguity.
Source code in py/cogant/simulate/free_energy.py
34 35 36 37 38 39 40 41 42 43 | |
uniform_distribution(n)
Construct an explicitly requested uniform categorical distribution.
Source code in py/cogant/simulate/free_energy.py
46 47 48 49 50 | |
variational_free_energy(beliefs, prior, likelihood_matrix, observation=None)
Evaluate F against an explicit predicted prior and observation distribution.
Source code in py/cogant/simulate/free_energy.py
13 14 15 16 17 18 19 20 21 22 | |
Visualization
cogant.simulate.visualization
Visualization for Active Inference simulations.
Generates SVG plots and HTML reports of simulation traces.
SimulationVisualizer
Generate visualizations of simulation traces.
Source code in py/cogant/simulate/visualization.py
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 | |
__init__()
Initialize the visualizer.
Source code in py/cogant/simulate/visualization.py
16 17 18 | |
generate_html_report(trace, state_space)
Generate full HTML report of simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
list[dict[str, Any]]
|
List of simulation steps. |
required |
state_space
|
Any
|
The state space model (for metadata). |
required |
Returns:
| Type | Description |
|---|---|
str
|
HTML string. |
Source code in py/cogant/simulate/visualization.py
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 | |
generate_mermaid_trajectory(trace)
Generate Mermaid sequence diagram of state transitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
list[dict[str, Any]]
|
List of simulation steps. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Mermaid diagram string. |
Source code in py/cogant/simulate/visualization.py
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 | |
plot_action_distribution(trace)
Generate SVG bar chart of actions taken.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
list[dict[str, Any]]
|
List of simulation steps, each with 'action' key. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SVG string. |
Source code in py/cogant/simulate/visualization.py
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 | |
plot_belief_evolution(trace)
Generate SVG showing belief evolution over time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
list[dict[str, Any]]
|
List of simulation steps, each with 'beliefs' key (dict of state -> prob). |
required |
Returns:
| Type | Description |
|---|---|
str
|
SVG string. |
Source code in py/cogant/simulate/visualization.py
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 | |
plot_free_energy_trajectory(trace)
Generate SVG plot of free energy over time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
list[dict[str, Any]]
|
List of simulation steps, each with 'free_energy' key. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SVG string. |
Source code in py/cogant/simulate/visualization.py
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 | |