In [1]:
using Plots, ApproxFun

Piecewise function sampling


In [2]:
f = abs(Fun(sin,[-5,5]))
r = ApproxFun.sample(f,10000)
plot(f/sum(f))               
histogram!(r;normed=true,nbins=100)


[Plots.jl] Initializing backend: gr
Out[2]:
-2.5 0.0 2.5 0.00 0.05 0.10 0.15 y1 y5

Jacobi singularity sampling


In [3]:
x=Fun(identity)
f = exp(x)*sqrt(1-x^2)
r = ApproxFun.sample(f,10000)
plot(f/sum(f))                     
histogram!(r;normed=true,nbins=100)


Out[3]:
-1.0 -0.5 0.0 0.5 1.0 0.00 0.25 0.50 0.75 y1 y2

In [4]:
x=Fun(identity)
f = exp(x)/sqrt(1-x^2)
r = ApproxFun.sample(f,10000)
plot(f/sum(f);ylims=(0.,5.))    
histogram!(r;normed=true,nbins=100)


Out[4]:
-0.5 0.0 0.5 0 1 2 3 4 5 y1 y2

In [5]:
x=Fun(identity)
f = exp(x)*(1-x)^0.123*(1+x)^(-0.234)
r = ApproxFun.sample(f,10000)
plot(f/sum(f))                         
histogram!(r;normed=true,nbins=100)


Out[5]:
-0.5 0.0 0.5 1.0 0.0 0.5 1.0 1.5 y1 y2

Heavy-tail sampling


In [6]:
f=Fun(x->(1+sech(x))/(1+x^2),[-Inf,Inf])
r=ApproxFun.sample(f,10000)

plot(-15.:.01:15.,f/sum(f))
histogram!(filter!(r->abs(r)<15,r);normed=true,nbins=100)


Out[6]:
-15 -10 -5 0 5 10 15 0.0 0.1 0.2 0.3 0.4 y1 y2

In [7]:
f=Fun(x->(exp(-x)+1)/(1+x^2),[0,Inf])
r=ApproxFun.sample(f,10000)

plot(0:.05:15,f/sum(f))
histogram!(filter!(r->0<r<15,r);normed=true,nbins=100)


Out[7]:
0.0 2.5 5.0 7.5 10.0 12.5 15.0 0.00 0.25 0.50 0.75 y1 y2