Moving average model, MA(q) implementation

377 Views Asked by At

Assuming that I have a Time Series 'X' which is a 1000 days data of a stock.

can someone share a code sample (in any language) even pseudo code for the Moving average model order q?

3

There are 3 best solutions below

1
On BEST ANSWER

This is an example in Matlab for MA(2) the problem that I was engaging with is how to get the models parameters (0.2,0.5).

X = zeros(10000,1);
Z = randn(10000,1);

for i=3:length(X)
    X(i) = Z(i) + 0.5*Z(i-1) + 0.2*Z(i-2); 
end 

plot(X) 

I did some more research and I understand that the parameters are sent as an input which is selected manually or by an optimization process. so this the same works for Moving average order q.

1
On

I dont know about your purpose but if you just want to calibrate the model, use R. i.p require(smooth) require(MComp) sma(data,q)$forecast

0
On

Again an R Code:


weights<-rep(1/q,q) # or any desired weights
ma<-filter(data,weights,sides=1)


plot(data)
lines(ma)

and you can also take a look at the autocorrelation function
acf(ma,na.action=na.pass)