# Predictor

MaxWiK metasampling algorithm can be used for prediction. For this purpose one can run function MaxWiK.predictor(), here is the 2D demonstration:

```{r, include = TRUE, echo=TRUE, eval=FALSE}
pred.input  =  MaxWiK::Data.2D$predictor$result$input.parameters
stat.sim    =  MaxWiK::Data.2D$Y
par.sim     =  MaxWiK::Data.2D$X
new.param   =  as.data.frame( t( MaxWiK::Data.2D$observation$x0 ) )
iKernelABC  =  MaxWiK::Data.2D$predictor$result$iKernelABC
predictor   =  MaxWiK.predictor( psi = pred.input$psi, 
                                 t   = pred.input$t, 
                                 param     = par.sim, 
                                 stat.sim  = stat.sim, 
                                 new.param = new.param,
                                 talkative = FALSE, 
                                 check_pos_def = FALSE ,
                                 n_bullets  = 42, 
                                 n_best     = 12, 
                                 halfwidth  = 0.5, 
                                 epsilon    = 0.001, 
                                 rate       = 0.2, 
                                 max_iteration = 10, 
                                 save_web   = TRUE, 
                                 use.iKernelABC = iKernelABC 
)
```

Here the iKernelABC is the results of MaxWiK ABC algorithm. So, the most probable value of the function is

```{r, include = TRUE, echo=TRUE, eval=FALSE}
predictor$prediction.best
```

To see the distribution of the points generated by metasampling one can get a metasampling 'network':

```{r, include = TRUE, echo=TRUE, eval=FALSE}
pred.network  = unique.data.frame( do.call(rbind.data.frame, predictor$spiderweb ) )
```

In principle, MaxWiK algorithm is able to produce metasampling points out of the reasonble range.
So, please, pay attention that results should be restricted by possible range of values for each dimension, for example:

```{r, include = TRUE, echo=TRUE, eval=FALSE}
pred.network  = apply_range( diapason = c(0,1000), input.data = pred.network )
predictor$prediction.best = apply_range( diapason = c(0,1000), 
                            input.data = predictor$prediction.best )
```

There are plots for posteriori distribution of $Y1$ and $Y2$ after the range application:

```{r, echo=FALSE, include = TRUE, fig.height=3, fig.width=5}
library('ggplot2')
posteriori.pred.MaxWiK  =  MaxWiK::Data.2D$predictor$posteriori.MaxWiK
pred.network = MaxWiK::Data.2D$predictor$network
pred.network  = apply_range( diapason = c(0,1000), input.data = pred.network )
obs = MaxWiK::Data.2D$observation
best.sim = MaxWiK::Data.2D$predictor$result$prediction.best
best.sim  = apply_range( diapason = c(0,1000), input.data = best.sim )
MaxWiK::MaxWiK.ggplot.density( title = ' Posteriori distribution of Y1 parameter', 
                   datafr1 = posteriori.pred.MaxWiK, 
                   datafr2 = pred.network, 
                   var.df  = 'stat.sim.Y1', 
                   obs.true = obs$A[ 1 ], 
                   best.sim = as.numeric( best.sim[ 1 ] )
)
```

```{r, echo=FALSE, include = TRUE, fig.height=3, fig.width=5}
library('ggplot2')
posteriori.pred.MaxWiK  =  MaxWiK::Data.2D$predictor$posteriori.MaxWiK
pred.network = MaxWiK::Data.2D$predictor$network
pred.network  = apply_range( diapason = c(0,1000), input.data = pred.network )
obs = MaxWiK::Data.2D$observation
best.sim = MaxWiK::Data.2D$predictor$result$prediction.best
best.sim  = apply_range( diapason = c(0,1000), input.data = best.sim )
MaxWiK::MaxWiK.ggplot.density( title = ' Posteriori distribution of Y2 parameter', 
                   datafr1 = posteriori.pred.MaxWiK, 
                   datafr2 = pred.network, 
                   var.df  = 'stat.sim.Y2', 
                   obs.true = obs$A[ 2 ], 
                   best.sim = as.numeric( best.sim[ 2 ] )
)
```

Here the red line is a true value, blue one is the best prediction, the green area is the posteriori based on the MaxWiK ABC algorithm, the red area is metasampling.
