7

How is this recursive formula

$$ f_{n+1}(z) = \int_0^1 f_{n}(z-y)\,{\rm d}y $$

implemented in Mathematica? The base case is

$$ f_1(z) = \begin{cases} 1 & 0\leq z\leq 1 \\ 0 & \text{otherwise} \end{cases} $$

I have tried setting up a recursive integral relation, but my syntax must not be right.

martin
  • 8,678
  • 4
  • 23
  • 70

4 Answers4

8

Adapting once again Leonid's solution from here,

f[1, z_] := UnitBox[z - 1/2];
f[n_Integer, z_] := Module[{zl, t},
                           Set @@ Hold[f[n, zl_], 
                           Simplify[Convolve[UnitBox[t - 1/2], f[n - 1, t], t, zl]]];
                           f[n, z]];

f[4, z]

$\displaystyle\begin{cases} -\frac16(-4+z)^3&3\le z<4\\ \frac{z^3}{6}&0<z\le1\\ \frac23-2z+2z^2-\frac{z^3}{2}&1<z\le2\\ -\frac{22}{3}+10z-4z^2+\frac{z^3}{2}&2<z<3\\ 0&\mathtt{True} \end{cases}$

Plot[f[6, t], {t, -2, 2}]

plot of the function


Now, here's the surprise: there's an even shorter implementation for f[]!

f[n_Integer, z_] := BSplineBasis[n - 1, z/n]
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
6
f[1] = Integrate[PDF[UniformDistribution[{0, 1}], z - y], {y, 0, 1}] /. z -> y;
f[n_] := f[n] = Integrate[f[n - 1] /. y -> z - y, {y, 0, 1}] /. z -> y // Simplify;
f[3]

Mathematica graphics

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
2

@Winther's solution is particularly fast from here

adapted slightly:

pwf[z_] := Piecewise[{z[[#]], # - 1 <= y < #} & /@ Range@Length@z]
iidf[n_] := With[{nn = n}, ffunc = Table[If[i == 1, 1, 0], {i, 1, n}]; 
Do[temp = ffunc; temp[[1]] = Integrate[ffunc[[1]], {z, 0, z}];
Do[temp[[k]] = Integrate[ffunc[[k - 1]], {z, z - 1, k - 1}] + 
Integrate[ffunc[[k]], {z, k - 1, z}];, {k, 2, Floor[(i + 1)/2]}];
Do[temp[[k]] = temp[[i - k + 1]] /. z -> i - z;, 
{k, Floor[(i + 1)/2] + 1, i}]; ffunc = temp;, {i, 2, n}];
pwf@ExpandAll[ffunc] /. z -> y]

Plot[Evaluate[iidf@6], {y, 0, 6}, PlotPoints -> 400]

enter image description here

martin
  • 8,678
  • 4
  • 23
  • 70
2

Along with @J.M.'s superfast solution, and this nice little identity, where for $X_j \text{ iid},$ uniformly distributed on $[0,1],$

$$\dfrac{1}{n!} \left\langle n \atop k \right\rangle = P\left(\sum_{j=1}^{n}X_j\in[k,k+1]\right)$$

we can get eg eulplot[6, 2], eulplot[12, 5]:

enter image description here

enter image description here

eulerian[k_, n_] := 
CoefficientList[(1 - x)^(n + 1) PolyLog[-n, x]/x, x][[k + 1]]

iid[k_, n_] := eulerian[k, n]/n!

eulplot[n_, pt_] := With[{aa = Piecewise[SortBy[(BSplineBasis[n - 1, 
x/(n)] // PiecewiseExpand)[[1]], Last@# &]]}, 
Show[Plot[Evaluate[aa], {x, 0, n}, PlotPoints -> 1000], 
Plot[Evaluate[aa[[1, pt + 1]]], {x, pt, pt + 1}, Filling -> Axis, 
PlotRange -> {{0, Automatic}, {0, Automatic}}], Frame -> True, 
LabelStyle -> Black, PlotLabel -> StringJoin["A=", ToString[
TraditionalForm[HoldForm[CenterDot[(1/n!) , 
AngleBracket[Style[Overscript[Underscript["", pt], n],  
ScriptSizeMultipliers -> 1]]]] == iid[pt, n]]]]]]
martin
  • 8,678
  • 4
  • 23
  • 70