Характерні функції можуть зробити обчислення, що включають суми та відмінності випадкових величин, дуже просто. Mathematica має безліч функцій для роботи зі статистичними розподілами, включаючи вбудований для перетворення розподілу в його характерну функцію.
Я хотів би проілюструвати це двома конкретними прикладами: (1) Припустимо, ви хотіли визначити результати прокатки колекції кісток з різною кількістю сторін, наприклад, розкачайте два шестигранні кістки плюс один восьмисторонній штамб (тобто , 2d6 + d8 )? Або (2) припустимо, ви хотіли знайти різницю двох рулонів з кістки (наприклад, d6-d6 )?
An easy way to do this would be to use the characteristic functions of the underlying discrete uniform distributions. If a random variable X has a probability mass function f, then its characteristic function φX(t) is just the discrete Fourier Transform of f, i.e., φX(t)=F{f}(t)=E[eitX]. A theorem tells us:
If the independent random variables X and Y have corresponding probability mass functions f and g, then the pmf h of the sum X+Y of these RVs is the convolution of their pmfs h(n)=(f∗g)(n)=∑∞m=−∞f(m)g(n−m).
We can use the convolution property of Fourier Transforms to restate this more simply in terms of characteristic functions:
The characteristic function φX+Y(t) of the sum of independent random variables X and Y equals the product of their characteristic functions φX(t)φY(t).
This Mathematica function will make the characteristic function for an s-sided die:
MakeCf[s_] :=
Module[{Cf},
Cf := CharacteristicFunction[DiscreteUniformDistribution[{1, s}],
t];
Cf]
The pmf of a distribution can be recovered from its characteristic function, because Fourier Transforms are invertible. Here is the Mathematica code to do it:
RecoverPmf[Cf_] :=
Module[{F},
F[y_] := SeriesCoefficient[Cf /. t -> -I*Log[x], {x, 0, y}];
F]
Continuing our example, let F be the pmf that results from 2d6+d8.
F := RecoverPmf[MakeCf[6]^2 MakeCf[8]]
There are 62⋅8=288 outcomes. The domain of support of F is S={3,…,20}. Three is the min because you're rolling three dice. And twenty is the max because 20=2⋅6+8. If you want to see the image of F, compute
In:= F /@ Range[3, 20]
Out= {1/288, 1/96, 1/48, 5/144, 5/96, 7/96, 13/144, 5/48, 1/9, 1/9, \
5/48, 13/144, 7/96, 5/96, 5/144, 1/48, 1/96, 1/288}
If you want to know the number of outcomes that sum to 10, compute
In:= 6^2 8 F[10]
Out= 30
If the independent random variables X and Y have corresponding probability mass functions f and g, then the pmf h of the difference X−Y of these RVs is the cross-correlation of their pmfs h(n)=(f⋆g)(n)=∑∞m=−∞f(m)g(n+m).
We can use the cross-correlation property of Fourier Transforms to restate this more simply in terms of characteristic functions:
The characteristic function φX−Y(t) of the difference of two independent random variables X,Y equals the product of the characteristic function φX(t) and φY(−t) (N.B. the negative sign in front of the variable t in the second characteristic function).
So, using Mathematica to find the pmf G of d6-d6:
G := RecoverPmf[MakeCf[6] (MakeCf[6] /. t -> -t)]
There are 62=36 outcomes. The domain of support of G is S={−5,…,5}. -5 is the min because −5=1−6. And 5 is the max because 6−1=5. If you want to see the image of G, compute
In:= G /@ Range[-5, 5]
Out= {1/36, 1/18, 1/12, 1/9, 5/36, 1/6, 5/36, 1/9, 1/12, 1/18, 1/36}