/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.io.XMLio;


import com.approximatrix.draco.analysis.*;
import com.approximatrix.draco.fit.*;

public class AnalysisLoadStore {
	
	private Class analysis_type = null;
	
	private String analysis_subtype = null;
	
	private String name = null;
	private String results = null;
	private String dependent = null;
	private String[] independents = null;
	private String[] instruments = null;
	
	private String[] parameter_names = null;
	private double[] parameter_values = null;
	
	private double[] residuals = null;
	private double[][] covariance = null;
	
	private String equation = null;
	
	public AnalysisLoadStore(String type) {
		
		if(type == null) return;
		
		String ptype = type;
		
		if(type.indexOf(':') >= 0) {
			String[] values = type.split(":");
			ptype = values[0];
			this.analysis_subtype = values[1];
		}
		
		// Cycle through to get the proper class
		
		// Logit
		//if(ptype.compareTo(RunLogit.class.getName())==0) {
		if(ptype.compareTo(SimpleMLEFit.class.getName())==0) {
			analysis_type = SimpleMLEFit.class;
			
		// Least Squares
		} else if(ptype.compareTo(RunLS.class.getName())==0) {
			analysis_type = RunLS.class;
			
		// LS with Autocorrellation
		} else if(ptype.compareTo(RunLSAutocorrelation.class.getName())==0) {
			analysis_type = RunLSAutocorrelation.class;
			
		// Non-Linear Least Squares
		} else if(ptype.compareTo(RunNLLS.class.getName())==0) {
			analysis_type = RunNLLS.class;
			
		// Two-Stage Least Squares
		} else if(ptype.compareTo(RunTSLS.class.getName())==0) {
			analysis_type = RunTSLS.class;
			
		// ANOVA
		} else if(ptype.compareTo(RunANOVA.class.getName())==0) {
			analysis_type = RunANOVA.class;
		}
		
	}
	
	public Class getAnalysisClass() {
		return analysis_type;
	}
	
	public String getAnalysisSubType() {
		return analysis_subtype;
	}
	
	public String getName() {
		return name;
	}
	
	public String getDependentVariable() {
		return dependent;
	}
	
	public String[] getIndependentVariables() {
		return independents;
	}
	
	public String getResults() {
		return results;
	}
	
	public String[] getInstruments() {
		return instruments;
	}
	
	public String[] getCoefLabels() {
		return parameter_names;
	}
	
	public double[] getCoef() {
		return parameter_values;
	}
	
	public double[] getResiduals() {
		return residuals;
	}
	
	public double[][] getCovarianceMatrix() {
		return covariance;
	}
	
	public String getEquation() {
		return equation;
	}
	
	public void setAnalysisSubType(String text) {
		analysis_subtype = text;
	}
	
	public void setName(String text) {
		name = text;
	}
	
	public void setResults(String text) {
		results = text;
	}
	
	public void setDependentVariable(String text) {
		dependent = text;
	}
	
	public void setIndependentVariables(String[] text) {
		independents = text;
	}
	
	public void setInstruments(String[] text) {
		instruments = text;
	}
	
	public void setCoefLabels(String[] text) {
		parameter_names = text;
	}
	
	public void setCoefValues(String[] text) {
		if(text == null) return;
		double[] temp = new double[text.length];
		for(int i=0;i<text.length;i++) {
			try {
				temp[i] = new Double(text[i]).doubleValue();
			} catch(NumberFormatException nfe) {
				temp[i] = Double.NaN;
			}
		}
		this.setCoefValues(temp);
	}
	
	public void setCoefValues(Double[] values) {
		if(values == null) return;
		double[] temp = new double[values.length];
		for(int i=0;i<values.length;i++) {
			try {
				temp[i] = values[i].doubleValue();
			} catch(NullPointerException npe) {
				temp[i] = Double.NaN;
			}
		}
		this.setCoefValues(temp);
	}
	
	public void setCoefValues(double[] values) {
		parameter_values = values;
	}
	
	public void setResiduals(String[] text) {
		if(text == null) return;
		double[] temp = new double[text.length];
		for(int i=0;i<text.length;i++) {
			try {
				temp[i] = new Double(text[i]).doubleValue();
			} catch(NumberFormatException nfe) {
				temp[i] = Double.NaN;
			}
		}
		this.setResiduals(temp);
	}
	
	public void setResiduals(double[] values) {
		residuals = values;
	}
	
	public void setCovarianceMatrix(String[][] text) {
		if(text == null) return;
		double[][] temp = new double[text.length][];
		for(int i=0;i<text.length;i++) {
			temp[i] = new double[text[i].length];
			for(int j=0;j<text[i].length;j++) {
				try {
					temp[i][j] = new Double(text[i][j]).doubleValue();
				} catch(NumberFormatException nfe) {
					temp[i][j] = Double.NaN;
				}
			}
		}
		this.setCovarianceMatrix(temp);
	}
	
	public void setCovarianceMatrix(double[][] values) {
		covariance = values;
	}
	
	public void setEquation(String text) {
		equation = text;
	}
}
