distribution
This module defines abstract classes for statistical distributions.
We define 2 types of distributions. The first is
UnivariateDistribution
. Most named statistical distributions we
learn about in a statistics course (Normal, Beta, Uniform, etc.) can
be thought of as instances of UnivariateDistribution
.
Second, we define a statistical distribution whose parameter might not
be known. Such a distribution is tracked by keeping around a really
large sample. We name this a VectorizedDistribution
. In many cases,
working with vectorized distributions is easier since it allows us to
apply algebraic operations to them.
UnivariateDistribution
Bases: Algebra
, Generic[T_in]
A univariate probability distribution.
All named distributions defined in this library are subclasses of
UnivariateDistribution
. This class can also be used by a user for defining new
distributions (as subclasses) but beware that the class mandates
lots of properties and methods be defined for each concrete subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
is_continuous |
used to track if the distribution is continuous or discrete. |
required |
Source code in distribution_algebra/distribution.py
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 |
|
is_continuous
property
Return True if the distribution is continuous, else return False.
Continuity (or distcreteness) of the distribution is the
continuity (or discreteness) of its support space. This is
captured by the Generic type variable T_in
. If the
distribution is continuous (like Normal <:
UnivariateDistribution[numpy.float64]
, the this property returns
True. If the distribution is discrete (like Binomial <:
UnivariateDistribution[numpy.int_]
, then this property is False.
median
abstractmethod
property
Return the median value of the distribution if it is defined.
mode
abstractmethod
property
Return the mode (most-liekely) value of the distribution if it is defined.
support
abstractmethod
property
Return the (min, max) range of the distribution's support interval.
In some cases, if one of the support ends is positive or
negative infinity, we use the closest value to infinity
expressible while using a constrained T_in
type.
__add__
Return the left-sum of a UnivatiateDistribution with any other type.
Types
UnivariateDistribution + UnivariateDistribution
: the result is the sum of the vectorized versions of the two distributions.UnivariateDistribution + Any
: the left-parameter is vectorized and then added to the right-parameter.
Source code in distribution_algebra/distribution.py
__mul__
Return the left-product of a UnivariateDistribution with any other type.
Types
UnivariateDistribution * UnivariateDistribution
: the result is the product of the vectorized versions of the two distributions.UnivariateDistribution * Any
: the left-parameter is vectorized and then multiplied by the right-parameter.
Source code in distribution_algebra/distribution.py
__pow__
Return the left-power of a VectorDistribution with any other type.
Types
UnivariateDistribution ** UnivariateDistribution
: the result is the vectorized version of the first distribution raised to the power of the two distributions.UnivariateDistribution ** Any
: the left-parameter is vectorized and then raised to the right-parameter.
Source code in distribution_algebra/distribution.py
__sub__
Return the left-difference of a UnivariateDistribution with any other type.
Types
UnivariateDistribution - UnivariateDistribution
: the result is the difference of the vectorized versions of the two distributions.UnivariateDistribution - Any
: the left-parameter is vectorized and then the right-parameter is subtracted from it.
Source code in distribution_algebra/distribution.py
__truediv__
Return the left-division of a UnivariateDistribution with any other type.
Types
UnivariateDistribution / UnivariateDistribution
: the result is the ratio of the vectorized versions of the two distributions.UnivariateDistribution / Any
: the left-parameter is vectorized and then divided by the right-parameter.
Source code in distribution_algebra/distribution.py
draw
abstractmethod
Draw a random sample of given size
from the distribution.
For efficiently drawing many random numbers from a
distribution with fixed parameters, it is recommended that
this method be used with the appropriate size
instead of
drawing size=1
samples inside a separate loop.
Note
Not to be confused with distribution_algebra.plotter.plot
method which is used for plotting a distribution's graph.
Implementation detail
This method should always use numpy's default_rng
and its
associated methods for drawing random samples from various
distributions.
Source code in distribution_algebra/distribution.py
pdf
abstractmethod
Return the probability density (or mass) function's values at the given domain points.
Implementation detail
This method usually uses scipy's statistical distribution's probability density/mass functions.
In the case of continuous (or discrete) distributions, this
function accepts a parameter vector of points in
numpy.float64
(or numpy.int_
) space. While the function's
implementation does not put any constraints on the input
vector, the user can usually generate an appropriate input by
using numpy.linspace
or numpy.arange
.
Source code in distribution_algebra/distribution.py
to_vectorized
Convert to a VectorizedDistribution
.
This method is called internally when we perform algebraic operations on distributions whose closed-form expressions are not known.
Note
In most cases, if using (x:
UnivariateDistribution).to_vectorized()
to create a
VectorizedDistribution
instance, the instance's
is_continuous
parameter is equal to x.is_continuous
. In
other words, continuous (discrete) univariate distributions
give rise to continuous (discrete) vectorized
distributions.
Source code in distribution_algebra/distribution.py
VectorizedDistribution
Bases: Algebra
, Generic[T_in]
A Vectorized form of a probability distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample |
the sample-vector used in lieu of a closed-form expression for the distribution. |
required | |
is_continuous |
used to track if the distribution is continuous or discrete. |
required |
Source code in distribution_algebra/distribution.py
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 |
|
mean
property
Mean of the distribution, defined as the mean of the sample.
Returns:
Type | Description |
---|---|
float64
|
mean of the distribution, defined as the mean of the sample. |
var
property
Variance of the distribution, defined as the variance of the sample.
Returns:
Type | Description |
---|---|
float64
|
variance of the distribution, defined as the variance of the sample. |
__add__
Return the left-sum of a VectorDistribution with any other type.
Types
VectorizedDistribution + VectorizedDistribution
: the result is also a vectorized-distrubution whose samples parameter is the element-wise sum of the sample parameters of the summands.VectorizedDistribution + Number
: the result is a vectorized-distribution whose sample values have been shifted.
Source code in distribution_algebra/distribution.py
__mul__
Return the left-product of a VectorDistribution with any other type.
Types
VectorizedDistribution * VectorizedDistribution
: the result is also a vectorized-distrubution whose samples parameter is the element-wise product of the sample parameters of the multiplicands.VectorizedDistribution * Number
: the result is a vectorized-distribution whose sample values have been scaled.
Source code in distribution_algebra/distribution.py
__neg__
Return the negative of a VectorizedDistribution.
Returns:
Type | Description |
---|---|
VectorizedDistribution
|
A VectorizedDistribution whose sample entries are the negative of the original's. |
Source code in distribution_algebra/distribution.py
__pow__
Return the left-power of a VectorDistribution with any other type.
Types
VectorizedDistribution ** VectorizedDistribution
: the result is also a vectorized-distrubution. Result's i'th sample entry isself.sample[i] ** other.sample[i]
.VectorizedDistribution - Number
: the result is a vectorized-distribution. Result's i'th sample entry isself.sample[i] ** other
.
Warns:
Type | Description |
---|---|
- UserWarning
|
if computing |
- UserWarning
|
if computing |
Source code in distribution_algebra/distribution.py
__sub__
Return the left-difference of a VectorDistribution with any other type.
Types
VectorizedDistribution - VectorizedDistribution
: the result is also a vectorized-distrubution whose samples parameter is the element-wise difference of the sample parameters of the arguments.VectorizedDistribution - Number
: the result is a vectorized-distribution whose sample values have been shifted.
Source code in distribution_algebra/distribution.py
__truediv__
Return the left-division of a VectorDistribution with any other type.
Types
VectorizedDistribution / VectorizedDistribution
: the result is also a vectorized-distrubution whose sample parameter is the element-wise ratio of the sample parameters of the arguments.VectorizedDistribution / Number
: the result is a vectorized-distribution whose sample parameter has been scaled down.
Warns:
Type | Description |
---|---|
- UserWarning
|
if computing |