API Reference
margarine.base.baseflow
Base density estimator for margarine package.
Defines a base class for density estimators with common interface methods including:
- train
- sample
- __call__
- log_prob
- log_like
- save
- load
BaseDensityEstimator
Bases: ABC
Base class for density estimators in the margarine package.
Source code in margarine/base/baseflow.py
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 | |
__call__(u)
Evaluate the density estimator at given points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
ndarray
|
Samples from the unit hypercube. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: samples from the density estimator. |
Source code in margarine/base/baseflow.py
44 45 46 47 48 49 50 51 52 53 | |
load(filepath)
classmethod
Load a density estimator from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the file from which to load the estimator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
BaseDensityEstimator |
BaseDensityEstimator
|
Loaded density estimator instance. |
Source code in margarine/base/baseflow.py
94 95 96 97 98 99 100 101 102 103 104 | |
log_like(x, logevidence, prior_density)
abstractmethod
Compute the log-likelihood of given samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Samples for which to compute the log-likelihood. |
required |
logevidence
|
float
|
Log-evidence value. |
required |
prior_density
|
ndarray
|
Prior density estimator or densities. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Log-likelihoods of the samples. |
Source code in margarine/base/baseflow.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
log_prob(x)
abstractmethod
Compute the log-probability of given samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Samples for which to compute the log-probability. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Log-probabilities of the samples. |
Source code in margarine/base/baseflow.py
55 56 57 58 59 60 61 62 63 64 65 | |
sample(key, num_samples)
abstractmethod
Generate samples from the density estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
ndarray
|
JAX random key for sampling. |
required |
num_samples
|
int
|
Number of samples to generate. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Generated samples as a JAX array. |
Source code in margarine/base/baseflow.py
30 31 32 33 34 35 36 37 38 39 40 41 42 | |
save(filepath)
Save the density estimator to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the file where the estimator will be saved. |
required |
Source code in margarine/base/baseflow.py
86 87 88 89 90 91 92 | |
train()
abstractmethod
Train the density estimator on the provided data.
Source code in margarine/base/baseflow.py
25 26 27 28 | |
margarine.estimators.kde
KDE implementation using JAX.
KDE
Bases: BaseDensityEstimator
Kernel Density Estimator (KDE) using JAX.
Source code in margarine/estimators/kde.py
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 | |
__call__(u)
Transform samples from the unit hypercube to samples on the KDE.
Uses the Rosenblatt transformation (conditional inverse transform sampling) to map uniform samples to a Gaussian mixture KDE distribution.
In detail, for a d-dimensional KDE, each output dimension is computed sequentially:
x_1 = F_{X_1}^{-1}(u_1)
x_2 = F_{X_2 | X_1}^{-1}(u_2 | x_1)
...
x_d = F_{X_d | X_{1:d-1}}^{-1}(u_d | x_1, ..., x_{d-1})
where (u_i \sim \text{Uniform}[0,1]), (F^{-1}) denotes the inverse CDF, and the conditional CDFs are computed from the Gaussian mixture KDE. Since these inverse CDFs do not have a closed form, we solve for each x_i using a root-finding algorithm (e.g., Newton-Raphson).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
ndarray
|
Samples from the unit hypercube [0,1]^d Shape: (n_samples, n_dims) |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: The transformed samples following the KDE distribution. Shape: (n_samples, n_dims) |
Source code in margarine/estimators/kde.py
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 | |
__init__(theta, weights=None, theta_ranges=None, bandwidth='silverman')
Initialize the KDE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
ndarray
|
Parameters of the density estimator. |
required |
weights
|
ndarray | None
|
Optional weights for the parameters. |
None
|
theta_ranges
|
ndarray | None
|
Optional ranges for the parameters. |
None
|
bandwidth
|
float | str
|
Bandwidth for the KDE. |
'silverman'
|
Source code in margarine/estimators/kde.py
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 | |
load(filename)
classmethod
Load a KDE from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the file from which to load the KDE. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
KDE |
BaseDensityEstimator | None
|
Loaded KDE instance. |
Source code in margarine/estimators/kde.py
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 | |
log_like(x, logevidence, prior_density)
Compute the marginal log-likelihood of given samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Samples for which to compute the log-likelihood. |
required |
logevidence
|
float
|
Log-evidence term. |
required |
prior_density
|
ndarray | BaseDensityEstimator
|
Prior density or density estimator. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Log-likelihoods of the samples. |
Source code in margarine/estimators/kde.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
log_prob(x)
Compute the log-probability of given samples.
While the density estimator has its own built in log probability function, a correction has to be applied for the transformation of variables that is used to improve accuracy when learning. The correction is implemented here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Samples for which to compute the log-probability. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Log-probabilities of the samples. |
Source code in margarine/estimators/kde.py
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 | |
sample(key, num_samples)
Sample from the KDE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
ndarray
|
JAX random key for sampling. |
required |
num_samples
|
int
|
Number of samples to draw. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Samples drawn from the KDE. |
Source code in margarine/estimators/kde.py
67 68 69 70 71 72 73 74 75 76 77 78 79 | |
save(filename)
Save the KDE to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the file where the KDE will be saved. |
required |
Source code in margarine/estimators/kde.py
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 | |
train()
Generates a weighted KDE.
Source code in margarine/estimators/kde.py
56 57 58 59 60 61 62 63 64 65 | |
margarine.estimators.nice
Implementation of the NICE estimator.
NICE
Bases: BaseDensityEstimator, Module
Implementation of the NICE architecture for density estimation.
Details in https://arxiv.org/abs/1410.8516.
Source code in margarine/estimators/nice.py
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 | |
__call__(u)
Transform samples from the unit hypercube.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
ndarray
|
Samples from the unit hypercube. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log probabilities of the input data. |
Source code in margarine/estimators/nice.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
__init__(theta, weights=None, theta_ranges=None, in_size=2, hidden_size=128, num_layers=2, num_coupling_layers=4, nnx_rngs=None)
Initialize the NICE estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
ndarray
|
Parameters of the density estimator. |
required |
weights
|
ndarray | None
|
Optional weights for the parameters. |
None
|
theta_ranges
|
ndarray | None
|
Optional ranges for the parameters. |
None
|
in_size
|
int
|
Input size. |
2
|
hidden_size
|
int
|
Sizes of hidden layers. |
128
|
num_layers
|
int
|
Number of layers in each coupling network. |
2
|
num_coupling_layers
|
int
|
Number of coupling layers. |
4
|
nnx_rngs
|
dict | None
|
Optional RNGs for Flax. |
None
|
Source code in margarine/estimators/nice.py
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 | |
forward(x)
NICE forward pass.
This is the forward pass of the NICE coupling layer from samples in the target space to the base distribution space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input samples. |
required |
Source code in margarine/estimators/nice.py
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 | |
inverse(x)
NICE inverse pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input samples. |
required |
Source code in margarine/estimators/nice.py
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 | |
load(filename)
classmethod
Load a trained NICE model from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the file from which the model will be loaded. |
required |
Returns:
| Type | Description |
|---|---|
BaseDensityEstimator | None
|
Loaded NICE model. |
Source code in margarine/estimators/nice.py
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 | |
log_like(x, logevidence, prior_density)
Compute the marginal log-likelihood of given samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Samples for which to compute the log-likelihood. |
required |
logevidence
|
float
|
Log-evidence term. |
required |
prior_density
|
ndarray | BaseDensityEstimator
|
Prior density or density estimator. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log likelihoods of the input data. |
Source code in margarine/estimators/nice.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |
log_prob(x)
Compute the log probability of the input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input data. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log probabilities of the input data. |
Source code in margarine/estimators/nice.py
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 | |
log_prob_under_NICE(x)
Compute the log probability under the NICE model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input data. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log probabilities of the input data. |
Source code in margarine/estimators/nice.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
sample(key, num_samples)
Sample from the trained NICE model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
ndarray
|
JAX random key. |
required |
num_samples
|
int
|
Number of samples to draw. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Samples drawn from the NICE model. |
Source code in margarine/estimators/nice.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
save(filename)
Save the trained NICE model to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the file where the model will be saved. |
required |
Source code in margarine/estimators/nice.py
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 | |
train(key, learning_rate=0.0001, epochs=1000, patience=50, batch_size=256)
Train the NICE model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
ndarray
|
JAX random key for data splitting. |
required |
learning_rate
|
float
|
Learning rate for the optimizer. |
0.0001
|
epochs
|
int
|
Number of training epochs. |
1000
|
patience
|
int
|
Patience for early stopping. |
50
|
batch_size
|
int
|
Batch size for training. |
256
|
Source code in margarine/estimators/nice.py
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 | |
margarine.estimators.realnvp
Implementation of the RealNVP estimator.
RealNVP
Bases: BaseDensityEstimator, Module
Implementation of the RealNVP architecture for density estimation.
Details in https://arxiv.org/abs/1605.08803.
Source code in margarine/estimators/realnvp.py
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 548 549 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 582 583 584 585 586 | |
__call__(u)
Transform samples from the unit hypercube.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
ndarray
|
Samples from the unit hypercube. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log probabilities of the input data. |
Source code in margarine/estimators/realnvp.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
__init__(theta, weights=None, theta_ranges=None, in_size=2, hidden_size=128, num_layers=2, num_coupling_layers=4, nnx_rngs=None, permutations_key=jax.random.PRNGKey(0))
Initialize the RealNVP estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
ndarray
|
Parameters of the density estimator. |
required |
weights
|
ndarray | None
|
Optional weights for the parameters. |
None
|
theta_ranges
|
ndarray | None
|
Optional ranges for the parameters. |
None
|
in_size
|
int
|
Input size. |
2
|
hidden_size
|
int
|
Size of hidden layers. |
128
|
num_layers
|
int
|
Number of layers in each coupling network. |
2
|
num_coupling_layers
|
int
|
Number of coupling layers. |
4
|
nnx_rngs
|
dict | None
|
Optional RNGs for Flax. |
None
|
permutations_key
|
ndarray
|
JAX random key for permutations. |
PRNGKey(0)
|
Source code in margarine/estimators/realnvp.py
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 | |
forward(x, return_log_det=False)
Forward pass of the RealNVP coupling layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input data. |
required |
return_log_det
|
bool
|
Whether to return the log-determinant of the Jacobian. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray | tuple[ndarray, ndarray]
|
jnp.ndarray | tuple[jnp.ndarray, jnp.ndarray] : Transformed data, and optionally the log-determinant of the Jacobian. |
Source code in margarine/estimators/realnvp.py
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 | |
inverse(x)
Inverse pass of the RealNVP coupling layer.
From the base distribution to the target distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Samples from the base distribution. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Transformed samples in the target distribution. |
Source code in margarine/estimators/realnvp.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
load(filename)
classmethod
Load a trained RealNVP model from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the file from which the model will be loaded. |
required |
Returns:
| Type | Description |
|---|---|
BaseDensityEstimator | None
|
Loaded RealNVP model. |
Source code in margarine/estimators/realnvp.py
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 548 549 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 582 583 584 585 586 | |
log_like(x, logevidence, prior_density)
Compute the marginal log-likelihood of given samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Samples for which to compute the log-likelihood. |
required |
logevidence
|
float
|
Log-evidence term. |
required |
prior_density
|
ndarray | BaseDensityEstimator
|
Prior density or density estimator. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log likelihoods of the input data. |
Source code in margarine/estimators/realnvp.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | |
log_prob(x)
Compute the log probability of the input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input data. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log probabilities of the input data. |
Source code in margarine/estimators/realnvp.py
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 | |
log_prob_under_RealNVP(x)
Compute the log probability under the RealNVP model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input data. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log probabilities of the input data. |
Source code in margarine/estimators/realnvp.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
sample(key, num_samples)
Sample from the trained RealNVP model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
ndarray
|
JAX random key. |
required |
num_samples
|
int
|
Number of samples to draw. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Samples drawn from the RealNVP model. |
Source code in margarine/estimators/realnvp.py
368 369 370 371 372 373 374 375 376 377 378 379 380 | |
save(filename)
Save the trained RealNVP model to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the file where the model will be saved. |
required |
Source code in margarine/estimators/realnvp.py
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 | |
train(key, learning_rate=0.0001, epochs=1000, patience=50, batch_size=256)
Train the RealNVP model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
ndarray
|
JAX random key for data splitting. |
required |
learning_rate
|
float
|
Learning rate for the optimizer. |
0.0001
|
epochs
|
int
|
Number of training epochs. |
1000
|
patience
|
int
|
Patience for early stopping. |
50
|
batch_size
|
int
|
Size of training batches. |
256
|
Source code in margarine/estimators/realnvp.py
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 | |
margarine.estimators.clustered
Module for clustered mixture of density estimators.
cluster
Create clustered mixture of MAFs to model multi-modal distributions.
Source code in margarine/estimators/clustered.py
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 | |
__call__(key, u)
Transform samples from the unit hypercube to the cluster.
This function is used when calling the cluster class to transform samples from the unit hypercube to samples on the clustered distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
ndarray
|
JAX random key for sampling. |
required |
u
|
ndarray
|
Samples on the unit hypercube. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Samples transformed to the clusterMAF. |
Source code in margarine/estimators/clustered.py
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 | |
__init__(theta, base_estimator, weights=None, theta_ranges=None, clusters=None, max_cluster_number=10, **kwargs)
Piecewise normalizing flow built from masked autoregressive flows.
This class is a wrapper around the MAF class with additional clustering functionality. It trains, loads, and calls a piecewise density estimator where different base estimators are trained on different clusters of the sample space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
ndarray
|
Samples to train the clustered MAF on. |
required |
base_estimator
|
NICE | KDE | RealNVP
|
The base density estimator to use for each cluster. |
required |
weights
|
ndarray | None
|
Weights for the samples. Defaults to None. |
None
|
theta_ranges
|
ndarray | None
|
Ranges for the parameters. Should have shape (nparams, 2). Defaults to None. |
None
|
clusters
|
ndarray | None | int
|
Predefined cluster labels for each sample or an integer corresponding to the number of expected clusters. If None, k-means clustering is used. Defaults to None. |
None
|
max_cluster_number
|
int
|
Maximum number of clusters to consider when using k-means clustering. Defaults to 10. |
10
|
**kwargs
|
object
|
Additional keyword arguments for the base estimator. |
{}
|
Source code in margarine/estimators/clustered.py
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 | |
load(filename)
classmethod
Load a clustered estimator from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The name of the file to load the estimator from. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
cluster |
cluster
|
The loaded clustered estimator. |
Source code in margarine/estimators/clustered.py
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 | |
log_like(x, logevidence, prior_density)
Compute the marginal log-likelihood of given samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Samples for which to compute the log-likelihood. |
required |
logevidence
|
float
|
Log-evidence term. |
required |
prior_density
|
ndarray | NICE | KDE | RealNVP
|
Prior density or density estimator. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Log-likelihoods of the samples. |
Source code in margarine/estimators/clustered.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
log_prob(x)
Log-probability for a given set of parameters.
While each density estimator has its own built in log probability function, a correction has to be applied for the transformation of variables that is used to improve accuracy when learning and we have to sum probabilities over the series of flows. The correction and the sum are implemented here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
The set of samples for which to calculate the log probability. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: The log-probabilities of the provided samples. |
Source code in margarine/estimators/clustered.py
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 | |
sample(key, num_samples=1000)
Generate samples on the cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
ndarray
|
JAX random key for sampling. |
required |
num_samples
|
int
|
The number of samples to generate. Defaults to 1000. |
1000
|
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Samples generated on the clusterMAF. |
Source code in margarine/estimators/clustered.py
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 | |
save(filename)
Save the clustered estimator to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The name of the file to save the estimator to. |
required |
Source code in margarine/estimators/clustered.py
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 | |
train(**kwargs)
Train the cluster estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
object
|
Additional keyword arguments for training. |
{}
|
Source code in margarine/estimators/clustered.py
150 151 152 153 154 155 156 157 | |
margarine.utils.kmeans
K-Means clustering utility functions.
kmeans(X, k, num_iters=100)
Performs K-Means clustering on the given data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input data of shape (n_samples, n_features). |
required |
k
|
int | ndarray
|
Number of clusters. |
required |
num_iters
|
int
|
Number of iterations for the K-Means algorithm. |
100
|
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Cluster labels for each data point. |
Source code in margarine/utils/kmeans.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 | |
silhouette_score(X, labels)
Calculates the silhouette score for the clustering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input data of shape (n_samples, n_features). |
required |
labels
|
ndarray
|
Cluster labels for each data point. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
ndarray
|
Silhouette score. |
Source code in margarine/utils/kmeans.py
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 | |
margarine.utils.utils
Utility functions for margarine package.
approximate_bounds(theta, weights)
Function to estimate prior bounds from samples.
Sample maximum and minimum are biased estimators of the true bounds of the distribution. This function provides an improved estimate using the weights of the samples. Comes from the expectation value of the maximum and minimum samples of a uniform distribution with an effective number of samples given by Kish's formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
ndarray
|
samples from the target distribution. |
required |
weights
|
ndarray
|
weights for the samples from the target distribution. |
required |
Return
a (jnp.ndarray): estimate of the upper bound on the prior. b (jnp.ndarray): estimate of the lower bound on the prior.
Source code in margarine/utils/utils.py
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 | |
forward_transform(x, min_val, max_val)
Forward transform input samples.
Normalise between 0 and 1 and then transform onto samples of standard normal distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Samples to be normalised. |
required |
min_val
|
float
|
Minimum value for normalization. |
required |
max_val
|
float
|
Maximum value for normalization. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Transformed samples. |
Source code in margarine/utils/utils.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
inverse_transform(x, min_val, max_val)
Inverse transform output samples.
Inverts the processes in forward_transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Samples to be inverse normalised. |
required |
min_val
|
float
|
Minimum value for normalization. |
required |
max_val
|
float
|
Maximum value for normalization. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jnp.ndarray: Inverse transformed samples. |
Source code in margarine/utils/utils.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
train_test_split(a, b, key=None, test_size=0.2)
Splitting data into training and testing sets.
Function is equivalent to sklearn.model_selection.train_test_split but a and b are jax arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ndarray
|
First set of data to be split. |
required |
b
|
ndarray
|
Second set of data to be split. |
required |
test_size
|
float
|
Proportion of data to be used for testing. |
0.2
|
key
|
KeyArray
|
JAX random key for shuffling. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
a_train |
ndarray
|
Training set from a. |
a_test |
ndarray
|
Testing set from a. |
b_train |
ndarray
|
Training set from b. |
b_test |
ndarray
|
Testing set from b. |
Source code in margarine/utils/utils.py
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 | |
margarine.statistics
Statistics functions for margarine.
integrate(density_estimator, likelihood, prior, batch_size=1000, sample_size=10000, logzero=-1e+30, key=jax.random.PRNGKey(0))
Importance sampling integration of a likelihood function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
density_estimator
|
BaseDensityEstimator
|
A density estimator |
required |
likelihood
|
Callable
|
The likelihood function to integrate. |
required |
prior
|
BaseDensityEstimator
|
A density estimator |
required |
batch_size
|
int
|
The number of samples to draw at each iteration. |
1000
|
sample_size
|
int
|
The number of samples to draw in total. |
10000
|
logzero
|
float
|
The definition of zero for the loglikelihood function. |
-1e+30
|
key
|
ndarray
|
JAX random key for sampling. |
PRNGKey(0)
|
Returns:
| Name | Type | Description |
|---|---|---|
stats |
dict
|
Dictionary containing useful statistics |
Source code in margarine/statistics.py
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 | |
kldivergence(density_estimator_p, density_estimator_q, samples_p=None, weights=None, key=jax.random.PRNGKey(0))
Kullback-Leibler divergence between two density estimators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
density_estimator_p
|
BaseDensityEstimator
|
The first |
required |
density_estimator_q
|
BaseDensityEstimator
|
The second |
required |
samples_p
|
ndarray | None
|
Optional samples from the |
None
|
weights
|
ndarray | None
|
Optional weights for the |
None
|
key
|
ndarray
|
JAX random key for sampling. |
PRNGKey(0)
|
Returns:
| Name | Type | Description |
|---|---|---|
kld |
float
|
The Kullback-Leibler divergence D_KL(P || Q). |
Source code in margarine/statistics.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 | |
model_dimensionality(density_estimator_p, density_estimator_q, samples_p=None, weights=None, key=jax.random.PRNGKey(0))
Model dimensionality between two density estimators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
density_estimator_p
|
BaseDensityEstimator
|
The first |
required |
density_estimator_q
|
BaseDensityEstimator
|
The second |
required |
samples_p
|
ndarray | None
|
Optional samples from the |
None
|
weights
|
ndarray | None
|
Optional weights for the |
None
|
key
|
ndarray
|
JAX random key for sampling. |
PRNGKey(0)
|
Returns:
| Name | Type | Description |
|---|---|---|
dim |
float
|
The model dimensionality. |
Source code in margarine/statistics.py
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 | |