# Creating a user defined PDF

I have a derived distribution which I want to play with, which is of the form $$\frac{1}{\sigma^{2}}2^{-2+\frac{x}{10}} 5^{-1+\frac{x}{10}} e^{-\frac{1}{\sigma^{2}} 2^{-1+\frac{x}{10}} 5^{\frac{x}{10}}}\ln(10)$$ This is a $$x \rightarrow 10^{x/20}$$ transformation of the Rayleigh distribution, derrived by: $$R(x, \sigma) = \frac{x}{\sigma^{2}} e^{-x^{2}/(2\sigma)^{2}}$$ and consider $$X = 20 \log_{10}(x)$$, so $$x = 10^{X/20}$$. Then $$R_X(X, \sigma) = \frac{10^{X/20}}{\sigma^{2}} e^{-(10^{X/20})^{2}/(2\sigma)^{2}} \frac{d}{dX}10^{X/20}$$ Plugging into mathematica 10^(x/20)/\[Sigma]^2 Exp[-(10^(x/20))^2/ (2 \[Sigma]^2)]D[10^(x/20),x] gives the top expression.

The function appears well behaved for values I am interested in, here I choose \[Sigma] = 0.00005 which when plotted gives

Plot[(2^(-2 + x/10) 5^(-1 + x/10)E^(-((2^(-1 + x/10) 5^(x/10))/\[Sigma]^2))Log[10])/\[Sigma]^2, {x, -180, -50}, PlotRange -> All]


I want to use this function as a PDF so I can do some analysis I have tried

CustomDistribution[\[Sigma]_] := ProbabilityDistribution[Evaluate[10^(x/20)/\[Sigma]^2 Exp[-(10^(x/20))^2/ (2 \[Sigma]^2)]D[10^(x/20),x]],{x, -Infinity, Infinity}]


Which returns

Function[\[FormalX], (
2^(-2 + \[FormalX]/10) 5^(-1 + \[FormalX]/10)
E^(-((2^(-1 + \[FormalX]/10) 5^(\[FormalX]/10))/\[Sigma]^2))
Log[10])/\[Sigma]^2]


But if I now try and plot this, with the same value for $$\sigma$$, as

Plot[PDF[CustomDistribution[\[Sigma]]][x],{x, -60, -160}]


I just get a flat line. Am I defining my PDF incorrectly? Is it possible to make a user defined PDF

• Did you actually run the plot I submitted? What you say is demonstrably not true. – Q.P. Aug 10 '19 at 16:26
• Plot[(2^(-2 + x/10) 5^(-1 + x/10)E^(-((2^(-1 + x/10) 5^(x/10))/\[Sigma]^2))Log[10])/\[Sigma]^2, {x, -180, -50}, PlotRange -> All] It is defined. Just run it. I will add some text how I derived the expression. – Q.P. Aug 10 '19 at 16:34
• @wolfies See above. I genuinely don't understand why you say the expression is not valid for $x<0$. You can even demonstrate the transformation validity by simulation. Generate some Rayleigh distributed data transform with $20\log_{10}x$ and then you will see exactly the distribution outlined in the top equation. – Q.P. Aug 10 '19 at 16:48
• Using $Y$ for the transformed variable (rather than X and x) would avoid notation ambiguities. – wolfies Aug 10 '19 at 16:59
• Granted, the notation was ambiguous. But perhaps next time actually check code before making an assertion. – Q.P. Aug 10 '19 at 17:11

## 1 Answer

Clear["Global*"]


When defining a custom distribution, the constraints on the parameters should be included as Assumptions and the Evaluate should evaluate the entire expression rather than just the argument to ProbabilityDistribution

CustomDistribution[σ_] :=
Evaluate@ProbabilityDistribution[
10^(x/20)/σ^2 Exp[-(10^(x/20))^2/(2 σ^2)] D[10^(x/20),
x], {x, -Infinity, Infinity}, Assumptions -> σ > 0]


The constraints are then available with DistributionParameterAssumptions.

assume = DistributionParameterAssumptions[
CustomDistribution[σ]]

(* σ > 0 *)


Verifying that the total probability for the distribution is valid

Assuming[assume,
Integrate[PDF[CustomDistribution[σ], x],
{x, -Infinity, Infinity}]]

(* 1 *)


The Mean is

μ[σ_] = Mean[CustomDistribution[σ]]

(* -((10 (EulerGamma + Log[1/(2 σ^2)]))/Log[10]) *)

μ[0.00005]

(* -85.5171 *)

With[{σ = 0.00005},
Plot[PDF[CustomDistribution[σ], x],
{x, -120, -60},
PlotRange -> All]]


EDIT: It is more straightforward to let Mathematica do all of the work by using TransformedDistribution.

dist[σ_] = TransformedDistribution[20 Log10[x],
x \[Distributed] RayleighDistribution[σ]];

PDF[dist[σ], x]

(* (2^(-2 + x/10) 5^(-1 + x/10) E^(-((2^(-1 + x/10) 5^(x/10))/σ^2))
Log[10])/σ^2 *)


The constraint on the parameter is then inherited from RayleighDistribution

DistributionParameterAssumptions[dist[σ]]

(* σ > 0 *)

• Wonderful as always. Thanks Bob. – Q.P. Aug 10 '19 at 19:21
• You can replace f[x_]:=Evaluate[...] with f[x_]=…, which seems a bit more direct in my opinion – Lukas Lang Aug 12 '19 at 13:42
• @LukasLang - I agree; however, I was trying to point out that the problem was improper placement of the Evaluate` – Bob Hanlon Aug 12 '19 at 13:45