########################################################################
1. load
scipy.optimize.minimize
# weights are summed to be 1
2.
cons = ({'type': 'eq', 'fun': lambda x: 1 - sum(x)})
3. def cal_mase(w):
forecasts by w[0]*method0 + w[1]*mothod1 + ...
calculate the mase from forecasts and testing set
4. min_mase = minimize(cal_mase, init_weight0, method='SLSQP', constraints=cons)
5. get the weights
#########################################################################
wilcox.test
#########################################################################
How to estimate the parameters of ARIMA
https://stats.stackexchange.com/questions/77663/arima-estimation-by-hand
## Load Packages
library(stats)
library(forecast)
set.seed(456)
## Simulate Arima
y <- arima.sim(n=250,list(ar=0.3,ma=0.7),mean = 5)
plot(y)
## Optimize Log Liklihood for ARIMA
n = length(y) ## Count the number of Observatuions
e = rep(1,n) ## Initialize e
logl <- function(mx){
g <- numeric
mx <- matrix(mx,ncol=4)
mu <- mx[,1] ## Constant Term
sigma <- mx[,2]
rho <- mx[,3] ## Ar coeff
theta <- mx[,4] ## Ma coeff
e[1] = 0 ## Since e1 = 0
for (t in (2 : n)){
e[t] = y[t] - mu - rho*y[t-1] + theta*e[t-1]
}
## Maximize Log Liklihood Function
g1 <- (-((n)/2)*log(2*pi) - ((n)/2)*log(sigma^2+0.000000001) - (1/2)*(1/(sigma^2+0.000000001))*e%*%e)
##note: multiplying Log Likelihood by "-1" inorder to maximize in the optimziation
## This is done becuase Optim function in R can only minimize, "X"ing by -1 we can maximize
## also "+"ing by 0.000000001 sigma^2 to avoid divisible by 0
g <- -1 * g1
return(g)
}
## Optimize Log Liklihood
arimopt <- optim(par=c(10,0.6,0.3,0.5), fn=logl, gr = NULL,
method = c("L-BFGS-B"),control = list(), hessian = T)
arimopt
#######################################################################################
Spyder profiler
need to be optimized
留言
張貼留言