# Generating model for position
Xs=matrix(c( -50, 0 , 150, 0, 50,173.2),2,3)  # "satelite positions"
# For simplicity in the example we fix the position of satellites



## ground truth position
x1GT = 0:100
x2GT = x1GT*sin(x1GT*pi/20)
##velocity
v1GT =diff(x1GT) # unit time i.e. Delta_t = 1
v2GT =diff(x2GT)
par(mfrow=c(1,1))
plot(Xs[1,],Xs[2,],col=3, xlim=c(-60, 160),ylim=c(-110, 210))
points(x1GT,x2GT)
 
## Ground true distance
d1=sqrt(  (x1GT-Xs[1,1])^2 + (x2GT-Xs[2,1])^2 );
d2=sqrt(  (x1GT-Xs[1,2])^2 + (x2GT-Xs[2,2])^2 );
d3=sqrt(  (x1GT-Xs[1,3])^2 + (x2GT-Xs[2,3])^2 );

##  distance with errors
sigmaD=1  # measurement error
y1 = d1+rnorm(101,0,sigmaD)
y2 = d2+rnorm(101,0,sigmaD)
y3 = d3+rnorm(101,0,sigmaD)

plot(y1,col=2, ylim=c(0,300))
points(y2,col=3)
points(y3,col=4)
lines(d1)
lines(d2)
lines(d3)


#####################################################################################
# initialize parameters
Stdv1  = 1  # additive model for velocity x-direction standard deviation
Stdv2  = 1  # additive model for velocity y-direction
StdX01 = 10 # uncertaintyof starting point x-direction
StdX02 = 10 # uncertaintyof starting point  y-direction
muX01  = 0  # mean starting point x-direction
muX02  = 0  # mean starting point  y-direction
  



#################################################################################
###########   computing the filter distributions 
N=10000

x1=matrix(0,N,101)
x2=matrix(0,N,101)
v1=matrix(0,N,101)
v2=matrix(0,N,101)
neff=matrix(0,101,1)



x1prop = rnorm(N,muX01,StdX01 )
x2prop = rnorm(N,muX02,StdX02 )
v1prop = rep(0,N)
v2prop = rep(0,N)

## solving the filtering problem  [keeping the filter distribution in x1,x2,v1,v2]
for(i in 1:101){
  d1prop =sqrt(  (x1prop-Xs[1,1])^2 + (x2prop-Xs[2,1])^2 );
  d2prop =sqrt(  (x1prop-Xs[1,2])^2 + (x2prop-Xs[2,2])^2 );
  d3prop =sqrt(  (x1prop-Xs[1,3])^2 + (x2prop-Xs[2,3])^2 );
  
  logw  = -0.5*((d1prop-y1[i])/sigmaD)^2 -0.5*((d2prop-y2[i])/sigmaD)^2 -0.5*((d3prop-y3[i])/sigmaD)^2
  wun = exp(logw-max(logw))
  w=wun/sum(wun)
  neff[i]=1/sum(w^2)
  
  ## resampling every time
  ind = sample(1:N, size=N, replace = TRUE, prob = w)
  
  x1[,i]=x1prop[ind]; #x[,i] ~ f(x_i|y1...yi)  filter distribution
  x2[,i]=x2prop[ind]
  v1[,i]=v1prop[ind]
  v2[,i]=v2prop[ind]
  
  v1prop= v1[,i]+rnorm(N,0,Stdv1 )
  v2prop= v2[,i]+rnorm(N,0,Stdv2 )
  
  x1prop =x1[,i]+v1prop
  x2prop =x2[,i]+v2prop
}


par(mfrow=c(1,1)) 
plot(x1GT,x2GT,xlim=c(-10, 110),ylim=c(-110, 210))
for(i in 1:min(N,100))
{
  lines(x1[i,],x2[i,],col=2)
}
 points(x1GT,x2GT,col=1) 
 points(Xs[1,],Xs[2,],col=3 )
 
 
par(mfrow=c(2,1)) 
plot(v1GT,ylim=c(-9,10),main = "Velocity 1st component")
for(i in 1:min(N,100))
{
  lines(v1[i,-1],col=2)
}
points(v1GT,col=1)


plot(v2GT[-101],main = "Velocity 2nd component")
for(i in 1:min(N,100))
{
  lines(v2[i,-1],col=2)
}
points(v2GT[-101],col=1)


#plot(neff,ylim=c(0 ,N))
#plot(sqrt(v1GT^2+v2GT^2))


 
#################################################################################
###########   resampeling full path trying to solve the smooting problem


## resampling
N=10000

x1=matrix(0,N,101)
x2=matrix(0,N,101)
v1=matrix(0,N,101)
v2=matrix(0,N,101)
neff=matrix(0,101,1)



x1prop = rnorm(N,muX01,StdX01 )
x2prop = rnorm(N,muX02,StdX02 )
v1prop = rep(0,N)
v2prop = rep(0,N)

## solving the filtering problem  [keeping the posterior distribution in x1,x2,v1,v2]
##NB this has problems with degenracy 
for(i in 1:101){
  d1prop =sqrt(  (x1[,i] -Xs[1,1])^2 + (x2[,i] -Xs[2,1])^2 );
  d2prop =sqrt(  (x1[,i] -Xs[1,2])^2 + (x2[,i] -Xs[2,2])^2 );
  d3prop =sqrt(  (x1[,i] -Xs[1,3])^2 + (x2[,i] -Xs[2,3])^2 );
  logw  = -0.5*((d1prop-y1[i])/sigmaD)^2 -0.5*((d2prop-y2[i])/sigmaD)^2 -0.5*((d3prop-y3[i])/sigmaD)^2
  wun = exp(logw-max(logw))
  w=wun/sum(wun)
  ## resampling every time
  neff[i]=1/sum(w^2)
  ind = sample(1:N, size=N, replace = TRUE, prob = w)
  
  x1=x1[ind,]; #At end x[,i] ~ f(x_i|y1...y101)  smooting distribution
  x2=x2[ind,]
  v1=v1[ind,]
  v2=v2[ind,]
  
  if( i<101){
    v1[,i+1]= v1[,i]+rnorm(N,0,Stdv1 )
    v2[,i+1]= v2[,i]+rnorm(N,0,Stdv2 )
  
    x1[,i+1] =x1[,i]+v1[,i+1]
    x2[,i+1] =x2[,i]+v2[,i+1]
  }
}



par(mfrow=c(1,1)) 
plot(x1GT,x2GT,xlim=c(-10, 110),ylim=c(-110, 210))
for(i in 1:min(N,100))
{
  lines(x1[i,],x2[i,],col=2)
}
points(x1GT,x2GT,col=1) 
points(Xs[1,],Xs[2,],col=3 )


par(mfrow=c(2,1)) 
plot(v1GT,ylim=c(-9,10),main = "Velocity 1st component")
for(i in 1:min(N,100))
{
  lines(v1[i,-1],col=2)
}
points(v1GT,col=1)


plot(v2GT[-101],main = "Velocity 2nd component")
for(i in 1:min(N,100))
{
  lines(v2[i,-1],col=2)
}
points(v2GT[-101],col=1)


#plot(neff,ylim=c(0 ,N))
#plot(sqrt(v1GT^2+v2GT^2))





