loss_functions Module

Overview

The loss_functions module contains code for calculating loss functions.

For a random variable \(X\), the loss function, \(n(x)\), and the complementary loss function, \(\bar{n}(x)\), are defined as:

  • \(n(x) = E[(X-x)^+]\)

  • \(\bar{n}(x) = E[(X-x)^-]\),

where \(x^+ = \max\{x,0\}\) and \(x^- = |\min\{x,0\}|\). The second-order loss function, \(n^{(2)}(x)\), and the second-order complementary loss function, \(\bar{n}^{(2)}(x)\), are defined as:

  • \(n^{(2)}(x) = \frac{1}{2}E\left[\left([X-x]^+\right)^2\right]\)

  • \(\bar{n}^{(2)}(x) = \frac{1}{2}E\left[\left([X-x]^-\right)^2\right]\)

Note

The notation and references (equations, sections, examples, etc.) used below refer to Snyder and Shen, Fundamentals of Supply Chain Theory (FoSCT), 2nd edition (2019).

API Reference

standard_normal_loss(z)[source]

Return \(\mathscr{L}(z)\) and \(\bar{\mathscr{L}}(z)\), the standard normal loss and complementary loss functions.

Parameters

z (float) – Argument of loss function.

Returns

  • L (float) – Loss function. [\(\mathscr{L}(z)\)]

  • L_bar (float) – Complementary loss function. [\(\bar{\mathscr{L}}(z)\)]

Equations Used (equations (C.22) and (C.23)):

\[\mathscr{L}(z) = \phi(z) - z(1 - \Phi(z))\]
\[\bar{\mathscr{L}}(z) = z + \mathscr{L}(z)\]

Example:

>>> standard_normal_loss(1.3)
(0.04552796208651397, 1.345527962086514)
standard_normal_second_loss(z)[source]

Return \(\mathscr{L}^{(2)}(z)\) and \(\bar{\mathscr{L}}^{(2)}(z)\), the standard normal second-order loss and complementary loss functions.

Parameters

z (float) – Argument of loss function.

Returns

  • L2 (float) – Loss function. [\(\mathscr{L}^{(2)}(z)\)]

  • L2_bar (float) – Complementary loss function. [\(\bar{\mathscr{L}}^{(2)}(z)\)]

Equations Used (equations (C.27) and (C.28)):

\[\mathscr{L}^{(2)}(z) = \frac12\left[\left(z^2+1\right)(1-\Phi(z)) - z\phi(z)\right]\]
\[\bar{\mathscr{L}}^{(2)}(z) = \frac12(z^2 + 1) - \mathscr{L}^{(2)}(z)\]

Example:

>>> standard_normal_second_loss(1.3)
(0.01880706693657111, 1.326192933063429)
normal_loss(x, mean, sd)[source]

Return \(n(x)\) and \(\bar{n}(x)\), the normal loss function and complementary loss functions for a \(N(\mu,\sigma^2)\) distribution.

Parameters
  • x (float) – Argument of loss function.

  • mean (float) – Mean of normal distribution. [\(\mu\)]

  • sd (float) – Standard deviation of normal distribution. [\(\sigma\)]

Returns

  • n (float) – Loss function. [\(n(x)\)]

  • n_bar (float) – Complementary loss function. [\(\bar{n}(x)\)]

Equations Used (equations (C.31) and (C.32)):

\[ \begin{align}\begin{aligned}n(x) = \mathscr{L}(z) \sigma\\\bar{n}(x) = \bar{\mathscr{L}}(z) \sigma\end{aligned}\end{align} \]

where \(z = (x-\mu)/\sigma\) and \(\mathscr{L}(z)\) and \(\bar{\mathscr{L}}(z)\) are the standard normal loss and complementary loss functions.

Example:

>>> normal_loss(18.6, 15, 3)
(0.1683073521514889, 3.7683073521514903)
normal_second_loss(x, mean, sd)[source]

Return \(n^{(2)}(x)\) and \(\bar{n}^{(2)}(x)\), the second-order normal loss function and complementary second-order loss function for a \(N(\mu,\sigma^2)\) distribution.

Parameters
  • x (float) – Argument of loss function.

  • mean (float) – Mean of normal distribution. [\(\mu\)]

  • sd (float) – Standard deviation of normal distribution. [\(\sigma\)]

Returns

  • n2 (float) – Second-order loss function. [\(n^{(2)}(x)\)]

  • n2_bar (float) – Complementary second-order loss function. [\(\bar{n}^{(n)}(x)\)]

Equations Used (equations (C.33) and (C.34)):

\[ \begin{align}\begin{aligned}n^{(2)}(x) = \mathscr{L}^{(2)}(z) \sigma^2\\\bar{n}^{(2)}(x) = \bar{\mathscr{L}}^{(2)}(z) \sigma^2\end{aligned}\end{align} \]

where \(z = (x-\mu)/\sigma\) and \(\mathscr{L}^{(2)}(z)\) and \(\bar{\mathscr{L}}^{(2)}(z)\) are the standard normal second-order loss and complementary loss functions.

Example:

>>> normal_second_loss(18.6, 15, 3)
(0.21486028212500707, 10.765139717874998)
lognormal_loss(x, mu, sigma)[source]

Return lognormal loss and complementary loss functions for \(\text{lognormal}(\mu,\sigma)\) distribution.

Parameters
  • x (float) – Argument of loss function.

  • mu (float) – Mean of distribution of [\(\ln X\)].

  • sigma (float) – Standard deviation of distribution of [\(\ln X\)].

Returns

  • n (float) – Loss function. [\(n(x)\)]

  • n_bar (float) – Complementary loss function. [\(\bar{n}(x)\)]

Equations Used (equations (4.102) and (C.14)):

\[n(x) = e^{\mu+\sigma^2/2} \Phi((\mu+\sigma^2-\ln x)/\sigma) - x(1 - \Phi((\ln x - \mu)/\sigma))\]
\[\bar{n}(x) = x - E[X] + n(x)\]

Example:

>>> lognormal_loss(10, 2, 0.3)
(0.28364973888106326, 2.5544912009711833)
exponential_loss(x, mu)[source]

Return exponential loss and complementary loss functions for \(\text{exp}(\mu)\) distribution.

Parameters
  • x (float) – Argument of loss function.

  • mu (float) – Rate of exponential distribution.

Returns

  • n (float) – Loss function. [\(n(x)\)]

  • n_bar (float) – Complementary loss function. [\(\bar{n}(x)\)]

Raises

ValueError – If x < 0.

Equations Used (Zipkin (2000), p. 457 and (C.14)):

\[n(x) = \frac{e^{-\mu x}}{\mu}\]
\[\bar{n}(x) = x - E[X] + n(x)\]

References

    1. Zipkin, Foundations of Inventory Management, Irwin/McGraw-Hill (2000).

Example:

>>> exponential_loss(1, 0.2)
(4.0936537653899085, 0.09365376538990855)
exponential_second_loss(x, mu)[source]

Return \(n^{(2)}(x)\) and \(\bar{n}^{(2)}(x)\), the second-order exponential loss function and complementary second-order loss function for an \(\text{exp}(\mu)\) distribution.

Parameters
  • x (float) – Argument of loss function.

  • mu (float) – Rate of exponential distribution.

Returns

  • n2 (float) – Second-order loss function. [\(n^{(2)}(x)\)]

  • n2_bar (float) – Complementary second-order loss function. [\(\bar{n}^{(n)}(x)\)]

Raises

ValueError – If x < 0.

Equations Used (Zipkin (2000), p. 457 and (C.19)):

\[n^{(2)}(x) = \frac{e^{-\mu x}}{\mu^2}\]
\[\bar{n}^{(2)}(x) = \frac12\left((x-E[X])^2 + \text{Var}[X]\right) - n^{(2)}(x)\]

References

    1. Zipkin, Foundations of Inventory Management, Irwin/McGraw-Hill (2000).

Example:

>>> exponential_second_loss(1, 0.2)
(20.46826882694954, 0.031731173050459915)
gamma_loss(x, a, b)[source]

Return gamma loss and complementary loss functions for a \(\text{Gamma}(a,b)\) distribution with shape parameter \(a\) and scale parameter \(b\), i.e., a distribution with pdf

\[f(x) = \frac{x^{a-1}e^{-\frac{x}{b}}}{\Gamma(a)b^a},\]

where \(\Gamma(\cdot)\) is the gamma function.

Parameters
  • x (float) – Argument of loss function.

  • a (float) – Shape parameter of gamma distribution.

  • b (float) – Scale parameter of gamma distribution.

Returns

  • n (float) – Loss function. [\(n(x)\)]

  • n_bar (float) – Complementary loss function. [\(\bar{n}(x)\)]

Raises

ValueError – If x <= 0.

Equations Used (Zipkin (2000), p. 457 and (C.14)):

\[n(x) = \left[\left(a - \frac{x}{b}\right)(1-F(x)) + xf(x)\right]b,\]

where \(f(x)\) and \(F(x)\) are the gamma pdf and cdf, respectively.

\[\bar{n}(x) = x - E[X] + n(x)\]

References

    1. Zipkin, Foundations of Inventory Management, Irwin/McGraw-Hill (2000).

Example:

>>> gamma_loss(4, 2, 3)
(2.635971381157268, 0.635971381157268)
gamma_second_loss(x, a, b)[source]

Return \(n^{(2)}(x)\) and \(\bar{n}^{(2)}(x)\), the second-order loss and complementary loss functions for a \(\text{Gamma}(a,b)\) distribution with shape parameter \(a\) and scale parameter \(b\), i.e., a distribution with pdf

\[f(x) = \frac{x^{a-1}e^{-\frac{x}{b}}}{\Gamma(a)b^a},\]

where \(\Gamma(\cdot)\) is the gamma function.

Parameters
  • x (float) – Argument of loss function.

  • a (float) – Shape parameter of gamma distribution.

  • b (float) – Scale parameter of gamma distribution.

Returns

  • n2 (float) – Second-order loss function. [\(n^{(2)}(x)\)]

  • n2_bar (float) – Complementary second-order loss function. [\(\bar{n}^{(n)}(x)\)]

Raises

ValueError – If x < 0.

Equations Used (Zipkin (2000), p. 457 and (C.19)):

\[n^{(2)}(x) = \frac{1}{2}\left[\left[\left(a-\frac{x}{b}\right)^2 + a\right](1-F(x)) + \left(a - \frac{x}{b} + 1\right)xf(x)\right]b^2,\]

where \(f(x)\) and \(F(x)\) are the gamma pdf and cdf, respectively.

\[\bar{n}^{(2)}(x) = \frac12\left((x-E[X])^2 + \text{Var}[X]\right) - n^{(2)}(x)\]

References

    1. Zipkin, Foundations of Inventory Management, Irwin/McGraw-Hill (2000).

Example:

>>> gamma_second_loss(4, 2, 3)
(10.280288386513346, 0.7197116134866537)
uniform_loss(x, a, b)[source]

Return uniform loss and complementary loss functions for a continuous \(U[a,b]\) distribution.

Parameters
  • x (float) – Argument of loss function.

  • a (float) – Lower bound of uniform distribution.

  • b (float) – Upper bound of uniform distribution.

Returns

  • n (float) – Loss function. [\(n(x)\)]

  • n_bar (float) – Complementary loss function. [\(\bar{n}(x)\)]

Raises

ValueError – If x < a or > b.

Equations Used:

\[ \begin{align}\begin{aligned}n(x) = \frac{(b-x)^2}{2(b-a)}\\\bar{n}(x) = \frac{(x-a)^2}{2(b-a)}\end{aligned}\end{align} \]

Example:

>>> uniform_loss(4, 2, 8)
(1.3333333333333333, 0.3333333333333333)
uniform_second_loss(x, a, b)[source]

Return \(n^{(2)}(x)\) and \(\bar{n}^{(2)}(x)\), the second-order loss and complementary loss functions for a \(U[a,b]\) distribution.

Parameters
  • x (float) – Argument of loss function.

  • a (float) – Lower bound of uniform distribution.

  • b (float) – Upper bound of uniform distribution.

Returns

  • n2 (float) – Second-order loss function. [\(n^{(2)}(x)\)]

  • n2_bar (float) – Complementary second-order loss function. [\(\bar{n}^{(n)}(x)\)]

Raises

ValueError – If x < a or > b.

Equations Used:

\[ \begin{align}\begin{aligned}n^{(2)}(x) = \frac{(b-x)^3}{6(b-a)}\\\bar{n}^{(2)}(x) = \frac{(x-a)^3}{6(b-a)}\end{aligned}\end{align} \]

Example:

>>> uniform_second_loss(4, 2, 8)
(1.7777777777777777, 0.2222222222222222)
continuous_loss(x, distrib)[source]

Return loss and complementary loss functions for an arbitrary continuous distribution, using numerical integration.

Parameters
  • x (float) – Argument of loss function.

  • distrib (rv_continuous) – Desired distribution.

Returns

  • n (float) – Loss function. [\(n(x)\)]

  • n_bar (float) – Complementary loss function. [\(\bar{n}(x)\)]

Equations Used (equations (C.12) and (C.13)):

\[ \begin{align}\begin{aligned}n(x) = \int_x^\infty \bar{F}(y)dy\\\bar{n}(x) = \int_{-\infty}^x F(y)dy\end{aligned}\end{align} \]

Example:

Calculate loss function for \(\exp(10)\) distribution, by declaring a custom rv_continuous distribution:

>>> from scipy import stats
>>> import math
>>> class my_exp(stats.rv_continuous):
...     def _pdf(self, x):
...         if x >= 0:
...             return 10 * math.exp(-10 * x)
...         else:
...             return 0
>>> my_dist = my_exp()
>>> continuous_loss(0.2, my_dist)
(0.013533528323661264, 0.10522318598177341)

Or by using a “frozen” built-in exponential distribution:

>>> from scipy.stats import expon
>>> my_dist = expon(scale=1/10)
>>> continuous_loss(0.2, my_dist)
(0.013533528103402742, 0.11353352830366131)

(The two methods give slightly different results due to differences in the ways the two rv_continuous objects generate the distribution.)

continuous_second_loss(x, distrib)[source]

Return second-order loss and complementary loss functions for an arbitrary continuous distribution, using numerical integration.

Parameters
  • x (float) – Argument of loss function.

  • distrib (rv_continuous) – Desired distribution.

Returns

  • n2 (float) – Second-order loss function. [\(n(x)\)]

  • n2_bar (float) – Complementary second-order loss function. [\(\bar{n}(x)\)]

Equations Used (equations (C.17) and (C.18)):

\[n^{(2)}(x) = \frac12\int_x^\infty (y-x)^2 f(y)dy\]
\[n^{(2)}(x) = \frac12\int_0^x (x-y)^2 f(y)dy\]

Example:

Calculate second-order loss functions for \(\exp(10)\) distribution, by declaring a custom rv_continuous distribution:

>>> from scipy import stats
>>> import math
>>> class my_exp(stats.rv_continuous):
...     def _pdf(self, x):
...         if x >= 0:
...             return 10 * math.exp(-10 * x)
...         else:
...             return 0
>>> my_dist = my_exp()
>>> continuous_second_loss(0.2, my_dist)
(0.0013533528323661267, 0.007824431159359786)

Or by using a “frozen” built-in exponential distribution:

>>> from scipy.stats import expon
>>> my_dist = expon(scale=1/10)
>>> continuous_second_loss(0.2, my_dist)
(0.001353352589297054, 0.008646647165633875)

(The two methods give slightly different results due to differences in the ways the two rv_continuous objects generate the distribution.)

poisson_loss(x, mean)[source]

Return Poisson loss and complementary loss functions for \(\text{Pois}\) (mean) distribution.

Parameters
  • x (float) – Argument of loss function.

  • mean (float) – Mean of Poisson distribution.

Returns

  • n (int) – Loss function. [\(n(x)\)]

  • n_bar (float) – Complementary loss function. [\(\bar{n}(x)\)]

Raises

ValueError – If x is not an integer.

Equations Used (equations (C.41) and (C.42)):

\[n(x) = -(x - \mu)(1-F(x)) + \mu f(x)\]
\[\bar{n}(x) = (x - \mu) F(x) + \mu f(x)\]

Example:

>>> poisson_loss(18, 15)
(0.5176095282584724, 3.5176095282584723)
poisson_second_loss(x, mean)[source]

Return \(n^{(2)}(x)\) and \(\bar{n}^{(2)}(x)\), the second-order Poisson loss function and complementary second-order loss function for a \(\text{Pois}\) (mean) distribution.

Parameters
  • x (float) – Argument of loss function.

  • mean (float) – Mean of Poisson distribution. [\(\mu\)]

Returns

  • n2 (float) – Second-order loss function. [\(n^{(2)}(x)\)]

  • n2_bar (float) – Complementary second-order loss function. [\(\bar{n}^{(n)}(x)\)]

Raises

ValueError – If x is not an integer.

Equations Used (equations (C.41) and (C.42)):

\[n^{(2)}(x) = \frac12 \left[\left((x-\mu)^2 + x\right)(1-F(x)) - \mu(x-\mu)f(x)\right]\]
\[\bar{n}^{(2)}(x) = \frac12 \left[\left((x-\mu)^2 + x\right)F(x) + \mu(x-\mu)f(x)\right]\]

Example:

>>> poisson_second_loss(18, 15)
(0.848340302917789, 12.651659697082211)
geometric_loss(x, p)[source]

Return geometric loss and complementary loss functions for \(\text{Geom}\) (p) distribution. Uses the “number of trials” version of the geometric distribution, i.e., the pmf is

\[f(x) = (1-p)^{x-1}p\]

(This is the same version of the distribution used by scipy.)

Parameters
  • x (float) – Argument of loss function.

  • p (float) – Success probability for geometric distribution.

Returns

  • n (int) – Loss function. [\(n(x)\)]

  • n_bar (float) – Complementary loss function. [\(\bar{n}(x)\)]

Raises

ValueError – If x is not an integer.

Equations Used (Zipkin (2000), Section C.2.3.5, and (C.14)):

\[n(x) = \left(\frac{1-p}{p}\right)(1-p)^{x-1}\]
\[\bar{n}(x) = x - E[X] + n(x)\]

(Note that Zipkin (2000) uses the “number of failures” version of the geometric distribution and uses \(p\) to refer to the failure probability rather than the success probability. The notation has been adjusted to account for the version we use here.)

References

    1. Zipkin, Foundations of Inventory Management, Irwin/McGraw-Hill (2000).

Example:

>>> geometric_loss(7, 0.2)
(1.0485760000000004, 3.0485760000000006)
geometric_second_loss(x, p)[source]

Return \(n^{(2)}(x)\) and \(\bar{n}^{(2)}(x)\), the second-order geometric loss function and complementary second-order loss function for a \(\text{Geom}\) (p) distribution. Uses the “number of trials” version of the geometric distribution, i.e., the pmf is

\[f(x) = (1-p)^{x-1}p\]

(This is the same version of the distribution used by scipy.)

Parameters
  • x (float) – Argument of loss function.

  • p (float) – Success probability for geometric distribution.

Returns

  • n2 (float) – Second-order loss function. [\(n^{(2)}(x)\)]

  • n2_bar (float) – Complementary second-order loss function. [\(\bar{n}^{(n)}(x)\)]

Raises

ValueError – If x is not an integer.

Equations Used (Zipkin (2000), Section C.2.3.5, and (C.40)):

\[n^{(2)}(x) = \left(\frac{1-p}{p}\right)^2 (1-p)^{x-1}\]
\[\bar{n}^{(2)}(x) = \frac12\left((x-E[X])^2 + (x-E[X]) + \text{Var}[X]\right) - n^{(2)}(x)\]

(Note that Zipkin (2000) uses the “number of failures” version of the geometric distribution and uses \(p\) to refer to the failure probability rather than the success probability. The notation has been adjusted to account for the version we use here.)

References

    1. Zipkin, Foundations of Inventory Management, Irwin/McGraw-Hill (2000).

Example:

>>> geometric_second_loss(7, 0.2)
(4.194304000000002, 8.805695999999998)
negative_binomial_loss(x, r=None, p=None, mean=None, sd=None)[source]

Return negative binomial (NB) loss and complementary loss functions for NB distribution with shape parameters \(r\) and \(p\). The pmf of this distribution is given by

\[f(x) = {x+r-1 \choose r-1}p^r(1-p)^x\]

(See https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.nbinom.html.)

If mean and sd are provided instead of r and p, the function calculates \(r\) and \(p\) from mean (\(\mu\)) and sd (\(\sigma\)) using

\[r = \frac{\mu^2}{\sigma^2 - \mu}\]
\[p = 1 - \frac{\sigma^2 - \mu}{\sigma^2}\]

Assumes mean < sd**2; an exception is raised if not.

Parameters
  • x (int) – Argument of loss function.

  • r (int, optional) – Shape parameter of NB distribution representing number of successes until Bernoulli trials stop.

  • p (float, optional) – Shape parameter of NB distribution representing success probability for one Bernoulli trial.

  • mean (float, optional) – Mean of NB distribution. Ignored if r and p are both provided, required otherwise.

  • sd (float, optional) – Standard deviation of NB distribution. Ignored if r and p are both provided, required otherwise.

Returns

  • n (float) – Loss function. [\(n(x)\)]

  • n_bar (float) – Complementary loss function. [\(\bar{n}(x)\)]

Raises
  • ValueError – If x or r is not an integer.

  • ValueError – If r and p are not both provided and mean and sd are also not both provided.

  • ValueError – If mean is not less than sd ** 2.

Equations Used (Zipkin (2000), Section C.2.3.6, and equation (C.14)):

\[n(x) = -(x - r*\beta)(1-F(x)) + (x + r) * \beta * f(x),\]

where \(\beta = (1-p)/p\).

\[\bar{n}(x) = x - E[X] + n(x)\]

(Note that Zipkin (2000) uses a different version of the negative-binomial distribution. The notation has been adjusted to account for the version we use here.)

References

    1. Zipkin, Foundations of Inventory Management, Irwin/McGraw-Hill (2000).

Example:

>>> negative_binomial_loss(14, 4, 0.2)
(4.447304632028364, 2.447304632028364)
>>> negative_binomial_loss(14, mean=23, sd=8)
(9.326459980156931, 0.32645998015693145)
negative_binomial_second_loss(x, r=None, p=None, mean=None, sd=None)[source]

Return \(n^{(2)}(x)\) and \(\bar{n}^{(2)}(x)\), the second-order exponential loss function and complementary second-order loss function for a negative binomial (NB) distribution with shape parameters \(r\) and \(p\). The pmf of this distribution is given by

\[f(x) = {x+r-1 \choose r-1}p^r(1-p)^x\]

(See https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.nbinom.html.)

If mean and sd are provided instead of r and p, the function calculates \(r\) and \(p\) from mean (\(\mu\)) and sd (\(\sigma\)) using

\[r = \frac{\mu^2}{\sigma^2 - \mu}\]
\[p = 1 - \frac{\sigma^2 - \mu}{\sigma^2}\]

Assumes mean < sd**2; an exception is raised if not.

Parameters
  • x (int) – Argument of loss function.

  • r (int, optional) – Shape parameter of NB distribution representing number of successes until Bernoulli trials stop.

  • p (float, optional) – Shape parameter of NB distribution representing success probability for one Bernoulli trial.

  • mean (float, optional) – Mean of NB distribution. Ignored if r and p are both provided, required otherwise.

  • sd (float, optional) – Standard deviation of NB distribution. Ignored if r and p are both provided, required otherwise.

Returns

  • n2 (float) – Second-order loss function. [\(n^{(2)}(x)\)]

  • n2_bar (float) – Complementary second-order loss function. [\(\bar{n}^{(n)}(x)\)]

Raises
  • ValueError – If x or r is not an integer.

  • ValueError – If r and p are not both provided and mean and sd are also not both provided.

  • ValueError – If mean is not less than sd ** 2.

Equations Used (Zipkin (2000), Section C.2.3.6, and (C.40)):

\[n^{(2)}(x) = \frac12\left[\left[r(r+1)\beta^2 - 2r\beta x + x(x+1)\right](1-F(x)) + \left[(r+1)\beta - x\right](x+r)\beta f(x)\right],\]

where \(\beta = (1-p)/p\).

\[\bar{n}^{(2)}(x) = \frac12\left((x-E[X])^2 + (x-E[X]) + \text{Var}[X]\right) - n^{(2)}(x)\]

(Note that Zipkin (2000) uses a different version of the negative-binomial distribution. The notation has been adjusted to account for the version we use here.)

References

    1. Zipkin, Foundations of Inventory Management, Irwin/McGraw-Hill (2000).

Example:

>>> negative_binomial_second_loss(14, 4, 0.2)
(30.877804945158942, 10.122195054841043)
>>> negative_binomial_second_loss(14, mean=23, sd=8)
(67.10108087745232, 0.8989191225476816)
discrete_loss(x, distrib=None, pmf=None)[source]

Return loss and complementary loss function for an arbitrary discrete distribution.

Must provide either rv_discrete distribution (in distrib) or demand pmf (in pmf, as a dict).

Assumes \(F(x) = 0\) for \(x < 0\) (where \(F(\cdot)\) is the cdf).

Parameters
  • x (int) – Argument of loss function.

  • distrib (rv_discrete, optional) – Desired distribution.

  • pmf (dict, optional) – pmf, as a dict in which keys are the support of the distribution and values are their probabilities. Ignored if distrib is not None.

Returns

  • n (float) – Loss function. [\(n(x)\)]

  • n_bar (float) – Complementary loss function. [\(\bar{n}(x)\)]

Raises
  • ValueError – If x is not an integer.

  • ValueError – If distrib and pmf are both None.

Equations Used (equations (C.36) and (C.37)):

\[ \begin{align}\begin{aligned}n(x) = \sum_{y=x}^\infty (y-x)f(y) = \sum_{y=x}^\infty \bar{F}(y)dy\\\bar{n}(x) = \sum_{y=-\infty}^x (x-y)f(y) = \sum_{-\infty}^{x-1} F(y)dy\end{aligned}\end{align} \]

Example:

Calculate loss function for \(\text{geom}(0.2)\) distribution, by declaring a custom rv_discrete distribution:

>>> from scipy import stats
>>> class my_geom(stats.rv_discrete):
...     def _pmf(self, x):
...         return np.where(x >= 1, ((1 - 0.2) ** (x - 1)) * 0.2, 0)
>>> my_dist = my_geom()
>>> discrete_loss(4, my_dist)
(2.0479999999999743, 1.048)

Or by using a “frozen” built-in exponential distribution:

>>> from scipy.stats import geom
>>> my_dist = geom(0.2)
>>> discrete_loss(4, my_dist)
(2.048, 1.048)
discrete_second_loss(x, distrib=None, pmf=None)[source]

Return second-order loss and complementary loss function for an arbitrary discrete distribution.

Must provide either rv_discrete distribution (in distrib) or demand pmf (in pmf, as a dict).

Assumes the random variable cannot take negative values; i.e., \(F(x) = 0\) for \(x < 0\) (where \(F(\cdot)\) is the cdf).

Parameters
  • x (int) – Argument of loss function.

  • distrib (rv_discrete, optional) – Desired distribution.

  • pmf (dict, optional) – pmf, as a dict in which keys are the support of the distribution and values are their probabilities. Ignored if distrib is not None.

Returns

  • n2 (float) – Second-order loss function. [\(n(x)\)]

  • n2_bar (float) – Complementary second-order loss function. [\(\bar{n}(x)\)]

Raises
  • ValueError – If x is not an integer.

  • ValueError – If distrib and pmf are both None.

Equations Used (equations (C.38)-(C.40)):

\[ \begin{align}\begin{aligned}n^{(2)}(x) = \frac12\sum_{y=x}^\infty (y-x)(y-x-1)f(y) = \sum_{y=x}^\infty (y-x)(1-F(y))\\\bar{n}^{(2)}(x) = \frac12\sum_{y=0}^x (x-y)(x+1-y)f(y) = \sum_{y=0}^x (x-y)F(y)\end{aligned}\end{align} \]
\[\bar{n}^{(2)}(x) = \frac12\left(\left(x - E[X]\right)^2 + (x - E[X]) + \text{Var}[X]\right) - n^{(2)}(x)\]

Example:

Calculate loss function for \(\text{geom}(0.2)\) distribution, by declaring a custom rv_discrete distribution:

>>> from scipy import stats
>>> class my_geom(stats.rv_discrete):
...     def _pmf(self, x):
...         return np.where(x >= 1, ((1 - 0.2) ** (x - 1)) * 0.2, 0)
>>> my_dist = my_geom()
>>> discrete_loss(4, my_dist)
(2.0479999999999743, 1.048)

Or by using a “frozen” built-in exponential distribution:

>>> from scipy.stats import geom
>>> my_dist = geom(0.2)
>>> discrete_loss(4, my_dist)
(2.048, 1.048)