package lt.ktu.mockus.srgm;

public class AR
{
	static double[][] A;
	static double[] B;
	static double[] a;		//AR modelio koeficientai
	
	static double[] dVal;	//dVal	- zinomu reiksmiu vektorius
	static int iNum;		//iNum	- zinomu reiksmiu skaicius
	static int p;			//p 	- kiek parametru ivertinti AR modelyje
	
	public AR(double [] dVal,int iNum, int p)
	{
		this.dVal=dVal;
		this.iNum=iNum;
		
		
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<dVal.length; i++)
		{
			sb.append(dVal[i]);
			if (i+1 != dVal.length)
			{
				sb.append(", ");
			}
		}
		//System.out.println("AR -> ivertinti: "+p+" kai zinau:"+iNum + " vektorius:"+sb.toString());
		
		this.p=p;
	}
	void find()
	{
		if (iNum<=p) p=iNum-1;
		A = new double[p][p];
		B = new double[p];
		
		for(int i=0; i<p; i++)
			for (int j=0; j<p; j++)
			{
				A[i][j]=0; int k;
				if (i>j) k=i; else k=j;
				for (int t=k+1; t<iNum; t++)
					A[i][j]+=dVal[t-i-1]*dVal[t-j-1];
			}
		for (int j=0; j<p; j++)
		{
			B[j]=0;
			for (int t=j+1; t<iNum; t++)
				B[j]+=dVal[t-j-1]*dVal[t];
		}
		Gausas lygtis = new Gausas(A,B,p);
		a = lygtis.eqsolve();
	}
	
	public double predict()
	{
		double pred=0;
		
		find();
		for (int t=0; t<p; t++)
			pred+=a[t]*dVal[iNum-t-1];
			
		return pred;
	}
		
}
