

generateBayesianPredictionModel(EIs,Model):-
	set(depthLimit,DepthLimit),
	all_singleEIs(DepthLimit,EIs,AllEI_T),%write(' HYPOSESE have been obtained and they are '),nl,print_list(AllEI_T),nl,
	record(AllEI_T,AllT_EI),
	computeTsMatrixes(EIs,AllT_EI,Model). % model is in the form of [Matrix1-T1, Matrix2-T2..]
		
/*
%length(AllT,K), write(K),	
	%write(' HYPOSESE have been obtained and they are '),nl,
	sort(AllT_Matrix,AllT_Matrix_sorted),
	nl,print_list(AllT_Matrix_sorted)
	, told.
*/








/*******************************************************Counting part is here ******************/
% the input to the predicates in this file looks like [EI1-Ts1, EI2-Ts2..]

% both counting and translation into readable clauses
record(EIsTIs,TsEIs):-
	rb_new(Tree0),
	counting(EIsTIs,Tree0,Tree),
	rb_visit(Tree,TIsEIs), write('OK after counting'),%write(TIsEIs),nl,
	maplist(tProcessing,TIsEIs,TsEIs),write('OK after translation'),
	nl.
	%print_list(TsEIs),nl.


% only translation
tProcessing(TI-EIs,T-EIs):-
	tInterpreter(TI,T).

/*% translation and Prior computation
tProcessing(TI-EIs,Prior-T-EIs):-
	tInterpreter(TI,T),
	computePrior(TI,Prior). % no need to normalize
	% Prior = 1. % Essentially no bias on any H 

% double iteration 
computePrior([[]],1).	
computePrior([[BI|ClaI]],Prior):-
	computePrior([ClaI],PrePrior),
	priorP(BI,BPrior),
	Prior is BPrior*PrePrior.
*/


/************************* Counting Theory as a whole ***************************/
counting([],Tree,Tree).
counting([EI-OneEITRecord|All_T_Records],TreeSoFar,Tree):-
	insertOneEI_Ts(OneEITRecord,EI,TreeSoFar,NTreeSoFar),
	counting(All_T_Records,NTreeSoFar,Tree).


insertOneEI_Ts([],EI,Tree,Tree).
insertOneEI_Ts([TI|TIs],EI,TreeSoFar,Tree):-
	(rb_lookup(TI,EI_List,TreeSoFar)->
		%TI is there-> previous example, or the same example(it is important not to redundant the example)
		(EI_List=[EI|_]->		% an efficient trick here(since all T for the same EI are added at the same time) 
						% append(Pre,[EI|Post],EI_List)-- this will continue to find EI, which is in vain
				NTreeSoFar=TreeSoFar;
				rb_update(TreeSoFar,TI,[EI|EI_List],NTreeSoFar)
		);		
		rb_insert(TreeSoFar,TI,[EI],NTreeSoFar)
	),	
	insertOneEI_Ts(TIs,EI,NTreeSoFar,Tree).

/************************* Counting on clauses -- decompose each Theory***************************/



/************************* Compute Matrix ***************************/
% differentiate neg and pos: (1) simple: assuming in order (2) go back to check ex(,1)? ex(,0)?
% representation issue. {(PosSucceed,NegSucceed),(PosFail,NegFail)}

computeTsMatrixes(AllEIs,TsEIs,TsMatrixes):-
	maplist(computeTMatrix(AllEIs),TsEIs,TsMatrixes).

/*
computeTMatrix(T-EIs,Matrixes-T):-
	sepPosNegSucceed(EIs,PosEIs,NegEIs),
	length(PosEIs,PosSucceed),
	length(NegEIs,NegSucceed),
	exNumber(pos,PosTotal),PosFail is PosTotal-PosSucceed,
	exNumber(neg,NegTotal),NegFail is NegTotal-NegSucceed,
	Matrixes={[PosSucceed,NegSucceed],[PosFail,NegFail]}.
*/

computeTMatrix(AllEIs,T-SucceedEIs,Matrixes-T):-
	
	length(AllEIs,TotalExNum),

	sepPN_ID(SucceedEIs,PosEIs,NegEIs),
	length(PosEIs,PosSucceed),
	length(NegEIs,NegSucceed),
	length(SucceedEIs,SucceedExNum),
	PosSucceedLikelihood is PosSucceed/SucceedExNum,
	NegSucceedLikelihood is NegSucceed/SucceedExNum,

	sepPN_ID(AllEIs,AllPosEIs,AllNegEIs),
	length(AllPosEIs,PosTotal),PosFail is PosTotal-PosSucceed,
	length(AllNegEIs,NegTotal),NegFail is NegTotal-NegSucceed,
	FailExNum is TotalExNum-SucceedExNum,
	PosFailLikelihood is PosFail/FailExNum,
	NegFailLikelihood is NegFail/FailExNum,

	Matrixes={[PosSucceedLikelihood,NegSucceedLikelihood],[PosFailLikelihood,NegFailLikelihood]}.





