Homeworks

Play a simple melody using Play, Sound, the packages Audio and Music.

Study the options of Speak.

Send me details of your project work, problems and partial solutions.

Probability theory

Distributions

What do we have? How many do you among these?

?*Distribution

ExponentialDistribution[λ]

This is just a symbol, one nees PDF and CDF to do calculations.

PDF[ExponentialDistribution[λ],x]

CDF[ExponentialDistribution[λ],x]

PDF[BinomialDistribution[n,p],k]

DiscretePlot[%/.{n->94,p->0.02},{k,0,12}]

Characteristics of distributions (not of samples)

Multiple functions, a single argument

Through[{ff,gg,hh}[x]]

Through[{Mean,Variance,Skewness,Kurtosis}[ExponentialDistribution[λ]]]

Through[{Mean,Variance,Skewness,Kurtosis}[PoissonDistribution[λ]]]

Skewness, def.

CentralMoment[PoissonDistribution[λ],3]/((CentralMoment[PoissonDistribution[λ],2])^(3/2))

Kurtosis, def.

CentralMoment[PoissonDistribution[λ],4]/((CentralMoment[PoissonDistribution[λ],2])^2)//Simplify

Through[{Mean,Variance,Skewness,Kurtosis}[NormalDistribution[μ,σ]]]

The form of the Poisson-distribution is close the normal one.

CentralMoment[ExponentialDistribution[λ],2]

Esc dist Esc: \[Distributed]

Expectation[(X^(1/2)),X\[Distributed]ExponentialDistribution[λ]]

Expectation[(((x-1))^2)+(E^(-x)),x\[Distributed]NormalDistribution[]]

For independent random variables:

Expectation[X+Y,{X\[Distributed]NormalDistribution[μ,1],Y\[Distributed]NormalDistribution[ν,1]}]

Expectation[X Y,{X\[Distributed]NormalDistribution[μ,1],Y\[Distributed]NormalDistribution[ν,1]}]

Expectation[(X^(1/2))+Log[Y],{X\[Distributed]ExponentialDistribution[λ],Y\[Distributed]NormalDistribution[0,1]}]

N[%/.{λ->π}]

Expectation[(X^(1/2))+Log[Abs[Y]],{X\[Distributed]ExponentialDistribution[λ],Y\[Distributed]NormalDistribution[0,1]}]

N[%/.{λ->π}]

N@EulerGamma

Conditional expectation

Esc cond Esc :\[Conditioned]

Expectation[x+(y^2)-(3/10)\[Conditioned](1/2<x<3/4),{x,y}\[Distributed]DirichletDistribution[{1,3,4}]]

Multivariate β-distribution

PDF[DirichletDistribution[{1,3,4}],{x,y,z}]

Is a given function a probability density function?

Here: PDF, CDF

ClearAll[f,x,a,b,A];
f[x_,a_,b_,A_]:=A/(1+a(((x-b))^2))

Integrate[f[x,a,b,A],{x,-∞,+∞}]

Integrate[f[x,a,b,A],{x,-∞,+∞},Assumptions->{{a,b,A}∈Reals,a>0}]

Assuming[{{a,b,A}∈Reals,a>0},Integrate[f[x,a,b,A],{x,-∞,+∞}]]

Integrate[f[x,a,b,A],{x,-∞,+∞},Assumptions->{{a,b,A}∈Reals,A>0,a>0}]

Possible domains here (and in Solve and elsewhere)?

Reals, Integers, Complexes, Algebraics

ClearAll[a,b,A];
First@Solve[Integrate[f[x,a,b,A],{x,-∞,+∞},Assumptions->{{a,b,A}∈Reals,a>0}]==1,A]

Introducing a new distribution

UpValues, DownValues

g is the protagonist

g/:f[g[x_]]:=h[x]

UpValues[g]

??f

f[g[5]]

UpValues[h]={h[x_]*h[y_]:>htimes[x,y]};

h[a] h[b]

:>

UpSet[lhs,rhs]

Level 1 in lhs

f[g[x]]=...

f is the protagonist

Clear[f,g];f[g[x]]^=h[(x^2)]

f[g[I]]

hh[kk[x_]]:=3x

hh[kk[t]]

hhh[Sin[x_]]:=4x

hhh[Sin[t]]

Sin[jjj[x_]]:=44x

Unprotect[Sin]

Protect[Sin]

Attributes[Log]

Sin[jjj[x_]]^=44(x^2)

Sin[jjj[u]]

Sin[jjj[I]]

?jjj

??Sin

Unprotect[Sin]

g is the protagonist

square/:area[square]=a^2;
area[rectangle]^=a*b;

This has nothing to do with Exp

Exp[g[x_]]^:=expg[x]

??Exp

Exp[g[5]]

??g

g/:g[x_]+g[y_]:=gplus[x,y]

?g

g[5]+g[7]

If we used the usual deefinition (DownValues), then all the expressions with the head Plus were to be chacked about its argument.

quat[x_]+quat[y_]^:=quat[x+y]

quat[a]+quat[b]+quat[c]

Extend the scope of Plus to objects with the head g.

Multiple methiods to define UpValues: UpSet, ^=, Upvalues, /:

Applications

Messages

Clear[GeneralizedExtremeValueDistribution,μ,σ,ξ,x];

GeneralizedExtremeValueDistribution::usage="GeneralizedExtremeValueDistribution[μ,σ,ξ] is the generalized extreme value distribution (GEV) with location parameter μ, scale parameter σ, and shape parameter ξ.";

GeneralizedExtremeValueDistribution/:CDF[GeneralizedExtremeValueDistribution[μ_,σ_,ξ_],x_]:=UnitStep[x-μ+(σ/ξ)]Exp[-(((1+((ξ(x-μ) )/σ)))^(-(1/ξ)))];

GeneralizedExtremeValueDistribution/:PDF[GeneralizedExtremeValueDistribution[μ_,σ_,ξ_],x_]=Simplify[D[CDF[GeneralizedExtremeValueDistribution[μ,σ,ξ],x],x]];

GeneralizedExtremeValueDistribution/:Mean[GeneralizedExtremeValueDistribution[μ_,σ_,0]]=μ+σ EulerGamma;

EulerGamma//N

?GeneralizedExtremeValueDistribution

Mean[GeneralizedExtremeValueDistribution[3,1,0]]

Kurtosis[GeneralizedExtremeValueDistribution[3,1,0]]

However

Expectation[X,X\[Distributed]GeneralizedExtremeValueDistribution[3,1,0]]

Pseudorandom numbers

RandomReal, RandomInteger, RandomChoice, SeedRandom[n,Method->""]

RandomReal[NormalDistribution[],4]

SeedRandom[11];RandomInteger[PoissonDistribution[4],{2,2}]

Monte Carlo methods

RandomReal[{0,1},{2,3}]//MatrixForm

SeedRandom[]

pi[n_]:=(SeedRandom[100];N[4Count[Table[RandomReal[{0,1},2,WorkingPrecision->32],{n}],x_/;Norm[x]<1]/n,16])

pi[55555]

pi[555]

pi[555]

Linear

Accumulate[{aa,ss,dd,ff}]

ListLinePlot[Accumulate[RandomReal[{-1,1},100]],PlotLabel->"Repeat it!"]

Generating functions

generatorfunctionology

ClearAll[p,k,z];Factor@Sum[PDF[BinomialDistribution[17,p],k](z^k),{k,0,17}]

Expand[%]

G[λ_,z_]:=Sum[(((λ^k)(E^(-λ)))/(k!))(z^k),{k,0,+∞}]

G[λ,z]

{D[G[λ,z],{z,1}]/.{z->1},Mean[PoissonDistribution[λ]]}

A simpler way

Derivative[0,1][G][λ,1]

Higher moments

Built-in function

GeneratingFunction[(((λ^n)(E^(-λ)))/(n!)),n,z]

GeneratingFunction[(1/((n!)^2)),n,x]

GeneratingFunction[DifferenceRoot[Function[{y,m},{y[m+2]==y[m+1]+y[m],y[0]==0,y[1]==1}]][n],n,z]

GeneratingFunction[Cos[n],n,x,GenerateConditions->True]

GeneratingFunction[2^n,n,x,VerifyConvergence->True,GenerateConditions->True]

FindGeneratingFunction[{1,1,2,3,5,8,13},x]

FindSequenceFunction[{3,7,13,21,31,43,57},n]

ExponentialGeneratingFunction[n/(m+1),{n,m},{x,y}]

Characteristic functions

CharacteristicFunction[CauchyDistribution[0,1],t]

CharacteristicFunction[NormalDistribution[0,1],t]

CharacteristicFunction[PoissonDistribution[λ],t]/.{t->z/I}

CharacteristicFunction[GeneralizedExtremeValueDistribution[3,1,0],t]

Moments

cm[n_]:=D[CharacteristicFunction[NormalDistribution[],t],{t,n}]/(I^n)/.{t->0}

TableForm[{cm/@Range[4],Through[{Mean,Variance, Skewness, Kurtosis}[NormalDistribution[]]]}]

Markov chains

Majd, ha lesznek, akkor ez jól jön

MatrixPower, Limit[MatrixPower[P,n],n->+∞]

𝒟=StationaryDistribution[QueueingProcess[λ,μ]];

PDF[𝒟,x]

{Mean[𝒟],Variance[𝒟]}

Probability[x^2+E^x<5,x\[Distributed]𝒟]

proc=DiscreteMarkovProcess[{1/3,0,2/3,0,0},{{(1/2),(1/2),0,0,0},{(1/3),(2/3),0,0,0},{0,(1/3),(2/3),0,0},{0,0,0,0,1},{0,0,0,1,0}}];

StationaryDistribution[proc]

Branching processes

NestList[(#/.z->G[λ,z])&,G[λ,z],2]//Simplify

Probability of extinction

FindRoot[G[2,z]==z,{z,0}]

Programming notes

Patterns

g[x_]:=PolyLog[2,x];
m={1,2,π,-3};

mymap1[f_,l_List]:=Table[f[x],{x,l}]
mymap1[g,m]

mymap[f_,l_List]:=mymap[f,{},l]
mymap[g,m]

mymap[f_,{x___},{y_,z___}]:=mymap[f,{x,f[y]},{z}]
mymap[g,m]

mymap[_,l_,{}]:=l

mymap[g,m]

Lists (Period doubling bifurcation)

Riffle

Riffle[{aa,bb,cc,dd},{ee,ff,gg,hh}]

palya={x0,fx0,ffx0,fffx0};

t1=Transpose[{palya,palya}]

Drop[palya,-1]

Drop[palya,1]

t2=Transpose[{Drop[palya,-1],Drop[palya,1]}]

Riffle[Most[t1],t2]

palya will be constructed using NestList.

Hyperlink["A good introductory paper about chaos with more biology than mathematics.","http://isites.harvard.edu/fs/docs/icb.topic1313269.files/Lecture22/May_Simple%20mathematical%20models%20with%20very_76.pdf"]

Magyarul : Nagyon bonyolult dinamikájú egyszerű matematikai modellek, Alkalmazott Matematikai Lapok 8 (1982), 427-446.

See also Pick and Distribute.