/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser 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 LICENSE and COPYING files for more details.
*/

#include "../comparator_stdheaders.h"
#include "RWFEvertsMI.h"

namespace smartcomparator
{
	namespace weights
	{
		RWFEvertsMI::RWFEvertsMI(Matrices::SuperMatrixPtr m, RankBuilderPtr pRankBuilder, double mi_threshold)
			: RWFBase(m, pRankBuilder),
			ContingencyTableWeight(m),
			m_dMIThreshold(mi_threshold)
		{
		}// c-tor

		void RWFEvertsMI::DoWeight(size_t rowindex, Matrices::CompressedVector *v, std::set<size_t> &filteredIndices,
					std::multimap<double, size_t> &val_to_col_no )
		{

			for(std::set<size_t>::iterator col_iter = filteredIndices.begin(); 
					col_iter != filteredIndices.end(); ++col_iter)
			{
				CountContingencyTableLocal(v, rowindex, *col_iter);
				double mi = log(observed[0][0]/expected[0][0]);
				if(mi > m_dMIThreshold)
					val_to_col_no.insert( std::pair<double, size_t> (mi, *col_iter) );
			}// for each non-zero column

		}// DoWeight

	}// namespace weights
}// namespace smartcomparator

