In [2]:
library(repr)
options(warn=-1)
options(repr.plot.width=10, repr.plot.height=5)
source("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/rfts/FTS.r")
In [3]:
enrollments <- read.csv("/home/petronio/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/DataSets/Enrollments.csv",sep=";")
#denrollments <- data.frame(enrollments$Year, c(NA,D(enrollments$Enrollments,1)))
#names(denrollments) <- c("Year","Enrollments")
summary(enrollments)
options(repr.plot.width=10, repr.plot.height=5)
#par(mfrow=c(2,1))
plot(enrollments,type="l",xlab="t",ylab="F(t)")
#plot(denrollments,type="h")
#abline(h=0)
Out[3]:
In [3]:
#rm(tmp)
#rm(taiex)
tmp <- read.csv("/home/petronio/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/DataSets/TAIEX.csv",sep=",") #, nrows=2000)
taiex <- tmp[c("Date","avg")]
taiex$Date <- as.Date(taiex$Date)
rownames(taiex) <- 1:nrow(taiex)
#dtaiex <- data.frame(taiex$Date, c(NA,D(taiex$avg,1)))
#names(dtaiex) <- c("Date","avg")
options(repr.plot.width=10, repr.plot.height=5)
#par(mfrow=c(2,1))
plot(taiex$avg,type="l")
#plot(dtaiex,type="h")
#abline(h=0)
#summary(taiex)
In [53]:
tmp <- read.csv("/home/petronio/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/DataSets/NASDAQ_IXIC.csv",sep=",") #, nrows=2000)
nasdaq <- tmp[c("Date","avg")]
nasdaq$Date <- as.Date(nasdaq$Date)
rownames(nasdaq) <- 1:nrow(nasdaq)
#nasdaq
#dtaiex <- data.frame(taiex$Date, c(NA,D(taiex$avg,1)))
#names(dtaiex) <- c("Date","avg")
options(repr.plot.width=10, repr.plot.height=5)
#par(mfrow=c(2,1))
plot(nasdaq,type="l")
#plot(dtaiex,type="h")
#abline(h=0)
#summary(taiex)
In [24]:
source("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/rfts/FTS.r")
# Construtor do modelo (dados, # partições, função de pertinência, outros parâmetros)
fit <- FitSongFTS(enrollments$Enrollments,20,trimf,NULL)
# treina o modelo
fts <- fit$train();
# mostra as regras (FLRG)
#sprintf(fts$dump())
# predição utilizando o modelo
forecasted = fts$forecast(enrollments$Enrollments)
plot(c(enrollments$Enrollments,NA), type="l",col="red")
lines(c(NA,forecasted))
MAPE(enrollments$Enrollments, forecasted)
Out[24]:
In [8]:
source("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/rfts/FTS.r")
# Construtor do modelo
fit <- FitChenFTS(enrollments$Enrollments,5,trimf,NULL)
# treina o modelo
fts <- fit$train();
# mostra as regras (FLRG)
sprintf(fts$dump())
# predição utilizando o modelo
fts$forecast(c(15000, 19000))
# f(t) f(t+1)
Out[8]:
Out[8]:
In [9]:
source("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/rfts/FTS.r")
fitw <- FitYuFTS(enrollments$Enrollments,7,trimf,NULL)
wfts <- fitw$train();
sprintf(wfts$dump())
Out[9]:
In [10]:
source("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/rfts/FTS.r")
fite <- FitEfendiFTS(taiex$avg[2500:4000],25,trimf,NULL)
efts <- fite$train();
sprintf(efts$dump())
Out[10]:
In [11]:
source("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/rfts/FTS.r")
fits <- FitSadaeiFTS(enrollments$Enrollments,6,trimf,1.2)
sfts <- fits$train();
sprintf(sfts$dump())
Out[11]:
In [12]:
fiti <- FitIFTS(enrollments$Enrollments,7,trimf,NULL)
ifts <- fiti$train();
sprintf(ifts$dump())
ifts$forecast(15000)
#pfts$fuzzySets
Out[12]:
Out[12]:
In [13]:
pfts$fuzzySets$A4$membership(16894)
pfts$fuzzySets$A5$membership(16894)
In [7]:
fitiw <- FitIWFTS(enrollments$Enrollments,8,trimf,NULL)
iwfts <- fitiw$train();
sprintf(iwfts$dump())
iwfts$forecast(5000)
Out[7]:
Out[7]:
In [8]:
subset <- taiex$avg[800:1000]
options(repr.plot.width=10, repr.plot.height=5)
plot(subset,type="l",xlab="t",ylab="F(t)")
l <- length(subset)
prevs <- matrix(rep(0,l*2),l,2)
#prevs_p <- pfts$forecast(subset)
prevs_iw <- iwfts$forecast(subset)
#lines(seq(1,l+1),prevs_p[,1],col="red")
lines(seq(1,l+1),prevs_iw[,1],col="green")
#lines(seq(1,l+1),prevs_p[,2],col="red")
lines(seq(1,l+1),prevs_iw[,2],col="green")
#legend("topright",legend=c("PFTS","PWFTS"), fill=c("red","green"))
In [44]:
fiti5 <- FitIFTS(enrollments$Enrollments,4,trimf,NULL)
ifts5 <- fiti5$train();
fiti6 <- FitIFTS(enrollments$Enrollments,6,trimf,NULL)
ifts6 <- fiti6$train();
fiti7 <- FitIFTS(enrollments$Enrollments,10,trimf,NULL)
ifts7 <- fiti7$train();
subset <- enrollments$Enrollments[4:19]
options(repr.plot.width=10, repr.plot.height=5)
plot(enrollments$Year[4:19],subset,type="l",xlab="t",ylab="F(t)",ylim=c(11000,25000), lwd=2, lty=2)
l <- length(subset)
prevs <- matrix(rep(0,l*2),l,2)
p5 <- ifts5$forecast(subset)
lines(enrollments$Year[4:18],p5[2:l,1],col="blue",lwd=2)
lines(enrollments$Year[4:18],p5[2:l,2],col="blue",lwd=2)
p6 <- ifts6$forecast(subset)
lines(enrollments$Year[4:18],p6[2:l,1],col="orange",lwd=2)
lines(enrollments$Year[4:18],p6[2:l,2],col="orange",lwd=2)
p7 <- ifts7$forecast(subset)
lines(enrollments$Year[4:18],p7[2:l,1],col="red",lwd=2)
lines(enrollments$Year[4:18],p7[2:l,2],col="red",lwd=2)
legend("topleft",legend=c("Original",ifts5$npart,ifts6$npart,ifts7$npart),
col=c("black", "blue","orange","red"), lwd=c(2,2,2,2), lty=c(2,1,1,1), cex=1.2)
In [56]:
Sharpness(p7)
ifts7$fuzzySets[[1]]$upper - ifts7$fuzzySets[[1]]$lower
Out[56]:
Out[56]:
In [61]:
intervals <- matrix(c(4711.5, 6608.796875, 2792, 4860.5625, 1507.68, 2475.775), ncol=2, byrow=TRUE )
colnames(intervals) <- c("Partition Length", "Interval Length")
rownames(intervals) <- c("4", "6", "10")
intervals <- as.table(intervals)
intervals
barplot(intervals,beside=TRUE)
Out[61]:
In [83]:
testPartitionsInterval(FitPWFTS, taiex,"Date","avg", c(10,15,20,25,30), 0.8)
In [127]:
benchmarkAll(taiex,"Date","avg", c(25,25,25,25,25), 0.8)
In [34]:
subset <- taiex$avg[2500:4500]
dates <-taiex$Date[2500:4500]
In [35]:
plot(dates,subset,type="l",xlab="t",ylab="F(t)",lwd=1)
abline(v=dates[1400])
In [43]:
source("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/rfts/FTS.r")
partitions <- 40
window <- 600
trainData <- subset[1:1400]
testData <- subset[1400:(1400 + window)]
testDates <- dates[1401:(1400 + window)]
index <- seq(2,1 + window,1)
options(repr.plot.width=10, repr.plot.height=5)
plot(testDates,testData[1:window],type="l",xlab="t",ylab="F(t)",lwd=2)
l <- length(subset)
##### FTS - CHEN
executaTeste(FitChenFTS,partitions,NULL, trainData, testData, testDates, index, "red", 1, 1)
##### WFTS - YU
executaTeste(FitYuFTS,partitions,NULL, trainData, testData, testDates, index, "blue", 1, 1)
##### IWFTS - Ismail & Efendi
executaTeste(FitEfendiFTS,partitions,NULL, trainData, testData, testDates, index, "green", 1, 1)
##### EWFTS - Sadaei
executaTeste(FitSadaeiFTS,partitions,1.2, trainData, testData, testDates, index, "purple", 1, 1)
##### IFTS - Interval FTS
executaTesteInterval(FitIFTS,partitions,NULL, trainData, testData, testDates, index, "black", 2, 1)
legend("topleft",
legend=c("TAIEX",fts$name, wfts$name, efts$name, sfts$name,"Proposed method"),
col=c("black","red","blue","green","orange","black"),
lty=c(1,1,1,1,1,2), lwd=c(2,1,1,1,1,1),
cex=1.2)
In [11]:
source("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/rfts/FTS.r")
window <- 200
trainData <- subset[1:1400]
testData <- subset[1400:(1400 + window)]
testDates <- dates[1401:(1400 + window)]
index <- seq(2,1 + window,1)
options(repr.plot.width=10, repr.plot.height=5)
plot(testDates,testData[1:window],type="l",xlab="t",ylab="F(t)",lwd=2,ylim=c(6600,9500))
executaTesteInterval(FitIFTS,15,NULL, trainData, testData, testDates, index, "blue", 1, 1)
executaTesteInterval(FitIFTS,20,NULL, trainData, testData, testDates, index, "green", 1, 1)
executaTesteInterval(FitIFTS,25,NULL, trainData, testData, testDates, index, "yellow", 1, 1)
executaTesteInterval(FitIFTS,30,NULL, trainData, testData, testDates, index, "orange", 1, 1)
executaTesteInterval(FitIFTS,35,NULL, trainData, testData, testDates, index, "red", 1, 1)
executaTesteInterval(FitIFTS,40,NULL, trainData, testData, testDates, index, "purple", 1, 1)
legend("topleft",
legend=c("TAIEX","15", "20", "25", "30","35","40"),
col=c("black","blue","green","yellow","orange","red","purple"),
lty=c(1,1,1,1,1,1), lwd=c(2,1,1,1,1,1),
cex=1.2)
In [46]:
subset <- nasdaq$avg[1:2000]
dates <-nasdaq$Date[1:2000]
In [54]:
#plot(nasdaq$avg,type="l",xlab="t",ylab="F(t)",lwd=1)
#abline(v=dates[1400])
dates[1]
dates[2000]
plot(dates,subset,type="l",xlab="t",ylab="F(t)",lwd=1)
abline(v=dates[1400])
Out[54]:
Out[54]:
In [52]:
source("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/rfts/FTS.r")
partitions <- 40
window <- 600
trainData <- subset[1:1400]
testData <- subset[1400:(1400 + window)]
testDates <- dates[1401:(1400 + window)]
index <- seq(2,1 + window,1)
options(repr.plot.width=10, repr.plot.height=5)
plot(testDates,testData[1:window],type="l",xlab="t",ylab="F(t)",lwd=2)
l <- length(subset)
##### FTS - CHEN
executaTeste(FitChenFTS,partitions,NULL, trainData, testData, testDates, index, "red", 1, 1)
##### WFTS - YU
executaTeste(FitYuFTS,partitions,NULL, trainData, testData, testDates, index, "blue", 1, 1)
##### IWFTS - Ismail & Efendi
executaTeste(FitEfendiFTS,partitions,NULL, trainData, testData, testDates, index, "green", 1, 1)
##### EWFTS - Sadaei
executaTeste(FitSadaeiFTS,partitions,1.2, trainData, testData, testDates, index, "purple", 1, 1)
##### IFTS - Interval FTS
executaTesteInterval(FitIFTS,partitions,NULL, trainData, testData, testDates, index, "black", 2, 1)
legend("topleft",
legend=c("NASDAQ",fts$name, wfts$name, efts$name, sfts$name,"Proposed method"),
col=c("black","red","blue","green","orange","black"),
lty=c(1,1,1,1,1,2), lwd=c(2,1,1,1,1,1),
cex=1.2)
In [ ]: