Index: applications/robust/main.cpp
===================================================================
--- applications/robust/main.cpp (revision 1379)
+++ applications/robust/main.cpp (revision 1383)
@@ -27,6 +27,6 @@
 //const int utility_constant = 5;
 
-const int max_model_order = 2;
-const double apriorno=0.005;
+const int max_model_order = 1;
+const double apriorno     = 0.01;
 
 /*
@@ -80,4 +80,5 @@
 	int  predicted_channel;
 	mat* data_matrix;
+	vec  predictions;
 	
 	model(set<pair<int,int>> ar_components, //funkcie treidz model-konstruktor
@@ -116,5 +117,5 @@
 			{				
 				V0  = apriorno * eye(ar_components.size()+2); //aj tu konst
-				V0(0,0) = 1;
+				V0(0,0) = 0;
 				my_arx->set_constant(true);				
 			}
@@ -123,5 +124,5 @@
 				
 				V0  = apriorno * eye(ar_components.size()+1);//menit konstantu
-				V0(0,0) = 1;
+				V0(0,0) = 0;
 				my_arx->set_constant(false);				
 				
@@ -143,5 +144,5 @@
 		}
 		if(my_rarx!=NULL)
-		{	//pre robusr priradi az tu do data_vector aj rpedikciu
+		{	//pre robust priradi az tu do data_vector aj predikciu
 			data_vector.ins(0,(*data_matrix).get(predicted_channel,time));
 			my_rarx->bayes(data_vector);
@@ -188,5 +189,5 @@
 		else
 		{
-			mat samples = my_arx->posterior().sample_mat(sample_size);
+			mat samples = my_arx->posterior().sample_mat(sample_size);			
 			
 			vec sample_prediction;
@@ -200,5 +201,5 @@
 				}
 				
-				gau_sample.ins(0,randn());
+				gau_sample.ins(gau_sample.size(),randn());
 
 				sample_prediction.ins(0,gau_sample*samples.get_col(t));				
@@ -262,177 +263,6 @@
 
 
-int main ( int argc, char* argv[] ) {	
-	
-	/*
-	DWORD Id;
-	HANDLE hThrd = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ThrdFunc, (LPVOID)1, 0, &Id);
-  
-	if ( !hThrd )
-    {
-        cout<<"Error Creating Threads,,,,.exiting"<<endl;
-        return -1;
-    }
-    Sleep ( 100 );
-
-	
-	char szApp[] = "MT4";
-	char szTopic[] = "QUOTE";	
-	char szItem1[] = "EURUSD";  	
-
-	//DDE Initialization
-	DWORD idInst=0;
-	UINT iReturn;
-	iReturn = DdeInitialize(&idInst, (PFNCALLBACK)DdeCallback, 
-							APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0 );
-	if (iReturn!=DMLERR_NO_ERROR)
-	{
-		printf("DDE Initialization Failed: 0x%04x\n", iReturn);
-		Sleep(1500);
-		return 0;
-	}
-
-	//DDE Connect to Server using given AppName and topic.
-	HSZ hszApp, hszTopic;
-	HCONV hConv;
-	hszApp = DdeCreateStringHandle(idInst, szApp, 0);
-	hszTopic = DdeCreateStringHandle(idInst, szTopic, 0);
-	hConv = DdeConnect(idInst, hszApp, hszTopic, NULL);
-	//DdeFreeStringHandle(idInst, hszApp);
-	//DdeFreeStringHandle(idInst, hszTopic);
-	if (hConv == NULL)
-	{
-		printf("DDE Connection Failed.\n");
-		Sleep(1500); DdeUninitialize(idInst);
-		return 0;
-	}
-
-	//Execute commands/requests specific to the DDE Server.
-	
-	DDERequest(idInst, hConv, szItem1); 		
-	
-	while(1)
-	{
-		MSG    msg;
-		BOOL   MsgReturn = GetMessage ( &msg , NULL , 0 , 0 );
-            
-		if(MsgReturn)
-		{
-			TranslateMessage(&msg);
-			DispatchMessage(&msg);			
-		}
-	}
-
-	//DDE Disconnect and Uninitialize.
-	DdeDisconnect(hConv);
-	DdeUninitialize(idInst);
-	*/
-
-	
-
-	/*
-	// EXPERIMENT: 100 AR model generated time series of length of 30 from y_t=0.95*y_(t-1)+0.05*y_(t-2)+0.2*e_t, 
-	// where e_t is normally, student(4) and cauchy distributed are tested using robust AR model, to obtain the 
-	// variance of location parameter estimators and compare it to the classical setup.
-	vector<vector<vector<string>>> string_lists;
-	string_lists.push_back(vector<vector<string>>());
-	string_lists.push_back(vector<vector<string>>());
-	string_lists.push_back(vector<vector<string>>());
-
-	char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"};
-	
-
-	for(int i = 0;i<3;i++)
-	{	
-		ifstream myfile(file_strings[i]);
-		if (myfile.is_open())
-		{
-			while ( myfile.good() )
-			{
-				string line;
-				getline(myfile,line);
-				
-				vector<string> parsed_line;
-				while(line.find(',') != string::npos)
-				{
-					int loc = line.find(',');
-					parsed_line.push_back(line.substr(0,loc));
-					line.erase(0,loc+1);					
-				}				
-
-				string_lists[i].push_back(parsed_line);
-			}
-			myfile.close();
-		}
-	}
-
-	for(int j = 0;j<string_lists.size();j++)
-	{ 
-		
-		for(int i = 0;i<string_lists[j].size()-1;i++)
-		{
-			vector<vec> conditions;
-			//emlig* emliga = new emlig(2);
-			RARX* my_rarx = new RARX(2,30);
-
-			for(int k = 1;k<string_lists[j][i].size();k++)
-			{
-				vec condition;
-				//condition.ins(0,1);				
-				condition.ins(0,string_lists[j][i][k]);				
-				conditions.push_back(condition);
-
-				//cout << "orig:" << condition << endl;
-
-				if(conditions.size()>1)
-				{		
-					conditions[k-2].ins(0,string_lists[j][i][k]);
-					
-				}
-
-				if(conditions.size()>2)
-				{
-					conditions[k-3].ins(0,string_lists[j][i][k]);
-
-					//cout << "modi:" << conditions[k-3] << endl;
-
-					my_rarx->bayes(conditions[k-3]);
-
-					
-					//if(k>5)
-					//{
-					//	cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
-					//}
-					
-				}				
-				
-			}
-
-			//emliga->step_me(0);
-			/*
-			ofstream myfile;
-			myfile.open("c:\\robust_ar1.txt",ios::app);
-			myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";";
-			myfile.close();
-
-			myfile.open("c:\\robust_ar2.txt",ios::app);
-			myfile << emliga->minimal_vertex->get_coordinates()[1] << ";";
-			myfile.close();
-			
-
-			cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
-			cout << "Step: " << i << endl;
-		}
-
-		cout << "One experiment finished." << endl;
-
-		ofstream myfile;
-		myfile.open("c:\\robust_ar1.txt",ios::app);
-		myfile << endl;
-		myfile.close();
-
-		myfile.open("c:\\robust_ar2.txt",ios::app);
-		myfile << endl;
-		myfile.close();
-	}*/    
+int main ( int argc, char* argv[] ) 
+{
 	
 	// EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from 
@@ -444,5 +274,5 @@
 	vector<vector<string>> strings;
 
-	char* file_string = "c:\\ar_normal_single"; // "c:\\dataTYClosePercDiff"; // 
+	char* file_string = "c:\\ar_normal_single"; // "c:\\dataTYClosePercDiff"; //  
 
 	char dfstring[80];
@@ -461,5 +291,5 @@
 			while(line.find(',') != string::npos) //zmenil som ciarku za medzeru
 			{
-				line.erase(0,1); // toto som sem pridal
+				//line.erase(0,1); // toto som sem pridal
 				int loc2 = line.find('\n');
 				int loc  = line.find(',');
@@ -484,10 +314,10 @@
 	for(set<set<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++)
 	{// prechadza rozne typy kanalov, a poctu regresorov
-		for(int window_size = 15;window_size < 16;window_size++)
-		{
-			models.push_back(new model((*model_type),true,true,window_size,0,&data_matrix));   // to su len konstruktory, len inicializujeme rozne typy
-			models.push_back(new model((*model_type),false,true,window_size,0,&data_matrix));
+		for(int window_size = 50;window_size < 51;window_size++)
+		{
+			//models.push_back(new model((*model_type),true,true,window_size,0,&data_matrix));   // to su len konstruktory, len inicializujeme rozne typy
+			//models.push_back(new model((*model_type),false,true,window_size,0,&data_matrix));
 			models.push_back(new model((*model_type),true,false,window_size,0,&data_matrix));
-			models.push_back(new model((*model_type),false,false,window_size,0,&data_matrix));		
+			//models.push_back(new model((*model_type),false,false,window_size,0,&data_matrix));		
 		}
 
@@ -498,4 +328,10 @@
 	mat result_lognc;
 	// mat result_preds;
+
+	ofstream myfilew;
+	char fstring[80];					
+	strcpy(fstring,file_string);
+	strcat(fstring,"lognc.txt");
+	//strcat(fstring,"preds.txt");
 
 	for(int time = max_model_order;time<data_matrix.cols();time++) //time<data_matrix.cols() 
@@ -513,5 +349,5 @@
 			if((*model_ref)->my_rarx!=NULL)	//vklada normalizacnz faktor do cur_res_lognc
 			{
-				cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_rarx->posterior->log_nc);
+				cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_rarx->posterior->log_nc);				
 			}
 			else
@@ -520,10 +356,26 @@
 			}
 
-			// pair<vec,vec> predictions = (*model_ref)->predict(200,time,&LapRNG);
-
-			// preds.ins(preds.size(),(predictions.first*predictions.second)/(predictions.first*ones(predictions.first.size())));
-			// preds.ins(0,data_matrix.get(0,time+1));
-
-		}
+			pair<vec,vec> predictions = (*model_ref)->predict(20,time,&LapRNG);
+
+			cout << predictions.first << endl << predictions.second << endl;
+
+			double avg_prediction = (predictions.first*predictions.second)/(predictions.first*ones(predictions.first.size()));
+
+			(*model_ref)->predictions.ins((*model_ref)->predictions.size(),avg_prediction);
+			
+			/*
+			myfilew.open(fstring,ios::app);
+			myfilew << avg_prediction << ",";
+			myfilew.close();
+			*/
+
+			//preds.ins(0,data_matrix.get(0,time+1));
+		}
+
+		/*
+		myfilew.open(fstring,ios::app);
+		myfilew << data_matrix.get(0,time+1) << endl;
+		myfilew.close();
+		*/
 
 		result_lognc.ins_col(result_lognc.cols(),cur_res_lognc);
@@ -531,306 +383,59 @@
 
 		// cout << "Updated." << endl;
-	
-		/*
-		vector<vec> conditions;
-		//emlig* emliga = new emlig(2);
-		RARX* my_rarx = new RARX(2,10,false);
-		
-		
-		mat V0 = 0.0001 * eye ( 3 );
-		ARX* my_arx = new ARX(0.85);
-		my_arx->set_statistics ( 1, V0 ); //nu is default (set to have finite moments)
-		my_arx->set_constant ( false );
-		my_arx->validate();
-		
-
-		for(int k = 1;k<strings[j].size();k++)
-		{
-			vec condition;
-			//condition.ins(0,1);				
-			condition.ins(0,strings[j][k]);				
-			conditions.push_back(condition);
-
-			//cout << "orig:" << condition << endl;
-
-			if(conditions.size()>1)
-			{		
-				conditions[k-2].ins(0,strings[j][k]);
+
+
+		
+							
+
+		myfilew.open(fstring,ios::app);
+		
+		// myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
+		
+		if(time == max_model_order)
+		{ 
+			for(int i = 0;i<cur_res_lognc.size();i++)
+			{
+				for(set<pair<int,int>>::iterator ar_ref = models[i]->ar_components.begin();ar_ref != models[i]->ar_components.end();ar_ref++)
+				{
+					myfilew << (*ar_ref).second << (*ar_ref).first;							
+				}
+
+				myfilew << ".";
+
+				if(models[i]->my_arx == NULL)
+				{
+					myfilew << "1";
+				}
+				else
+				{
+					myfilew << "0";
+				}
 					
-			}
-
-			if(conditions.size()>2)
-			{
-				conditions[k-3].ins(0,strings[j][k]);
-
-				// cout << "Condition:" << conditions[k-3] << endl;
-
-				my_rarx->bayes(conditions[k-3]);
-				//my_rarx->posterior->step_me(1);
-				
-				vec cond_vec;
-				cond_vec.ins(0,conditions[k-3][0]);
-				
-				my_arx->bayes(cond_vec,conditions[k-3].right(2));
-					
-				/*
-				if(k>8)
-				{
-					//my_rarx->posterior->step_me(0);
-
-					//mat samples = my_rarx->posterior->sample_mat(10);
-
-					pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(1000);
-
-					//cout << imp_samples.first << endl;
-					
-					vec sample_prediction;
-					vec averaged_params = zeros(imp_samples.second.rows());
-					for(int t = 0;t<imp_samples.first.size();t++)
-					{
-						vec lap_sample = conditions[k-3].left(2);
-						//lap_sample.ins(lap_sample.size(),1.0);
-						
-						lap_sample.ins(0,LapRNG());
-
-						sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t));
-
-						averaged_params += imp_samples.first[t]*imp_samples.second.get_col(t);
-					}
-
-					averaged_params = averaged_params*(1/(imp_samples.first*ones(imp_samples.first.size())));
-
-					// cout << "Averaged estimated parameters: " << averaged_params << endl;
-					
-					vec sample_pow = sample_prediction;					
-					
-					// cout << sample_prediction << endl;
-					vec poly_coefs;
-					double prediction;
-					bool stop_iteration = false;
-					int en = 1;
-					do
-					{
-						double poly_coef = imp_samples.first*sample_pow/(imp_samples.first*ones(imp_samples.first.size()));
-
-						if(en==1)
-						{
-							prediction = poly_coef;
-						}
-
-						poly_coef = poly_coef*en*fact(utility_constant-2+en)/fact(utility_constant-2);
-
-						if(abs(poly_coef)>numeric_limits<double>::epsilon())
-						{
-							sample_pow = elem_mult(sample_pow,sample_prediction);
-							poly_coefs.ins(0,pow(-1.0,en+1)*poly_coef);
-						}
-						else
-						{
-							stop_iteration = true;
-						}
-						
-						en++;
-
-						if(en>20)
-						{
-							stop_iteration = true;
-						}
-					}
-					while(!stop_iteration);
-
-					/*
-					ofstream myfile_coef;						
-
-					myfile_coef.open("c:\\coefs.txt",ios::app);
-					
-					for(int t = 0;t<poly_coefs.size();t++)
-					{
-						myfile_coef << poly_coefs[t] << ",";					
-					}
-
-					myfile_coef << endl;
-					myfile_coef.close();
-					*/
-
-					//cout << "Coefficients: " << poly_coefs << endl;
-										
-					/*
-					vec bas_coef = vec("1.0 2.0 -8.0");
-					cout << "Coefs: " << bas_coef << endl;
-					cvec actions2 = roots(bas_coef);
-					cout << "Roots: " << actions2 << endl;
-					*/
-					
-				    /*
-
-					cvec actions = roots(poly_coefs);
-					
-
-					bool is_max = false;
-					for(int t = 0;t<actions.size();t++)
-					{
-						if(actions[t].imag() == 0)
-						{
-							double second_derivative = 0;
-							for(int q = 1;q<poly_coefs.size();q++)
-							{
-								second_derivative+=poly_coefs[q]*pow(actions[t].real(),q-1)*q;
-							}
-
-							if(second_derivative<0)
-							{
-								cout << "Action:" << actions[t].real() << endl;
-
-								is_max = true;
-							}
-						}
-					}
-
-					if(!is_max)
-					{
-						cout << "No maximum." << endl;
-					}
-
-					// cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl;
-
-					/*
-					double prediction = 0;
-					for(int s = 1;s<samples.rows();s++)
-					{
-						
-						double avg_parameter = imp_samples.get_row(s)*ones(samples.cols())/samples.cols();
-
-						prediction += avg_parameter*conditions[k-3][s-1];
-
-						
-						
-						/*
-						ofstream myfile;
-						char fstring[80];
-						strcpy(fstring,file_strings[j]);
-
-						char es[5];
-						strcat(fstring,itoa(s,es,10));
-
-						strcat(fstring,"_res.txt");
-						
-
-						myfile.open(fstring,ios::app);
-						
-						//myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
-						myfile << avg_parameter;
-						
-						if(k!=strings[j].size()-1)
-						{
-							myfile << ",";
-						}
-						else
-						{
-							myfile << endl;
-						}
-						myfile.close();
-						*/
-
-					
-					//}
-
-					// cout << "Prediction: "<< prediction << endl;
-					/*
-					enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2));
-					double prediction2 = pred_mat->mean()[0];
-					*/
-
-					
-					ofstream myfile;
-					char fstring[80];					
-					strcpy(fstring,file_string);
-					
-					strcat(fstring,"lognc.txt");					
-
-					myfile.open(fstring,ios::app);
-					
-					// myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
-					
-					if(time == max_model_order)
-					{ 
-						for(int i = 0;i<cur_res_lognc.size();i++)
-						{
-							for(set<pair<int,int>>::iterator ar_ref = models[i]->ar_components.begin();ar_ref != models[i]->ar_components.end();ar_ref++)
-							{
-								myfile << (*ar_ref).second << (*ar_ref).first;							
-							}
-
-							myfile << ".";
-
-							if(models[i]->my_arx == NULL)
-							{
-								myfile << "1";
-							}
-							else
-							{
-								myfile << "0";
-							}
-								
-							if(models[i]->has_constant)
-							{
-								myfile << "1";
-							}
-							else
-							{
-								myfile << "0";
-							}
-
-							myfile << ",";
-						}
-
-						myfile << endl;
-					}
-					
-					for(int i = 0;i<cur_res_lognc.size();i++)
-					{
-						myfile << cur_res_lognc[i] << ' ';//zmenil som ciarku ze medzeru
-					}
-
-					myfile << endl;				
-					
-					myfile.close();
-			}
-					/*
-					myfile.open(f2string,ios::app);
-					myfile << prediction2;
-					
-					if(k!=strings[j].size()-1)
-					{
-						myfile << ",";
-					}
-					else
-					{
-						myfile << endl;
-					}
-					myfile.close();
-					//*//*
-
-				}					
-			}	*/
-			
-			//emliga->step_me(0);
-			/*
-			ofstream myfile;
-			myfile.open("c:\\robust_ar1.txt",ios::app);
-			myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";";
-			myfile.close();
-
-			myfile.open("c:\\robust_ar2.txt",ios::app);
-			myfile << emliga->minimal_vertex->get_coordinates()[1] << ";";
-			myfile.close();
-			
-
-			cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
-			cout << "Step: " << i << endl;*/
-		//}
-
-
-	//}
+				if(models[i]->has_constant)
+				{
+					myfilew << "1";
+				}
+				else
+				{
+					myfilew << "0";
+				}
+
+				myfilew << ",";
+			}
+
+			myfilew << endl;
+		}
+		
+		for(int i = 0;i<cur_res_lognc.size();i++)
+		{
+			myfilew << cur_res_lognc[i] << ' ';//zmenil som ciarku ze medzeru
+		}
+
+		myfilew << endl;				
+		
+		myfilew.close();
+		
+}
+	
 
 
