I have this function that works similar to a probability generating function

$$g(x):=\left(\frac{c}{D} x^{-1}+\frac{D-t-c}D+\frac{t-d}{D} x+\frac{d}D x^2\right)^M$$

with $g(1)=1$. This is not a generating function in the common sense but from $g$ we can define the following PMF

$$f(k):=\begin{cases}[x^k]g(x),& k>0\\\sum_{k=-M}^0 [x^k]g(x),& k=0\end{cases}$$

and it CDF in a similar manner. I want to use the built-in function ProbabilityDistribution (if this is possible) to define $f$ as a PDF for the discrete random variable $K$ that take values in $\{0,\ldots,2M\}$ and depend on parameters $D,t,c,d$ and $M$ where

$$D,M\in\Bbb N_{>0},\quad 0\le d,c<D\quad\text{and}\quad 1\le t<D$$

But in the Wolfram language documentation center there is no information about if ProbabilityDistribution can be used to define distributions that depends on parameters or how to do it.

Some help will be appreciated, thank you.

P.S.: the notation $[x^k]p$ means "the $k$-th coefficient of the series $p$".

  • 1
    $\begingroup$ Yes, ProbabilityDistribution can be used when you have parameters. Just make sure that the probabilities add up to 1—it doesn't check this. Also, if you have too many parameters, many of the simple symbolic calculations (like Mean) may fail. Consider also setting $Assumptions in that case. $\endgroup$
    – Szabolcs
    Mar 23, 2017 at 10:07
  • $\begingroup$ I guess the important question is: what do you want to do with this distribution? ProbabilityDistribution may or may not be the best way to deal with it. $\endgroup$
    – Szabolcs
    Mar 23, 2017 at 10:09
  • $\begingroup$ @Szabolcs well, I want to define it as a probability distribution to evaluate easily mean, variance and other parameters. But you right, probably is not needed at all, more like I wanted to see how to use this built-in function and if is possible to define these kind of probability distributions with various parameters. $\endgroup$
    – Masacroso
    Mar 23, 2017 at 10:12
  • $\begingroup$ In general, you can define distributions with parameters as usual. For example dist = ProbabilityDistribution[((-1 + x)/x) x^-k, {k, 0, Infinity, 1}]. Here x is a parameter. Mean[dist] and Variance[dist] work. You will notice that the latter is a ConditionalExpression, because these probabilities make sense only if x>1. It is these kinds of assumptions that are harder to control when using Mean/Variance than when you do the Sums directly. $\endgroup$
    – Szabolcs
    Mar 23, 2017 at 12:11
  • $\begingroup$ Unfortunately I cannot write an answer using the example from your question because I do not fully understand it (I don't know the meaning of the square brackets in $[x]$ and I do not see if $f(k)$ is normalized (which is necessary for ProbabilityDistribution) $\endgroup$
    – Szabolcs
    Mar 23, 2017 at 12:12

2 Answers 2


I think getting a symbolic result for general m will take a lot more work. But if a symbolic result for each value of m is satisfactory, here's one way to do it:

(* Set m *)
m = 3;

(* Generating function *)
g[x_] := ((c/bigD)/x + (bigD - t - c)/bigD + (t - d) x/bigD + (d/bigD) x^2)^m

(* pdf of k *)
f[k_] := If[k == 0, Sum[Coefficient[g[x], x, i], {i, -m, 0}], Coefficient[g[x], x, k]]

(* Make a probability distribution *)
distribution = ProbabilityDistribution[f[k], {k, 0, 2 m, 1},
  Assumptions -> {bigD ∈ Integers, 0 <= d < bigD, 0 <= c < bigD, 1 <= t < bigD}];

(* Check out Mean, Variance, and Expectation functions *)
meank = Simplify[Mean[distribution]]
(* (3 ((bigD-c) (bigD+c) d+(bigD-c)^2 t+c t^2))/bigD^3 *)
vark = Simplify[Variance[distribution]]
meank = Simplify[Expectation[k, k \[Distributed] distribution]]
vark = Simplify[Expectation[(k - meank)^2, k \[Distributed] distribution]]

(* Brute force *)
sumToOne = Simplify[Sum[f[k], {k, 0, 2 m}]]
meank = Simplify[Sum[k f[k], {k, 0, 2 m}]]
vark = Simplify[Sum[(k - meank)^2 f[k], {k, 0, 2 m}]]


From a comment by the OP there seems to be a difference of opinion as to whether $K$ goes from 0 to $M$ or from 0 to $2M$. I still argue that $K$ ranges from 0 to $2M$:

m = 3;
g[x_] := ((c/bigD)/x + (bigD - t - c)/bigD + (t - d) x/bigD + (d/bigD) x^2)^m
f[k_] := If[k == 0, Sum[Coefficient[g[x], x, i], {i, -m, 0}], Coefficient[g[x], x, k]]

Total[Table[f[k], {k, 0, m}]]
(* 1-(3 d^2)/bigD^2+(3 c d^2)/bigD^3-d^3/bigD^3+(6 d^2 t)/bigD^3-(3 d t^2)/bigD^3 *)

Total[Table[f[k], {k, 0, 2 m}]]
(* 1 *)

However, if 0 to $M$ is desired, then some additional scaling needs to be included to get the probabilities to sum to 1.

  • $\begingroup$ oh, thank you! When I tried to define the probability distribution I dont enclosed the assumptions in {}, I think this is the reason why it doesnt worked. The distribution simulate a dice game about the total value of throwing $M$ dice of $D$ sides where there are $t$ dice that have value $1$, $c$ dice that have value $-1$ and $d$ dice (from the $t$ dice) that have value 2 (the rest have value zero). A throw with negative values is interpreted as zero value, this is why I used the piecewise function. What is more interesting here is the CDF and the mean, variance and other statistics. $\endgroup$
    – Masacroso
    Mar 23, 2017 at 16:38
  • $\begingroup$ The problem with this approach is that I must define $m$ previously. There is a way to define a family of distributions based in $m$? By example, there is a way of call mean setting the parameter $m$ or any other number of parameters at the same time? $\endgroup$
    – Masacroso
    Mar 23, 2017 at 17:18
  • 1
    $\begingroup$ The option to leave m unspecified was precisely why I was recommending SeriesCoefficient[] instead of Coefficient[]. $\endgroup$ Mar 23, 2017 at 17:28
  • $\begingroup$ To get a result without having to specify m you'll need to get someone else to provide that answer (which follows @J.M. 's suggestion). So I've edited my answer (making an inconsequential change) so that you can retract your acceptance. I, too, would look forward to see how an answer for a unspecified m can be constructed. I have several generating functions for which such an approach would be great. $\endgroup$
    – JimB
    Mar 23, 2017 at 17:44
  • $\begingroup$ @JimBaldwin I checked that specifying the parameters as variables work, by example you can define distribution[m_]:=ProbabilityDistribution[f[k,m],etc...]. First time I tried it doesnt worked because I had previously assigned distributionto a value, so using ClearAllI free this word again and voilá! $\endgroup$
    – Masacroso
    Mar 23, 2017 at 18:02

With the help of @JimBaldwin I found this solution

    (*Our pseudo-generating function*)
g[x_, m_, c_, bigD_, t_, d_] := 
 ((c/bigD)/x + (bigD - t - c)/bigD + (t - d) x/bigD + (d/bigD) x^2)^m

    (*Probability mass function of k*)
f[k_, m_, c_, bigD_, t_, d_] :=
 If[k == 0,Sum[Coefficient[g[x, m, c, bigD, t, d], x, i], {i, -m, 0}],
  Coefficient[g[x, m, c, bigD, t, d], x, k]]

    (*We make a probability distribution*)
dist[m_, bigD_: 6, t_: 2, d_: 0, c_: 0] :=
 ProbabilityDistribution[f[k, m, c, bigD, t, d],{k, 0, 2 m, 1}, 
  Assumptions -> {bigD \[Element] Integers, 0 <= d < bigD, 0 <= c < bigD, 1 <= t < bigD, 
  m \[Element] Integers && m > 0}];

    (* Testing some statistics*)
{Mean[#], StandardDeviation[#], Table[Evaluate@CDF[#, k], {k, 0, 5}]} &@dist[5]

    (* Some plots *)
DiscretePlot[Evaluate@#1[dist[10], #2], {k, 0, 10}, ExtentSize -> 3/4] & @@@ 
 {{PDF, k}, {SurvivalFunction, k - 1}}

Because this probability function is modeling a dice game it could be a difference depending in how we can interpret the parameter $d$ what could imply that $K\in\{0,\ldots,M\}$ strictly instead that $K\in\{0,\ldots,2M\}$ when $d>0$.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.