R Markdown

Spliced distributions We defined the folloeing: \[ X_1 \sim exp(0.5) \quad for \quad [0,1) \] \[ X_2 \sim exp(2) \quad for \quad [1,3) \] \[ X_3 \sim P(2,3) \quad for \quad [3,\infty) \] with \[p_i=\frac{1}{3}\]

Before that, we can look at the distributions on each interval.

x1<-c(0:1)
x2<-c(1:3)
x3<-c(3:10)
plot(1, type="n", xlab="", ylab="", xlim=c(0, 10), ylim=c(0, 0.7))
lines(x1,dexp(x1,0.5),type="l",col="blue")
lines(x2,dexp(x2,2),col="red")
lines(x3,actuar::dpareto(x3,2,3),col="green")

We get the integrals to estimate the corresponding associated distributions:

f1<-function(x) {0.5*exp(-0.5*x)}
int1<-integrate(f1,0,1)
int1<-int1$value

f2<-function(x) {2*exp(-2*x)}
int2<-integrate(f2,1,3)
int2<-int2$value

f3<- function(x) {actuar::dpareto(x,2,3)}
int3<- integrate(f3,3,Inf)
int3<-int3$value

Now, we can estimate the new distributions. Since we have R, we can create new functions:

f1a<-function(x) {(1/3)*(f1(x1)/int1)}
f2a<-function(x) {(1/3)*(f2(x2)/int2)}
f3a<-function(x) {(1/3)*(f3(x3)/int3)}

x1<-c(0:1)
x2<-c(1:3)
x3<-c(3:10)
plot(1, type="n", xlab="", ylab="", xlim=c(0, 10), ylim=c(0, 0.7))
lines(x1,f1a(x1),type="l",col="blue")
lines(x2,f2a(x2),col="red")
lines(x3,f3a(x3),col="green")

Now, we have to make this a continous distribution. We have a system of equations to solve, I use the “solve” function but first, we have to create a matrix with coefficients and a vector with independent values:

A<- c(f1(1)/int1,-f2(1)/int2,f3(3)/int3,(f2(3)/int2)+(f3(3)/int3))
A.m<-matrix(A,nrow=2,ncol=2,byrow=T)
A.m
##           [,1]       [,2]
## [1,] 0.7707470 -2.0373147
## [2,] 0.3333333  0.3706481
B<- c(0,f3(3)/int3)

se<-solve(A.m,B)
p1<-se[1]
p2<-se[2]
p3<-1-p1-p2
p1
## [1] 0.7038955
p2
## [1] 0.2662944
p3
## [1] 0.0298101

And now, lets see what is the final result:

f1b<-function(x) {p1*(f1(x1)/int1)}
f2b<-function(x) {p2*(f2(x2)/int2)}
f3b<-function(x) {p3*(f3(x3)/int3)}

x1<-c(0:1)
x2<-c(1:3)
x3<-c(3:10)
plot(1, type="n", xlab="", ylab="", xlim=c(0, 10), ylim=c(0, 0.7))
lines(x1,f1b(x1),type="l",col="blue")
lines(x2,f2b(x2),col="red")
lines(x3,f3b(x3),col="green")

Plto everything together

x1<-c(0:1)
x2<-c(1:3)
x3<-c(3:10)
plot(1, type="n", xlab="", ylab="", xlim=c(0, 10), ylim=c(0, 0.7))
lines(x1,f1a(x1),type="l",col="blue",lty=2)
lines(x2,f2a(x2),col="red",lty=2)
lines(x3,f3a(x3),col="green",lty=2)
lines(x1,f1b(x1),type="l",col="blue",lty=1)
lines(x2,f2b(x2),col="red",lty=1)
lines(x3,f3b(x3),col="green",lty=1)