Convolution for Worksheet Problem 1

Let X and Y be independent uniform variables on [0,1]. Find the density of W=X+Y.

f[x_]:=Piecewise[{{1,0≤ x≤ 1}}];
Manipulate[Show[
Plot[1,{x,0,1},PlotStyle->Directive[Red,Opacity[0.5],Thickness[0.02],CapForm[None]]],
Plot[1,{x,w-1,w},PlotStyle->Directive[Green,Opacity[0.5],Thickness[0.02],CapForm[None]]],
Plot[f[x]×f[w-x],{x,-1.5,2.5},Filling->Axis,PlotStyle->Directive[Blue],PlotPoints->400],
PlotRange->{{-1.5,2.5},{-0.1,1.2}},AxesLabel->{"x","(Subscript[f, X])(x), (Subscript[f, y])(w-x)"}],
{w,0,2}]

f[x_]:=Piecewise[{{1,0≤ x≤ 1}}];
Manipulate[GraphicsRow[{
Show[
Plot[1,{x,0,1},PlotStyle->Directive[Red,Opacity[0.5],Thickness[0.02],CapForm[None]]],
Plot[1,{x,w-1,w},PlotStyle->Directive[Green,Opacity[0.5],Thickness[0.02],CapForm[None]]],
Plot[f[x]×f[w-x],{x,-1.5,2.5},Filling->Axis,PlotStyle->Directive[Blue],PlotPoints->400],
PlotRange->{{-1.5,2.5},{-0.1,1.2}},AxesLabel->{"x","(Subscript[f, X])(x), (Subscript[f, y])(w-x)"}],
Plot[Piecewise[{{t,0≤t≤1},{2-t,1<t≤2}}],{t,-1,w},PlotRange->{{-1,2.5},{-0.1,1.2}},AxesLabel->{"w","(Subscript[f, W])(w)"}]
}],{w,0,2.5}]

Worksheet Problem 2

Here is the pdf of one Unif[0,1] random variable:

f1[x_]:=Piecewise[{{1,0≤ x≤ 1}}];

Here is the pdf of a sum of two Unif[0,1] random variables:

f2[x_]:=Piecewise[{{x,0<=x<=1},{2-x,1<x<=2}}]
Plot[f2[x],{x,-1,3}]

The convolution of the previous two density functions gives the density of the sum of three Unif[0,1] random variables.

f3[x_]:=Integrate[f1[t]f2[x-t],{t,-Infinity,Infinity}]

f3[x]

Here is a plot of the density of a sum of three Unif[0,1] random variables:

Plot[f3[x],{x,0,3}]

Check that the area under the density function is 1:

Integrate[f3[x],{x,0,3}]

We can also find the density of a sum of four Unif[0,1] random variables!

f4[x_]:=Integrate[f2[t]f2[x-t],{t,-Infinity,Infinity}]

f4[x]

Plot[f4[x],{x,-1,5}]

Convolution for the example in the video

Let X~Exp(3) and Y~Exp(2). Let W=X+Y. Find the density of W.

fx[x_]:=Piecewise[{{3Exp[-3x],0≤ x}}];
fy[y_]:=Piecewise[{{2Exp[-2y],0≤y}}];
Manipulate[Show[
Plot[fx[x],{x,-5,5},PlotStyle->Directive[Red,Opacity[0.5],Thickness[0.01],CapForm[None]],PlotRange->{0,3}],
Plot[fy[w-x],{x,-5,5},PlotStyle->Directive[Green,Opacity[0.5],Thickness[0.01],CapForm[None]],PlotRange->{0,4},PlotPoints->1000],
Plot[fx[x]×fy[w-x],{x,-5,5},Filling->Axis,PlotStyle->Directive[Blue],PlotPoints->400],
PlotRange->{{-3,3},{-0.1,3}},AxesLabel->{"x","(Subscript[f, X])(x), (Subscript[f, y])(w-x)"}],
{w,0,5}]

Manipulate[GraphicsRow[{
Show[
Plot[fx[x],{x,-5,5},PlotStyle->Directive[Red,Opacity[0.5],Thickness[0.01],CapForm[None]],PlotRange->{0,3}],
Plot[fy[w-x],{x,-5,5},PlotStyle->Directive[Green,Opacity[0.5],Thickness[0.01],CapForm[None]],PlotRange->{0,4},PlotPoints->1000],
Plot[fx[x]×fy[w-x],{x,-5,5},Filling->Axis,PlotStyle->Directive[Blue],PlotPoints->400],
PlotRange->{{-3,3},{-0.1,3}},AxesLabel->{"x","(Subscript[f, X])(x), (Subscript[f, y])(w-x)"}],
Plot[Piecewise[{{6Exp[-3t](Exp[t]-1),t>0}}],{t,-5,w},PlotRange->{{-5,5},{-0.1,1.5}},AxesLabel->{"w","(Subscript[f, W])(w)"}]
}],{w,0,5}]