#include "Includes/posteriors.ibf";

_moment_matrix = {_sites, 5};

for (_counter2 = 0; _counter2 < _sites; _counter2 = _counter2 + 1)
{
	_firstmoment  = 0;
	_secondmoment = 0;
	_thirdmoment  = 0;
	
	_max_4_site	  = 0;
	_site_mode	  = 0;
	
	for (_counter = 0; _counter < _classes; _counter = _counter + 1)
	{
		_temp = _MARGINAL_MATRIX_[_counter][_counter2];
		if (_temp > _max_4_site)
		{
			_max_4_site = _temp;
			_site_mode  = _counter;
		}	
		_temp =  _temp * _CATEGORY_VARIABLE_CDF_[1][_counter];
		_firstmoment 	= _firstmoment 	+ _temp;
		_temp = _temp * _CATEGORY_VARIABLE_CDF_[1][_counter];
		_secondmoment	= _secondmoment + _temp;
		_thirdmoment	= _thirdmoment + _temp * _CATEGORY_VARIABLE_CDF_[1][_counter];
	}
	
	_moment_matrix [_counter2][0] = _firstmoment;
	_moment_matrix [_counter2][1] = _secondmoment - _firstmoment^2;
	_moment_matrix [_counter2][2] = Sqrt(_moment_matrix [_counter2][1])/Abs(_firstmoment);	
	_moment_matrix [_counter2][3] = (2*_firstmoment^3-3*_firstmoment*_secondmoment+_thirdmoment)/(_moment_matrix [_counter2][1])^1.5;
	_moment_matrix [_counter2][4] = _CATEGORY_VARIABLE_CDF_[1][_site_mode];
}


_labels = {{"Expectation","Variance", "COV", "Skewness", "Mode", ""}};


_rowLabel = "Site;";
for (_counter2 = 1; _counter2 <= _sites; _counter2 = _counter2 + 1)
{
	_rowLabel = _rowLabel + ";" + _counter2;
}

_labels [5] = _rowLabel;


promptString = "Posterior Moments at Sites for " + _CATEGORY_VARIABLE_ID_;

OpenWindow (CHARTWINDOW,{{promptString}
						   {"_labels"},
						   {"_moment_matrix"},
						   {"None"},
						   {""},
						   {""},
						   {""},
						   {""},
						   {""},
						   {"0"}},
						   "SCREEN_WIDTH-60;SCREEN_HEIGHT-50;30;50");
						   
_labels = 0;
_sites  = 0;
_momentMatrix = 0;
