# Scan statistique - Méthode de Monte Carlo et calcul de p-value
## 1. Proposition for simulations under $\mathcal{H}_1$
In this part, we propose a method that simulates a Poisson process under the hypothesis $\mathcal{H}_1$. The idea is to simulate a sample under $\mathcal{H}_0$, and add randomly a subsequence under the alternative hypothesis in this sequence.
## 2. Simulation of the sequences under $\mathcal{H}_0$ via a Monte Carlo Method
In this part, we will try to simulate, using a Monte Carlo method, a set of $10^5$ independant samples, under the assumption that $\lambda=\lambda_0$, hence, that we are under the null hypothesis $\mathcal{H}_0$.
We test the scan statistic method for different values of $\lambda_0$. The method of scan statistic we implemented will allow us to have access to the scan test statistic and where it happens in the sequence.
### 2.2. Simulation under $\mathcal{H}_0$ and computation of p-values
On simule des séquences sous $\mathcal{H}_0$, que l'on stocke. On calcule la valeur de la scan stat et de la p-value, que l'on stocke aussi. On a une séquence de p-valeur des scans et une séquence de score local.
```{r}
NbSeqH0=5
NbSeqH1=5
DataH0=vector("list")
DataH1=vector("list")
lambda0=3
lambda1=5
T=10
tau=1
#Creation of a sequence that contains the sequence simulated under the null hypothesis
for (i in 1:NbSeqH0) {
ppi=PoissonProcess(lambda0,T)
DataH0[[i]]=ppi
}
#Creation of a sequence that contains the sequence simulated under the alternative hypothesis
for (i in 1:NbSeqH1) {
pphi=SimulationH1(lambda0, lambda1,T,tau)
DataH1[[i]]=pphi
}
#Computation of the time between events
TimeBetweenEventList <- function(list,n_list){
TBE=vector("list",length=n_list)
for (i in (1:n_list)) {
ppi=list[[i]]
ni=length(ppi)
tbei=ppi[2:ni]-ppi[1:ni-1]
TBE[[i]]=tbei
}
return (TBE)
}
tbe0=TimeBetweenEventList(DataH0,NbSeqH0)
```
We compute the p-value associated to all 5 sequences, and stock them in a vector.
```{r}
#We start by computing the empirical distribution for lambda0
Emp=EmpDistrib(lambda0,n_sample,T,tau)
scan=c()
pvalue=c()
#Then, we stock the p-value and the
for (i in 1:NbSeqH0){
ppi=DataH0[[i]]
result=PValue(Emp,DataH0[[i]],T,tau)
scan=c(scan,result[1])
pvalue=c(pvalue,result[2])
cat(paste("\nSimulation for the sequence", i, ", for lambda0=",lambda0, " ,lambda1=", lambda1, " , scan=", result[1] ,"p-value=",result[2]))