Package: twopiece
twopiece: Two-Piece DistributionsHomepage:https://github.com/quantgirluk/twopiecePip Repository:twopieceDemo:Python NotebookOverviewSupported DistributionsMain FeaturesQuick StartInstallOverviewThetwopiecelibrary provides aPythonimplementation of the family of Two Piece
distributions. It covers three subfamiliesTwo-Piece Scale,Two-Piece Shape,
andDouble Two-Piece. The following diagram shows how these families relate.Two-Piece ScaleThe family oftwo–piece scale distributionsis a family of univariate three parameter location-scale models,
whereskewnessis introduced by differingscaleparameters either side of themode.Definition.Let $f: \mathbb{R} \mapsto \mathbb{R}_{+}$ be a unimodal symmetric (around zero) probability
density function (pdf) from thelocation-scale family,
possibly including ashape parameter$\delta$. Then, the
pdf of a member of the two-piece family of distributions is given by$$
s\left(x; \mu,\sigma_1,\sigma_2, \delta\right) =
\begin{cases}
\dfrac{2}{\sigma_1+\sigma_2}f\left(\dfrac{x-\mu}{\sigma_1};\delta\right), \mbox{if } x < \mu, \
\dfrac{2}{\sigma_1+\sigma_2}f\left(\dfrac{x-\mu}{\sigma_2};\delta\right), \mbox{if } x \geq \mu. \
\end{cases}
$$Example.If $f$ corresponds to the normal pdf, then $s$ corresponds to the pdf of theTwo-Piece Normaldistribution
as proposed byGustav Fechnerin 1887.Two-Piece ShapeThe family oftwo–piece shape distributionsis a family of univariate three parameter location-scale
models, whereskewnessis introduced by differingshapeparameters either side of the mode.Definition.Let $f: \mathbb{R} \mapsto \mathbb{R}_{+}$ be a unimodal symmetric (around 0) probability
density function (pdf) from thelocation-scale familywhich includes ashape parameter$\delta$. Then, the pdf of a member
of the two-piece family of distributions is given by$$
s\left(x; \mu,\sigma,\delta_1 \delta_2\right) =
\begin{cases}
\dfrac{2\epsilon}{\sigma}f\left(\dfrac{x-\mu}{\sigma};\delta_1\right), \mbox{if } x < \mu, \
\dfrac{2(1 -\epsilon)}{\sigma}f\left(\dfrac{x-\mu}{\sigma};\delta_2\right), \mbox{if } x \geq \mu. \
\end{cases}
$$where$$
\epsilon = \dfrac{f(0;\delta_2)}{f(0;\delta_1)+f(0;\delta_2)}.
$$Example.If $f$ corresponds to the Student-t pdf, then $s$ corresponds to the pdf of the
Two-Piece Shape Student-t distribution. Note that $s$ has different shape parameter on each side mode but
the same scale.Double Two-PieceThe family ofdouble two–piece distributionsis obtained by using
a density–based transformation of unimodal symmetric continuous distributions with a shape parameter. The
resulting distributions contain five interpretable parameters that control the mode, as well as bothscale
and shapein each direction.Definition.Let $f: \mathbb{R} \mapsto \mathbb{R}_{+}$ be a unimodal symmetric (around 0) probability density function (pdf) from thelocation-scale familywhich includes ashape parameter$\delta$. Then, the pdf of a member of the two-piece family of distributions is given by$$
s\left(x; \mu,\sigma_1,\sigma_2, \delta_1, \delta_2 \right) =
\begin{cases}
\dfrac{2\epsilon}{\sigma_1}f\left(\dfrac{x-\mu}{\sigma_1};\delta_1\right), \mbox{if } x < \mu, \
\dfrac{2(1 -\epsilon)}{\sigma_2}f\left(\dfrac{x-\mu}{\sigma_2};\delta_2\right), \mbox{if } x \geq \mu. \
\end{cases}
$$where$$
\epsilon = \dfrac{\sigma_1f(0;\delta_2)}{\sigma_2f(0;\delta_1)+\sigma_1 f(0;\delta_2)}.
$$Example.If $f$ corresponds to the Student-t pdf then $s$ corresponds to the pdf of the Double Two-Piece Student-t distribution. Note that $s$ has different scale and shape on each side of the mode.NotesFor technical details on this families of distributions we refer to the following two publications which serve as reference for our implementation.Inference in Two-Piece Location-Scale Models with Jeffreys Priorspublished inBayesian Anal.Volume 9, Number 1 (2014), 1-22.Bayesian modelling of skewness and kurtosis with Two-Piece Scale and shape distributionspublished inElectron. J. Statist., Volume 9, Number 2 (2015), 1884-1912.For theRimplementation we refer to the following packages.twopiece, DTP, and TPSAStwopiecehas been developed and tested onPython 3.6, and 3.7.Supported DistributionsImplementation is provided for the following distributions.Two-Piece ScaleNameFunctionParametersTwo-Piece Normaltpnormloc, sigma1, sigma2Two-Piece Laplacetplaplaceloc, sigma1, sigma2Two-Piece Cauchytpcauchyloc, sigma1, sigma2Two-Piece Logistictplogisticloc, sigma1, sigma2Two-Piece Student-ttpstudentloc, sigma1, sigma2, shapeTwo-Piece Exponential Powertpgennormloc, sigma1, sigma2, shapeTwo-Piece SinhArcSinhtpsasloc, sigma1, sigma2, shapeTwo-Piece ShapeNameFunctionParametersTwo-Piece-Shape Student-ttpshastudentloc, sigma, shape1, shape2Two-Piece-Shape Exponential Powertpshagennormloc, sigma, shape1, shape2Two-Piece-Shape SinhArcSinhtpshasasloc, sigma, shape1, shape2Double Two-PieceNameFunctionParametersDouble Two-Piece Student-tdtpstudentloc, sigma1, sigma2, shape1, shape2Double Two-Piece Exponential Powerdtpgennormloc, sigma1, sigma2, shape1, shape2Double Two-Piece SinhArcSinhdtpsinhasinhloc, sigma1, sigma2, shape1, shape2Main FeaturesWe provide the following functionality for all the supported distributions.FunctionMethodParametersProbability Density FunctionpdfxCumulative Distribution FunctioncdfxQuantile FunctionppfqRandom Sample Generationrandom_samplesizeQuick StartInstallWe recommend installtwopieceusingpipas follows.pip install twopieceTo illustrate usage two-piece scale distributions we will use
the two-piece Normal, and two-piece Student-t. The behaviour is analogous for the rest of
the supported distributions.1. Create a twopiece instanceFirst, we load the family (scale, shape and double) of two-piece distributions that we want to use.fromtwopiece.scaleimport*fromtwopiece.shapeimport*fromtwopiece.doubleimport*To create an instance we need to specify either 3, 4, or 5 parameters:For theTwo-Piece Normalwe require:loc: which is the location parametersigma1,sigma2: which are both scale parametersloc=0.0sigma1=1.0sigma2=1.0dist=tpnorm(loc=loc,sigma1=sigma1,sigma2=sigma2)For theTwo-Piece Student-twe require:loc: which is the location parametersigma1,sigma2: which are both scale parametersshape: which defines the degrees of freedom for the t-Student distributionloc=0.0sigma1=1.0sigma2=2.0shape=3.0dist=tpstudent(loc=loc,sigma1=sigma1,sigma2=sigma2,shape=shape)For theDouble Two-Piece Student-twe require:loc: which is the location parametersigma1, sigma2: which are both scale parametersshape1, shape2: which define the degrees of freedom for the t-Student distribution on each side of
the mode.loc=0.0sigma1=1.0sigma2=2.0shape1=3.0shape2=10.0dist=dtpstudent(loc=loc,sigma1=sigma1,sigma2=sigma2,shape1=shape1,shape2=shape2)Hereafter we assume that there is a twopiece instance calleddist.2. Evaluate and visualise the probability density function (pdf)We can evaluate the pdf on a single point or an array type objectdist.pdf(0)dist.pdf([0.0,0.25,0.5])To visualise the pdf usex=arange(-12,12,0.1)y=dist.pdf(x)plt.plot(x,y)plt.show()3. Evaluate the cumulative distribution function (cdf)We can evaluate the cdf on a single point or an array type objectdist.cdf(0)dist.cdf([0.0,0.25,0.5])To visualise the cdf usex=arange(-12,12,0.1)y=dist.cdf(x)plt.plot(x,y)plt.show()4. Evaluate the quantile function (ppf)We can evaluate the ppf on a single point or an array type object. Note that the ppf has support on [0,1].dist.ppf(0.95)dist.ppf([0.5,0.9,0.95])To visualise the ppf usex=arange(0.001,0.999,0.01)y=dist.ppf(x)plt.plot(x,y)plt.show()5. Generate a random sampleTo generate a random sample we require:size: which is simply the size of the samplesample=dist.random_sample(size=100)