Index: applications/robust/main.cpp
===================================================================
--- applications/robust/main.cpp (revision 1358)
+++ applications/robust/main.cpp (revision 1361)
@@ -13,7 +13,10 @@
 #include <fstream>
 #include <itpp/itsignal.h>
-#include <windows.h>
-#include "DDEClient.h"
-#include <conio.h>
+#include "windows.h"
+#include "ddeml.h"
+#include "stdio.h"
+
+//#include "DDEClient.h"
+//#include <conio.h>
 
 
@@ -21,67 +24,242 @@
 using namespace bdm;
 
-const int emlig_size = 2;
-const int utility_constant = 5;
+//const int emlig_size = 2;
+//const int utility_constant = 5;
+
+const int max_model_order = 2;
+
+
+
+HDDEDATA CALLBACK DdeCallback(
+	UINT uType,     // Transaction type.
+	UINT uFmt,      // Clipboard data format.
+	HCONV hconv,    // Handle to the conversation.
+	HSZ hsz1,       // Handle to a string.
+	HSZ hsz2,       // Handle to a string.
+	HDDEDATA hdata, // Handle to a global memory object.
+	DWORD dwData1,  // Transaction-specific data.
+	DWORD dwData2)  // Transaction-specific data.
+{
+	return 0;
+}
+
+void DDERequest(DWORD idInst, HCONV hConv, char* szItem)
+{
+	HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0);
+	HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT, 
+									XTYP_REQUEST,TIMEOUT_ASYNC , NULL);
+	if (hData==NULL)
+	{
+		printf("Request failed: %s\n", szItem);
+	}
+
+	if (hData==0)
+	{
+		printf("Request failed: %s\n", szItem);
+	}
+}
+
+class model
+{
+	
+
+public:
+	list<pair<int,int>> ar_components;
+
+	// Best thing would be to inherit the two models from a single souce, this is planned, but now structurally
+	// problematic.
+	RARX* my_rarx;
+	ARXwin* my_arx;
+
+	bool has_constant;
+	int  window_size;
+	int  predicted_channel;
+	mat* data_matrix;
+	
+	model(list<pair<int,int>> ar_components, 
+		  bool robust, 
+		  bool has_constant, 
+		  int window_size, 
+		  int predicted_channel,
+		  mat* data_matrix)
+	{
+		this->ar_components.insert(this->ar_components.begin(),ar_components.begin(),ar_components.end());
+		this->has_constant      = has_constant;
+		this->window_size       = window_size;
+		this->predicted_channel = predicted_channel;
+		this->data_matrix       = data_matrix;
+
+		if(robust)
+		{
+			if(has_constant)
+			{
+				my_rarx = new RARX(ar_components.size()+1,window_size,true);
+				my_arx  = NULL;
+			}
+			else
+			{
+				my_rarx = new RARX(ar_components.size(),window_size,false);
+				my_arx  = NULL;
+			}
+		}
+		else
+		{
+			my_rarx = NULL;
+			my_arx  = new ARXwin();
+			mat V0;
+
+			if(has_constant)
+			{				
+				V0  = 0.0001 * eye(ar_components.size()+2);
+				//V0(0,0) = 0.1;
+				my_arx->set_constant(true);	
+				
+			}
+			else
+			{
+				
+				V0  = 0.0001 * eye(ar_components.size()+1);
+				//V0(0,0) = 0.1;
+				my_arx->set_constant(false);				
+				
+			}
+
+			my_arx->set_statistics(1, V0); 
+			my_arx->set_parameters(window_size);
+			my_arx->validate();
+		}
+	}
+
+	void data_update(int time)
+	{
+		vec data_vector;
+		for(list<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++)
+		{
+			data_vector.ins(data_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second));
+		}
+
+		if(my_rarx!=NULL)
+		{
+			data_vector.ins(0,(*data_matrix).get(predicted_channel,time));
+			my_rarx->bayes(data_vector);
+		}
+		else
+		{
+			vec pred_vec;
+			pred_vec.ins(0,(*data_matrix).get(predicted_channel,time));
+			my_arx->bayes(pred_vec,data_vector);
+		}
+	}
+
+	static list<list<pair<int,int>>> possible_models_recurse(int max_order,int number_of_channels)
+	{
+		list<list<pair<int,int>>> created_model_types;		
+
+		if(max_order == 1)
+		{			
+			for(int channel = 0;channel<number_of_channels;channel++)
+			{
+				list<pair<int,int>> returned_type;
+				returned_type.push_back(pair<int,int>(channel,1));
+				created_model_types.push_back(returned_type);
+			}
+
+			return created_model_types;
+		}
+		else
+		{
+			created_model_types = possible_models_recurse(max_order-1,number_of_channels);
+			list<list<pair<int,int>>> returned_types;
+			
+			for(list<list<pair<int,int>>>::iterator model_ref = created_model_types.begin();model_ref!=created_model_types.end();model_ref++)
+			{				
+				
+				for(int order = 1; order<=max_order; order++)
+				{
+					for(int channel = 0;channel<number_of_channels;channel++)
+					{
+						list<pair<int,int>> returned_type;
+						pair<int,int> new_pair = pair<int,int>(channel,order);
+						if(find((*model_ref).begin(),(*model_ref).end(),new_pair)==(*model_ref).end())
+						{
+							returned_type.insert(returned_type.begin(),(*model_ref).begin(),(*model_ref).end());
+							returned_type.push_back(new_pair);
+							returned_types.push_back(returned_type);							
+						}
+					}
+				}
+			}
+
+			created_model_types.insert(created_model_types.end(),returned_types.begin(),returned_types.end());
+
+			return created_model_types;
+		}		
+	}
+};
+
+
 
 
 int main ( int argc, char* argv[] ) {
 	
-	itpp::Laplace_RNG LapRNG = Laplace_RNG();
-
-	WORD wConvNo;
-	char szType[] = "request";
-	char szData[21]; 
-	char szItem[] = "EURUSD";
-	char szService[] = "MT4";
-	char szTopic[] = "BID";
-	char szFormat[] = "CF_TEXT";
-	DWORD dwTimeout = 0;
-	//char szAccess[] = "string";
-
-	if(!DCInit()) 
-	{
-		cout << "DDE doesn't work." << endl;
-	}
-	else
-	{
-		// The following if-block shows a complete conversation with a
-		// single transaction. You therefore do not need to free any memory
-		// explicitly.
-
-		// connect to server
-		if (!DCConnect(&wConvNo,szService, szTopic)) 
-		{
-			cout << "Couldn't connect DDE." << endl;
-		}
-		else
-		{
-			// do synchronous request transaction, wait max. 1000 ms, 
-			// return data as string
-			if(!DCRequestString(wConvNo,szItem,100000))
-			{
-				cout << "No data available." << endl;
-			}
-			else
-			{
-				if(!DCAsynchTransactionCompleted(wConvNo,DCDA[wConvNo]->dwTransID,true))
-				{
-					cout << "Asynchronous transaction error." << endl;
-				}
-				else
-				{
-				// output data to console if transaction complete
-				// DCDA[wConvNo]->pszData is the pointer to the data string
-					cprintf(DCDA[wConvNo]->pszData);
-					DCFreeDdeMem(wConvNo);
-				}
-			}
-			// end conversation
-			if(!DCDisconnect(wConvNo))
-			{
-				cout << "Couldn't disconnect DDE." << endl;
-			} 
-		}
-	}
-	
+	itpp::Laplace_RNG LapRNG = Laplace_RNG();	
+	
+	/*
+	char szApp[] = "MT4";
+	char szTopic[] = "ASK";	
+	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;
+	}
+
+	/*
+	//Start DDE Server and wait for it to become idle.
+	HINSTANCE hRet = ShellExecute(0, "open", szTopic, 0, 0, SW_SHOWNORMAL);
+	if ((int)hRet < 33)
+	{
+		printf("Unable to Start DDE Server: 0x%04x\n", hRet);
+		Sleep(1500); DdeUninitialize(idInst);
+		return 0;
+	}
+	Sleep(1000);
+	*/
+
+	/*
+	//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); 	
+	
+	//DDE Disconnect and Uninitialize.
+	//DdeDisconnect(hConv);
+	//DdeUninitialize(idInst);
+
+	Sleep(300000);
+	Sleep(3000);
+	*/
+
 	/*
 	// 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, 
@@ -190,4 +368,5 @@
 	}*/
     
+
 	
 	// EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from 
@@ -198,36 +377,70 @@
 	vector<vector<string>> strings;
 
-	char* file_strings[3] = {"c:\\dataADClosePercDiff","c:\\ar_student_single","c:\\ar_cauchy_single"};
-
-	for(int i = 0;i<3;i++)
-	{			
-		char dfstring[80];
-		strcpy(dfstring,file_strings[i]);
-		strcat(dfstring,".txt");
+	char* file_string = "c:\\data"; 
+
+	char dfstring[80];
+	strcpy(dfstring,file_string);
+	strcat(dfstring,".txt");
 		
-		ifstream myfile(dfstring);
-		if (myfile.is_open())
+	
+	mat data_matrix;
+	ifstream myfile(dfstring);
+	if (myfile.is_open())
+	{		
+		string line;
+		while(getline(myfile,line))
 		{			
-			string line;
-			getline(myfile,line);
-				
-			vector<string> parsed_line;
+			vec data_vector;
 			while(line.find(',') != string::npos)
 			{
-				int loc = line.find(',');
-				parsed_line.push_back(line.substr(0,loc));
+				int loc2 = line.find('\n');
+				int loc  = line.find(',');
+				data_vector.ins(data_vector.size(),atof(line.substr(0,loc).c_str()));				
 				line.erase(0,loc+1);					
-			}				
-
-			strings.push_back(parsed_line);
-			
-			myfile.close();
-		}
-	}
-
-	
-	
-	for(int j = 0;j<strings.size();j++)
-	{ 		
+			}
+
+			data_matrix.ins_row(data_matrix.rows(),data_vector);
+		}		
+
+		myfile.close();	
+	}
+	else
+	{
+		cout << "Can't open data file!" << endl;
+	}
+	
+	list<list<pair<int,int>>> model_types = model::possible_models_recurse(max_model_order,data_matrix.rows());
+
+	list<model*> models;
+	for(list<list<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++)
+	{
+		models.push_back(new model((*model_type),true,false,30,0,&data_matrix));
+		models.push_back(new model((*model_type),false,false,30,0,&data_matrix));
+	}
+	
+	mat result_lognc;
+
+	for(int time = max_model_order;20;time++) //time<data_matrix.cols()
+	{ 	
+		vec cur_res_lognc;
+
+		for(list<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++)
+		{
+			(*model_ref)->data_update(time);
+			if((*model_ref)->my_rarx!=NULL)
+			{
+				cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_rarx->posterior->log_nc);
+			}
+			else
+			{
+				cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_arx->posterior().lognc());
+			}
+		}
+
+		result_lognc.ins_col(result_lognc.cols(),cur_res_lognc);
+
+		cout << "Updated." << endl;
+
+		/*
 		vector<vec> conditions;
 		//emlig* emliga = new emlig(2);
@@ -271,5 +484,5 @@
 				my_arx->bayes(cond_vec,conditions[k-3].right(2));
 					
-				
+				/*
 				if(k>8)
 				{
@@ -360,5 +573,5 @@
 					*/
 					
-
+				    /*
 
 					cvec actions = roots(poly_coefs);
@@ -434,23 +647,31 @@
 
 					// 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];
-					char f2string[80];
-					strcpy(fstring,file_strings[j]);
-					strcpy(f2string,fstring);
-
-					strcat(fstring,"pred.txt");
-					strcat(f2string,"2pred.txt");
-					
+					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];
-					myfile << prediction;
+					
+					for(int i = 0;i<cur_res_lognc.size();i++)
+					{
+						myfile << cur_res_lognc[i] << ',';
+					}
+
+					myfile << endl;				
+					
+					myfile.close();
+
+					/*
+					myfile.open(f2string,ios::app);
+					myfile << prediction2;
 					
 					if(k!=strings[j].size()-1)
@@ -463,22 +684,8 @@
 					}
 					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);
@@ -496,5 +703,5 @@
 			cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
 			cout << "Step: " << i << endl;*/
-		}
+		//}
 
 
