Distributions Facade#
statista.distributions.Distributions
#
Facade for working with probability distributions.
Distributions can be used in two modes:
- Single-distribution mode: pass a distribution name to wrap a specific distribution and delegate all method calls to it.
- Multi-distribution mode: pass only data (no distribution name)
and use
fit/best_fitto compare all distributions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str | None
|
Name of the distribution to use. Must be one of the
keys in |
None
|
data
|
list | ndarray | None
|
Data time series as a list or numpy array. |
None
|
parameters
|
dict[str, Any] | Parameters | None
|
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
available_distributions |
dict[str, type[AbstractDistribution]]
|
Registry mapping distribution names to their classes. |
distribution |
AbstractDistribution | None
|
The underlying distribution instance (None in multi-distribution mode). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the distribution name is not in
|
ValueError
|
If neither distribution nor data is provided. |
Examples:
- Single-distribution mode — wrap a Gumbel and fit:
>>> import numpy as np >>> from statista.distributions import Distributions >>> data = np.loadtxt("examples/data/time_series2.txt") >>> dist = Distributions("Gumbel", data=data) >>> params = dist.fit_model(method="lmoments", test=False) >>> params.loc is not None True >>> params.scale is not None True - Multi-distribution mode — find the best fit in one call:
- Create a distribution from known parameters:
- Invalid distribution name raises ValueError:
See Also
Gumbel: Gumbel (Extreme Value Type I) distribution. GEV: Generalized Extreme Value distribution. Exponential: Exponential distribution. Normal: Normal (Gaussian) distribution.
Source code in src/statista/distributions/facade.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 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 | |
__getattr__(name)
#
Delegate attribute access to the underlying distribution instance.
Any attribute or method not defined directly on Distributions
is looked up on the wrapped distribution object. This allows
transparent access to pdf, cdf, fit_model, ks,
chisquare, inverse_cdf, confidence_interval, plot,
and all other methods of the concrete distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Attribute name to look up. |
required |
Returns:
| Type | Description |
|---|---|
|
The attribute from the underlying distribution instance. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If neither |
Source code in src/statista/distributions/facade.py
fit(method='lmoments', distributions=None)
#
Fit multiple distributions to the data and evaluate goodness of fit.
Fits each distribution using the specified method, then runs both the Kolmogorov-Smirnov and Chi-square goodness-of-fit tests. NaN values are removed and the data is sorted before fitting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
Fitting method ('mle', 'mm', 'lmoments', or 'optimization'). Default is 'lmoments'. |
'lmoments'
|
distributions
|
list[str] | None
|
List of distribution names to fit. If None, fits all available distributions ('GEV', 'Gumbel', 'Exponential', 'Normal'). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict[str, dict[str, Any]]
|
Dictionary keyed by distribution name, each value is a dict |
|
containing |
dict[str, dict[str, Any]]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a distribution name is not in
|
Examples:
- Fit all distributions and inspect the result keys:
>>> import numpy as np >>> from statista.distributions import Distributions >>> data = np.loadtxt("examples/data/time_series2.txt") >>> dist = Distributions(data=data) >>> results = dist.fit() # doctest: +ELLIPSIS -----KS Test-------- ... >>> sorted(results.keys()) ['Exponential', 'GEV', 'Gumbel', 'Normal'] - Fit only a subset of distributions:
>>> import numpy as np >>> from statista.distributions import Distributions >>> data = np.loadtxt("examples/data/time_series2.txt") >>> dist = Distributions(data=data) >>> results = dist.fit( ... distributions=["Gumbel", "GEV"] ... ) # doctest: +ELLIPSIS -----KS Test-------- ... >>> sorted(results.keys()) ['GEV', 'Gumbel'] - Access fitted parameters and KS p-value:
>>> import numpy as np >>> from statista.distributions import Distributions >>> data = np.loadtxt("examples/data/time_series2.txt") >>> dist = Distributions(data=data) >>> results = dist.fit( ... distributions=["Gumbel"] ... ) # doctest: +ELLIPSIS -----KS Test-------- ... >>> results["Gumbel"]["parameters"].loc is not None True >>> bool(0 <= results["Gumbel"]["ks"][1] <= 1) True
See Also
best_fit: Fit all distributions and directly return the best one.
Source code in src/statista/distributions/facade.py
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 | |
best_fit(method='lmoments', distributions=None, criterion='ks')
#
Find the best-fitting distribution for the data.
Fits all (or selected) distributions and returns the one with the highest goodness-of-fit p-value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
Fitting method ('mle', 'mm', 'lmoments', or 'optimization'). Default is 'lmoments'. |
'lmoments'
|
distributions
|
list[str] | None
|
List of distribution names to fit. If None, fits all available distributions. |
None
|
criterion
|
str
|
Goodness-of-fit criterion for selection. 'ks' selects by highest Kolmogorov-Smirnov p-value. 'chisquare' selects by highest Chi-square p-value. Default is 'ks'. |
'ks'
|
Returns:
| Type | Description |
|---|---|
str
|
Tuple of (distribution_name, result_dict) for the best fit. |
dict[str, Any]
|
The result dict contains:
- 'distribution': the fitted distribution instance
- 'parameters': |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
- Find the best distribution directly from data:
>>> import numpy as np >>> from statista.distributions import Distributions >>> data = np.loadtxt("examples/data/time_series2.txt") >>> dist = Distributions(data=data) >>> best_name, best_info = dist.best_fit() # doctest: +ELLIPSIS -----KS Test-------- ... >>> best_name 'GEV' >>> best_info["parameters"].shape is not None True - Select by Chi-square criterion among specific distributions:
>>> import numpy as np >>> from statista.distributions import Distributions >>> data = np.loadtxt("examples/data/time_series2.txt") >>> dist = Distributions(data=data) >>> best_name, best_info = dist.best_fit( ... distributions=["Gumbel", "GEV"], ... criterion="chisquare", ... ) # doctest: +ELLIPSIS -----KS Test-------- ... >>> best_name in ("Gumbel", "GEV") True
See Also
fit: Fit multiple distributions and return all results.
Source code in src/statista/distributions/facade.py
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 | |