distributions
module
Source code
import numpy as np
import scipy.stats as st
# Bulge distributions
class radialPlummer(st.rv_continuous):
def _pdf(self, x):#(3M/4πa3)(1+(r/a)2)−5/2
return 3/(4*np.pi**3) * (1 + x**2)**(-5/2) * 4*np.pi*x**2
PLUMMER = radialPlummer(a=0, b=5, name='rPlummer')
# [TODO: Check Hernquist. Not currently in use]
class radialHernquist(st.rv_continuous):
def _pdf(self, x):
return 1/(2*np.pi) * 1/(x**4) * 4*np.pi*x**2
HERNQUIST = radialHernquist(a=0, b=5, name='rHernquist')
# Disk distributions
class radialUniform(st.rv_continuous):
def _pdf(self, x):
return 2*x if x<1 else 0
UNIFORM = radialUniform(a=0, b=10, name='rUniform')
class radialExp(st.rv_continuous):
def _pdf(self, x):
return x*np.exp(-x)
EXP = radialExp(a=0, b=10, name='rExp')
# Halo distributions
class radialNFW(st.rv_continuous):
def _pdf(self, x):
y = 1 / (x * (1 + x)**2) * x**2
# Normalize pdf in (0, 5) range
y /= (-5/6 + np.log(6))
NFW = radialNFW(a=0, b=5, name='rNFW')
Classes
class radialExp (ancestors: scipy.stats._distn_infrastructure.rv_continuous, scipy.stats._distn_infrastructure.rv_generic)
-
A generic continuous random variable class meant for subclassing.
rv_continuous
is a base class to construct specific distribution classes and instances for continuous random variables. It cannot be used directly as a distribution.Parameters
momtype
:int
, optional- The type of generic moment calculation to use: 0 for pdf, 1 (default) for ppf.
a
:float
, optional- Lower bound of the support of the distribution, default is minus infinity.
b
:float
, optional- Upper bound of the support of the distribution, default is plus infinity.
xtol
:float
, optional- The tolerance for fixed point calculation for generic ppf.
badvalue
:float
, optional- The value in a result arrays that indicates a value that for which some argument restriction is violated, default is np.nan.
name
:str
, optional- The name of the instance. This string is used to construct the default example for distributions.
longname
:str
, optional- This string is used as part of the first line of the docstring returned
when a subclass has no docstring of its own. Note:
longname
exists for backwards compatibility, do not use for new subclasses. shapes
:str
, optional- The shape of the distribution. For example
"m, n"
for a distribution that takes two integers as the two shape arguments for all its methods. If not provided, shape parameters will be inferred from the signature of the private methods,_pdf
and_cdf
of the instance. extradoc
:str
, optional,deprecated
- This string is used as the last part of the docstring returned when a
subclass has no docstring of its own. Note:
extradoc
exists for backwards compatibility, do not use for new subclasses. seed
:None
orint
ornumpy.random.RandomState
instance
, optional- This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance. Default is None.
Methods
rvs pdf logpdf cdf logcdf sf logsf ppf isf moment stats entropy expect median mean std var interval call fit fit_loc_scale nnlf
Notes
Public methods of an instance of a distribution class (e.g.,
pdf
,cdf
) check their arguments and pass valid arguments to private, computational methods (_pdf
,_cdf
). Forpdf(x)
,x
is valid if it is within the support of a distribution,self.a <= x <= self.b
. Whether a shape parameter is valid is decided by an_argcheck
method (which defaults to checking that its arguments are strictly positive.)Subclassing
New random variables can be defined by subclassing the
rv_continuous
class and re-defining at least the_pdf
or the_cdf
method (normalized to location 0 and scale 1).If positive argument checking is not correct for your RV then you will also need to re-define the
_argcheck
method.Correct, but potentially slow defaults exist for the remaining methods but for speed and/or accuracy you can over-ride::
_logpdf, _cdf, _logcdf, _ppf, _rvs, _isf, _sf, _logsf
Rarely would you override
_isf
,_sf
or_logsf
, but you could.Methods that can be overwritten by subclasses ::
_rvs _pdf _cdf _sf _ppf _isf _stats _munp _entropy _argcheck
There are additional (internal and private) generic methods that can be useful for cross-checking and for debugging, but might work in all cases when directly called.
A note on
shapes
: subclasses need not specify them explicitly. In this case,shapes
will be automatically deduced from the signatures of the overridden methods (pdf
,cdf
etc). If, for some reason, you prefer to avoid relying on introspection, you can specifyshapes
explicitly as an argument to the instance constructor.Frozen Distributions
Normally, you must provide shape parameters (and, optionally, location and scale parameters to each call of a method of a distribution.
Alternatively, the object may be called (as a function) to fix the shape, location, and scale parameters returning a "frozen" continuous RV object:
rv = generic(
, loc=0, scale=1) frozen RV object with the same methods but holding the given shape, location, and scale fixed Statistics
Statistics are computed using numerical integration by default. For speed you can redefine this using
_stats
:- take shape parameters and return mu, mu2, g1, g2
- If you can't compute one of these, return it as None
- Can also be defined with a keyword argument
moments
, which is a string composed of "m", "v", "s", and/or "k". Only the components appearing in string should be computed and returned in the order "m", "v", "s", or "k" with missing values returned as None.
Alternatively, you can override
_munp
, which takesn
and shape parameters and returns the n-th non-central moment of the distribution.Examples
To create a new Gaussian distribution, we would do the following:
>>> from scipy.stats import rv_continuous >>> class gaussian_gen(rv_continuous): ... "Gaussian distribution" ... def _pdf(self, x): ... return np.exp(-x**2 / 2.) / np.sqrt(2.0 * np.pi) >>> gaussian = gaussian_gen(name='gaussian')
scipy.stats
distributions are instances, so here we subclassrv_continuous
and create an instance. With this, we now have a fully functional distribution with all relevant methods automagically generated by the framework.Note that above we defined a standard normal distribution, with zero mean and unit variance. Shifting and scaling of the distribution can be done by using
loc
andscale
parameters:gaussian.pdf(x, loc, scale)
essentially computesy = (x - loc) / scale
andgaussian._pdf(y) / scale
.Source code
class radialExp(st.rv_continuous): def _pdf(self, x): return x*np.exp(-x)
class radialHernquist (ancestors: scipy.stats._distn_infrastructure.rv_continuous, scipy.stats._distn_infrastructure.rv_generic)
-
A generic continuous random variable class meant for subclassing.
rv_continuous
is a base class to construct specific distribution classes and instances for continuous random variables. It cannot be used directly as a distribution.Parameters
momtype
:int
, optional- The type of generic moment calculation to use: 0 for pdf, 1 (default) for ppf.
a
:float
, optional- Lower bound of the support of the distribution, default is minus infinity.
b
:float
, optional- Upper bound of the support of the distribution, default is plus infinity.
xtol
:float
, optional- The tolerance for fixed point calculation for generic ppf.
badvalue
:float
, optional- The value in a result arrays that indicates a value that for which some argument restriction is violated, default is np.nan.
name
:str
, optional- The name of the instance. This string is used to construct the default example for distributions.
longname
:str
, optional- This string is used as part of the first line of the docstring returned
when a subclass has no docstring of its own. Note:
longname
exists for backwards compatibility, do not use for new subclasses. shapes
:str
, optional- The shape of the distribution. For example
"m, n"
for a distribution that takes two integers as the two shape arguments for all its methods. If not provided, shape parameters will be inferred from the signature of the private methods,_pdf
and_cdf
of the instance. extradoc
:str
, optional,deprecated
- This string is used as the last part of the docstring returned when a
subclass has no docstring of its own. Note:
extradoc
exists for backwards compatibility, do not use for new subclasses. seed
:None
orint
ornumpy.random.RandomState
instance
, optional- This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance. Default is None.
Methods
rvs pdf logpdf cdf logcdf sf logsf ppf isf moment stats entropy expect median mean std var interval call fit fit_loc_scale nnlf
Notes
Public methods of an instance of a distribution class (e.g.,
pdf
,cdf
) check their arguments and pass valid arguments to private, computational methods (_pdf
,_cdf
). Forpdf(x)
,x
is valid if it is within the support of a distribution,self.a <= x <= self.b
. Whether a shape parameter is valid is decided by an_argcheck
method (which defaults to checking that its arguments are strictly positive.)Subclassing
New random variables can be defined by subclassing the
rv_continuous
class and re-defining at least the_pdf
or the_cdf
method (normalized to location 0 and scale 1).If positive argument checking is not correct for your RV then you will also need to re-define the
_argcheck
method.Correct, but potentially slow defaults exist for the remaining methods but for speed and/or accuracy you can over-ride::
_logpdf, _cdf, _logcdf, _ppf, _rvs, _isf, _sf, _logsf
Rarely would you override
_isf
,_sf
or_logsf
, but you could.Methods that can be overwritten by subclasses ::
_rvs _pdf _cdf _sf _ppf _isf _stats _munp _entropy _argcheck
There are additional (internal and private) generic methods that can be useful for cross-checking and for debugging, but might work in all cases when directly called.
A note on
shapes
: subclasses need not specify them explicitly. In this case,shapes
will be automatically deduced from the signatures of the overridden methods (pdf
,cdf
etc). If, for some reason, you prefer to avoid relying on introspection, you can specifyshapes
explicitly as an argument to the instance constructor.Frozen Distributions
Normally, you must provide shape parameters (and, optionally, location and scale parameters to each call of a method of a distribution.
Alternatively, the object may be called (as a function) to fix the shape, location, and scale parameters returning a "frozen" continuous RV object:
rv = generic(
, loc=0, scale=1) frozen RV object with the same methods but holding the given shape, location, and scale fixed Statistics
Statistics are computed using numerical integration by default. For speed you can redefine this using
_stats
:- take shape parameters and return mu, mu2, g1, g2
- If you can't compute one of these, return it as None
- Can also be defined with a keyword argument
moments
, which is a string composed of "m", "v", "s", and/or "k". Only the components appearing in string should be computed and returned in the order "m", "v", "s", or "k" with missing values returned as None.
Alternatively, you can override
_munp
, which takesn
and shape parameters and returns the n-th non-central moment of the distribution.Examples
To create a new Gaussian distribution, we would do the following:
>>> from scipy.stats import rv_continuous >>> class gaussian_gen(rv_continuous): ... "Gaussian distribution" ... def _pdf(self, x): ... return np.exp(-x**2 / 2.) / np.sqrt(2.0 * np.pi) >>> gaussian = gaussian_gen(name='gaussian')
scipy.stats
distributions are instances, so here we subclassrv_continuous
and create an instance. With this, we now have a fully functional distribution with all relevant methods automagically generated by the framework.Note that above we defined a standard normal distribution, with zero mean and unit variance. Shifting and scaling of the distribution can be done by using
loc
andscale
parameters:gaussian.pdf(x, loc, scale)
essentially computesy = (x - loc) / scale
andgaussian._pdf(y) / scale
.Source code
class radialHernquist(st.rv_continuous): def _pdf(self, x): return 1/(2*np.pi) * 1/(x**4) * 4*np.pi*x**2
class radialNFW (ancestors: scipy.stats._distn_infrastructure.rv_continuous, scipy.stats._distn_infrastructure.rv_generic)
-
A generic continuous random variable class meant for subclassing.
rv_continuous
is a base class to construct specific distribution classes and instances for continuous random variables. It cannot be used directly as a distribution.Parameters
momtype
:int
, optional- The type of generic moment calculation to use: 0 for pdf, 1 (default) for ppf.
a
:float
, optional- Lower bound of the support of the distribution, default is minus infinity.
b
:float
, optional- Upper bound of the support of the distribution, default is plus infinity.
xtol
:float
, optional- The tolerance for fixed point calculation for generic ppf.
badvalue
:float
, optional- The value in a result arrays that indicates a value that for which some argument restriction is violated, default is np.nan.
name
:str
, optional- The name of the instance. This string is used to construct the default example for distributions.
longname
:str
, optional- This string is used as part of the first line of the docstring returned
when a subclass has no docstring of its own. Note:
longname
exists for backwards compatibility, do not use for new subclasses. shapes
:str
, optional- The shape of the distribution. For example
"m, n"
for a distribution that takes two integers as the two shape arguments for all its methods. If not provided, shape parameters will be inferred from the signature of the private methods,_pdf
and_cdf
of the instance. extradoc
:str
, optional,deprecated
- This string is used as the last part of the docstring returned when a
subclass has no docstring of its own. Note:
extradoc
exists for backwards compatibility, do not use for new subclasses. seed
:None
orint
ornumpy.random.RandomState
instance
, optional- This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance. Default is None.
Methods
rvs pdf logpdf cdf logcdf sf logsf ppf isf moment stats entropy expect median mean std var interval call fit fit_loc_scale nnlf
Notes
Public methods of an instance of a distribution class (e.g.,
pdf
,cdf
) check their arguments and pass valid arguments to private, computational methods (_pdf
,_cdf
). Forpdf(x)
,x
is valid if it is within the support of a distribution,self.a <= x <= self.b
. Whether a shape parameter is valid is decided by an_argcheck
method (which defaults to checking that its arguments are strictly positive.)Subclassing
New random variables can be defined by subclassing the
rv_continuous
class and re-defining at least the_pdf
or the_cdf
method (normalized to location 0 and scale 1).If positive argument checking is not correct for your RV then you will also need to re-define the
_argcheck
method.Correct, but potentially slow defaults exist for the remaining methods but for speed and/or accuracy you can over-ride::
_logpdf, _cdf, _logcdf, _ppf, _rvs, _isf, _sf, _logsf
Rarely would you override
_isf
,_sf
or_logsf
, but you could.Methods that can be overwritten by subclasses ::
_rvs _pdf _cdf _sf _ppf _isf _stats _munp _entropy _argcheck
There are additional (internal and private) generic methods that can be useful for cross-checking and for debugging, but might work in all cases when directly called.
A note on
shapes
: subclasses need not specify them explicitly. In this case,shapes
will be automatically deduced from the signatures of the overridden methods (pdf
,cdf
etc). If, for some reason, you prefer to avoid relying on introspection, you can specifyshapes
explicitly as an argument to the instance constructor.Frozen Distributions
Normally, you must provide shape parameters (and, optionally, location and scale parameters to each call of a method of a distribution.
Alternatively, the object may be called (as a function) to fix the shape, location, and scale parameters returning a "frozen" continuous RV object:
rv = generic(
, loc=0, scale=1) frozen RV object with the same methods but holding the given shape, location, and scale fixed Statistics
Statistics are computed using numerical integration by default. For speed you can redefine this using
_stats
:- take shape parameters and return mu, mu2, g1, g2
- If you can't compute one of these, return it as None
- Can also be defined with a keyword argument
moments
, which is a string composed of "m", "v", "s", and/or "k". Only the components appearing in string should be computed and returned in the order "m", "v", "s", or "k" with missing values returned as None.
Alternatively, you can override
_munp
, which takesn
and shape parameters and returns the n-th non-central moment of the distribution.Examples
To create a new Gaussian distribution, we would do the following:
>>> from scipy.stats import rv_continuous >>> class gaussian_gen(rv_continuous): ... "Gaussian distribution" ... def _pdf(self, x): ... return np.exp(-x**2 / 2.) / np.sqrt(2.0 * np.pi) >>> gaussian = gaussian_gen(name='gaussian')
scipy.stats
distributions are instances, so here we subclassrv_continuous
and create an instance. With this, we now have a fully functional distribution with all relevant methods automagically generated by the framework.Note that above we defined a standard normal distribution, with zero mean and unit variance. Shifting and scaling of the distribution can be done by using
loc
andscale
parameters:gaussian.pdf(x, loc, scale)
essentially computesy = (x - loc) / scale
andgaussian._pdf(y) / scale
.Source code
class radialNFW(st.rv_continuous): def _pdf(self, x): y = 1 / (x * (1 + x)**2) * x**2 # Normalize pdf in (0, 5) range y /= (-5/6 + np.log(6))
class radialPlummer (ancestors: scipy.stats._distn_infrastructure.rv_continuous, scipy.stats._distn_infrastructure.rv_generic)
-
A generic continuous random variable class meant for subclassing.
rv_continuous
is a base class to construct specific distribution classes and instances for continuous random variables. It cannot be used directly as a distribution.Parameters
momtype
:int
, optional- The type of generic moment calculation to use: 0 for pdf, 1 (default) for ppf.
a
:float
, optional- Lower bound of the support of the distribution, default is minus infinity.
b
:float
, optional- Upper bound of the support of the distribution, default is plus infinity.
xtol
:float
, optional- The tolerance for fixed point calculation for generic ppf.
badvalue
:float
, optional- The value in a result arrays that indicates a value that for which some argument restriction is violated, default is np.nan.
name
:str
, optional- The name of the instance. This string is used to construct the default example for distributions.
longname
:str
, optional- This string is used as part of the first line of the docstring returned
when a subclass has no docstring of its own. Note:
longname
exists for backwards compatibility, do not use for new subclasses. shapes
:str
, optional- The shape of the distribution. For example
"m, n"
for a distribution that takes two integers as the two shape arguments for all its methods. If not provided, shape parameters will be inferred from the signature of the private methods,_pdf
and_cdf
of the instance. extradoc
:str
, optional,deprecated
- This string is used as the last part of the docstring returned when a
subclass has no docstring of its own. Note:
extradoc
exists for backwards compatibility, do not use for new subclasses. seed
:None
orint
ornumpy.random.RandomState
instance
, optional- This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance. Default is None.
Methods
rvs pdf logpdf cdf logcdf sf logsf ppf isf moment stats entropy expect median mean std var interval call fit fit_loc_scale nnlf
Notes
Public methods of an instance of a distribution class (e.g.,
pdf
,cdf
) check their arguments and pass valid arguments to private, computational methods (_pdf
,_cdf
). Forpdf(x)
,x
is valid if it is within the support of a distribution,self.a <= x <= self.b
. Whether a shape parameter is valid is decided by an_argcheck
method (which defaults to checking that its arguments are strictly positive.)Subclassing
New random variables can be defined by subclassing the
rv_continuous
class and re-defining at least the_pdf
or the_cdf
method (normalized to location 0 and scale 1).If positive argument checking is not correct for your RV then you will also need to re-define the
_argcheck
method.Correct, but potentially slow defaults exist for the remaining methods but for speed and/or accuracy you can over-ride::
_logpdf, _cdf, _logcdf, _ppf, _rvs, _isf, _sf, _logsf
Rarely would you override
_isf
,_sf
or_logsf
, but you could.Methods that can be overwritten by subclasses ::
_rvs _pdf _cdf _sf _ppf _isf _stats _munp _entropy _argcheck
There are additional (internal and private) generic methods that can be useful for cross-checking and for debugging, but might work in all cases when directly called.
A note on
shapes
: subclasses need not specify them explicitly. In this case,shapes
will be automatically deduced from the signatures of the overridden methods (pdf
,cdf
etc). If, for some reason, you prefer to avoid relying on introspection, you can specifyshapes
explicitly as an argument to the instance constructor.Frozen Distributions
Normally, you must provide shape parameters (and, optionally, location and scale parameters to each call of a method of a distribution.
Alternatively, the object may be called (as a function) to fix the shape, location, and scale parameters returning a "frozen" continuous RV object:
rv = generic(
, loc=0, scale=1) frozen RV object with the same methods but holding the given shape, location, and scale fixed Statistics
Statistics are computed using numerical integration by default. For speed you can redefine this using
_stats
:- take shape parameters and return mu, mu2, g1, g2
- If you can't compute one of these, return it as None
- Can also be defined with a keyword argument
moments
, which is a string composed of "m", "v", "s", and/or "k". Only the components appearing in string should be computed and returned in the order "m", "v", "s", or "k" with missing values returned as None.
Alternatively, you can override
_munp
, which takesn
and shape parameters and returns the n-th non-central moment of the distribution.Examples
To create a new Gaussian distribution, we would do the following:
>>> from scipy.stats import rv_continuous >>> class gaussian_gen(rv_continuous): ... "Gaussian distribution" ... def _pdf(self, x): ... return np.exp(-x**2 / 2.) / np.sqrt(2.0 * np.pi) >>> gaussian = gaussian_gen(name='gaussian')
scipy.stats
distributions are instances, so here we subclassrv_continuous
and create an instance. With this, we now have a fully functional distribution with all relevant methods automagically generated by the framework.Note that above we defined a standard normal distribution, with zero mean and unit variance. Shifting and scaling of the distribution can be done by using
loc
andscale
parameters:gaussian.pdf(x, loc, scale)
essentially computesy = (x - loc) / scale
andgaussian._pdf(y) / scale
.Source code
class radialPlummer(st.rv_continuous): def _pdf(self, x):#(3M/4πa3)(1+(r/a)2)−5/2 return 3/(4*np.pi**3) * (1 + x**2)**(-5/2) * 4*np.pi*x**2
class radialUniform (ancestors: scipy.stats._distn_infrastructure.rv_continuous, scipy.stats._distn_infrastructure.rv_generic)
-
A generic continuous random variable class meant for subclassing.
rv_continuous
is a base class to construct specific distribution classes and instances for continuous random variables. It cannot be used directly as a distribution.Parameters
momtype
:int
, optional- The type of generic moment calculation to use: 0 for pdf, 1 (default) for ppf.
a
:float
, optional- Lower bound of the support of the distribution, default is minus infinity.
b
:float
, optional- Upper bound of the support of the distribution, default is plus infinity.
xtol
:float
, optional- The tolerance for fixed point calculation for generic ppf.
badvalue
:float
, optional- The value in a result arrays that indicates a value that for which some argument restriction is violated, default is np.nan.
name
:str
, optional- The name of the instance. This string is used to construct the default example for distributions.
longname
:str
, optional- This string is used as part of the first line of the docstring returned
when a subclass has no docstring of its own. Note:
longname
exists for backwards compatibility, do not use for new subclasses. shapes
:str
, optional- The shape of the distribution. For example
"m, n"
for a distribution that takes two integers as the two shape arguments for all its methods. If not provided, shape parameters will be inferred from the signature of the private methods,_pdf
and_cdf
of the instance. extradoc
:str
, optional,deprecated
- This string is used as the last part of the docstring returned when a
subclass has no docstring of its own. Note:
extradoc
exists for backwards compatibility, do not use for new subclasses. seed
:None
orint
ornumpy.random.RandomState
instance
, optional- This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance. Default is None.
Methods
rvs pdf logpdf cdf logcdf sf logsf ppf isf moment stats entropy expect median mean std var interval call fit fit_loc_scale nnlf
Notes
Public methods of an instance of a distribution class (e.g.,
pdf
,cdf
) check their arguments and pass valid arguments to private, computational methods (_pdf
,_cdf
). Forpdf(x)
,x
is valid if it is within the support of a distribution,self.a <= x <= self.b
. Whether a shape parameter is valid is decided by an_argcheck
method (which defaults to checking that its arguments are strictly positive.)Subclassing
New random variables can be defined by subclassing the
rv_continuous
class and re-defining at least the_pdf
or the_cdf
method (normalized to location 0 and scale 1).If positive argument checking is not correct for your RV then you will also need to re-define the
_argcheck
method.Correct, but potentially slow defaults exist for the remaining methods but for speed and/or accuracy you can over-ride::
_logpdf, _cdf, _logcdf, _ppf, _rvs, _isf, _sf, _logsf
Rarely would you override
_isf
,_sf
or_logsf
, but you could.Methods that can be overwritten by subclasses ::
_rvs _pdf _cdf _sf _ppf _isf _stats _munp _entropy _argcheck
There are additional (internal and private) generic methods that can be useful for cross-checking and for debugging, but might work in all cases when directly called.
A note on
shapes
: subclasses need not specify them explicitly. In this case,shapes
will be automatically deduced from the signatures of the overridden methods (pdf
,cdf
etc). If, for some reason, you prefer to avoid relying on introspection, you can specifyshapes
explicitly as an argument to the instance constructor.Frozen Distributions
Normally, you must provide shape parameters (and, optionally, location and scale parameters to each call of a method of a distribution.
Alternatively, the object may be called (as a function) to fix the shape, location, and scale parameters returning a "frozen" continuous RV object:
rv = generic(
, loc=0, scale=1) frozen RV object with the same methods but holding the given shape, location, and scale fixed Statistics
Statistics are computed using numerical integration by default. For speed you can redefine this using
_stats
:- take shape parameters and return mu, mu2, g1, g2
- If you can't compute one of these, return it as None
- Can also be defined with a keyword argument
moments
, which is a string composed of "m", "v", "s", and/or "k". Only the components appearing in string should be computed and returned in the order "m", "v", "s", or "k" with missing values returned as None.
Alternatively, you can override
_munp
, which takesn
and shape parameters and returns the n-th non-central moment of the distribution.Examples
To create a new Gaussian distribution, we would do the following:
>>> from scipy.stats import rv_continuous >>> class gaussian_gen(rv_continuous): ... "Gaussian distribution" ... def _pdf(self, x): ... return np.exp(-x**2 / 2.) / np.sqrt(2.0 * np.pi) >>> gaussian = gaussian_gen(name='gaussian')
scipy.stats
distributions are instances, so here we subclassrv_continuous
and create an instance. With this, we now have a fully functional distribution with all relevant methods automagically generated by the framework.Note that above we defined a standard normal distribution, with zero mean and unit variance. Shifting and scaling of the distribution can be done by using
loc
andscale
parameters:gaussian.pdf(x, loc, scale)
essentially computesy = (x - loc) / scale
andgaussian._pdf(y) / scale
.Source code
class radialUniform(st.rv_continuous): def _pdf(self, x): return 2*x if x<1 else 0