 

d=read.table("Stamps_withoutdupl_norm_09.arff", skip=15,sep=",")



library(archetypes)
source("LstepArchetypesMod.R")

#1 if it is an outlier, 0 if it is not an outlier
label=d[,11]=='yes' 
label[label==TRUE]='1'
label[label==FALSE]='0'
label=as.factor(label)



X=d[,1:9]




#Find archetypes and screeplot (Step 2)
library(DDoutlier)
library(AUC)
nada=15 #15 #it is P
norep=20
set.seed(1234)
ai=LstepArchetypesMod(X, 1:nada, nrep=norep)

#Draw the RSS graph in order to find the elbow (number of initial archetypes)
screeplot(ai)


#Choose the number of initial archetypes
e=9 #2,4,5

vi=10 #5
vf=50 #15
#AA+ k-NN (Step 3)
auci=c()

for (i in vi:vf)
{
pei=rep(0,dim(X)[1])
for (j in e:nada){

a=bestModel(ai[[j]])

pei=pei+KNN_SUM(a$alphas, k=i) 

}
auci=c(auci,auc(roc(pei, label)))

}


#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
 #  Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.8030  0.8822  0.9051  0.8914  0.9119  0.9132 
#> sd(auci)
#[1] 0.02958462

#BEST: 

#e=15:
#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.8953  0.9363  0.9391  0.9354  0.9406  0.9416 
#> sd(auci)
#[1] 0.01029023

#> auci
# [1] 0.8952918 0.9039566 0.9124126 0.9185719 0.9235828 0.9288026 0.9324564 0.9344399 0.9363190 0.9380937 0.9392421 0.9400773
#[13] 0.9405992 0.9412256 0.9411212 0.9410168 0.9412256 0.9416432 0.9415388 0.9412256 0.9412256 0.9412256 0.9410168 0.9403904
#[25] 0.9402860 0.9403904 0.9401816 0.9403904 0.9397641 0.9393465 0.9391377 0.9388245 0.9385113 0.9384069 0.9380937 0.9377806
#[37] 0.9372586 0.9366322 0.9364234 0.9361102 0.9354839

#############


#Knn
pei=c()
auci=c()
vi=10
vf=50

for (i in vi:vf)
{

pei=KNN_SUM(X, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)


#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.8450  0.8796  0.8892  0.8834  0.8909  0.8928 
#> sd(auci)
#[1] 0.0118561


#LOF
pei=c()
auci=c()
vi=10
vf=50

for (i in vi:vf)
{

pei=LOF(X, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.5725  0.7021  0.7139  0.7032  0.7307  0.7478 
#> sd(auci)
#[1] 0.04432808



# COF
pei=c()
auci=c()
vi=10
vf=50

for (i in vi:vf)
{

pei=COF(X, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.4515  0.6151  0.6891  0.6582  0.7202  0.7307 
#> sd(auci)
#[1] 0.08009217




#LOOP
pei=c()
auci=c()
vi=10
vf=50

for (i in vi:vf)
{

pei=LOOP(X, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.4963  0.5941  0.6535  0.6340  0.6814  0.6959 
#> sd(auci)
#[1] 0.05858437


#PCA robust

pc.rob <- princomp(X, covmat = MASS::cov.rob(X))

pei=c()
auci=c()
vi=10
vf=50

for (i in vi:vf)
{

pei=KNN_SUM(pc.rob$scores, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.8450  0.8796  0.8892  0.8834  0.8909  0.8928 
#> sd(auci)
#[1] 0.0118561


#RDOS
pei=c()
auci=c()
vi=10
vf=50

for (i in vi:vf)
{

pei=RDOS(X, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.5188  0.7301  0.7714  0.7313  0.7761  0.7887 
#> sd(auci)
#[1] 0.07945224



#hbos
library('reticulate')
os <- import("os")
source_python('hbos.py')
hbos = HBOS()
hr = hbos$fit_predict(X)
 hr = hbos$fit_predict(data.frame(X))
auc(roc(hr, label)) #

#0.8890281
