%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MATLABArsenal %%%%%%%%%%%%%%%%%%%%%%%%%%%%
Arsenal('classify -t iris.data -sf 1 -- cross_validate -t 10 -- WekaClassify -MultiClassWrapper 0 -- bayes.NaiveBayes');
% O de forma equivalente
Arsenal('classify -t iris.data -sf 1 -- cross_validate -t 10 --NaiveBayes');
% Con weather.data, validar con el conjunto de entrenamiento.
Arsenal('classify -t weather.data --  Test_File_Validate -t weather.data --NaiveBayes');
% O de forma equivalente
Arsenal('classify -t weather.data -- Train_Only -m weather.model --NaiveBayes');
Arsenal('classify -t weather.data -- test_only -m weather.model --NaiveBayes');
Arsenal('classify -t DataExample1.txt -sf 1 -svd 15 -drf DataExample1svd.txt -- cross_validate -t 3 -- kNN_classify -k 3');
Arsenal('classify -t DataExample2.txt -sf 0 -- Train_Test_Validate -t 100 -- Train_Test_Multiple_Class -- SVM_Light -Kernel 2 -KernelParam 0.01 -CostFactor 3');
Arsenal('classify -t DataExample2.train.txt -sf 0 -- Test_File_Validate -t DataExample2.test.txt -- MCAdaBoostM1 -- WekaClassify -MultiClassWrapper 0 -- trees.J48');
Arsenal('classify -t DataExample2.txt -sf 0 -oflag w -- Train_Test_Validate -t 100 -- MCWithMultiFSet -Voting -Separator 1,120,121,150,154,225 -- NaiveBayes');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% BNT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio comparacion clasificadores
% 1.- Creacion clasificadores: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=3; C=1;A=2;B=3;
node_sizes = [2 3 2];
onodes = [2 3];
%CLASIFICADOR 1 (NAIVE BAYES)
dag1 = zeros(N,N);
dag1(C,[A,B])=1;
bnet1 = mk_bnet(dag1,node_sizes, 'names',{'C','A','B'},'observed',onodes);
bnet1.CPD{C} = tabular_CPD(bnet1,C,[0.5 0.5]);
bnet1.CPD{A} = tabular_CPD(bnet1,A,[0.3 0.2 0.4 0.6 0.3 0.2]);
bnet1.CPD{B} = tabular_CPD(bnet1,B,[0.4 0.5 0.6 0.5]);
disp('Parametros NB'); showCPT(bnet1);
%CLASIFICADOR 2
dag2 = zeros(N,N);
dag2(C,[A,B])=1;dag2(A,B) = 1;
bnet2 = mk_bnet(dag2,node_sizes, 'names',{'C','A','B'},'observed',onodes);
bnet2.CPD{C} = tabular_CPD(bnet2,C,[0.5 0.5]);
bnet2.CPD{A} = tabular_CPD(bnet2,A,[0.3 0.2 0.4 0.6 0.3 0.2]);
bnet2.CPD{B} = tabular_CPD(bnet2,B,[0.5 0.6 0.7 0.3 0.4 0.1 
                                    0.5 0.4 0.3 0.7 0.6 0.9]);
disp('Parametros NB con enlace extra'); showCPT(bnet2);

subplot(1,2,1),draw_graph(dag1);title('Clasificador 1');
subplot(1,2,2),draw_graph(dag2);title('Clasificador 2');
% 2.- Muestreo: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:1000
    s = sample_bnet(bnet1);
    data(1,i)=s{3};
    data(2,i)=s{2};
    data(3,i)=s{1};
end
%Recalculo de parametros para C1 y C2
bnet1m = learn_params(bnet1, data);
bnet2m = learn_params(bnet2, data);
%p(c=2|a=1,b=3)?
%En el clasificador 1 (NB)
engine = jtree_inf_engine(bnet1);
evidence = cell(1,N);
evidence{A} = 1; evidence{B} = 2;
[engine, loglik] = enter_evidence(engine, evidence);
marg = marginal_nodes(engine, C);
pc1=marg.T(2)
%En el clasificador 2 (NB)
engine = jtree_inf_engine(bnet2);
evidence = cell(1,N);
evidence{A} = 1; evidence{B} = 2;
[engine, loglik] = enter_evidence(engine, evidence);
marg = marginal_nodes(engine, C);
pc2=marg.T(2)

% 3.- Comparacion tasas de acierto: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cor = 0;
for i=1:100
    s = sample_bnet(bnet1);
    evidence = s'; evidence{C} = [];
    engine = jtree_inf_engine(bnet1);
    [engine, loglik] = enter_evidence(engine, evidence);
    marg = marginal_nodes(engine, C);
    [pr v]= max(marg.T);
    if v==s{1}
        cor=cor+1;
    end
end
accuracy1 = cor/100
cor = 0;
for i=1:100
    s = sample_bnet(bnet1);
    evidence = s'; evidence{C} = [];
    engine = jtree_inf_engine(bnet2);
    [engine, loglik] = enter_evidence(engine, evidence);
    marg = marginal_nodes(engine, C);
    [pr v]= max(marg.T);
    if v==s{1}
        cor=cor+1;
    end
end
accuracy2 = cor/100

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SLP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% NB %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; close all;
data=load('weather.data');
class=5;
dag = mk_naive_struct(5, class);
%Con BNT:
%dag = zeros(5);
%dag(5, [1,2,3,4]) = 1;
ns= [3 1 1 2 2];
bnet = mk_bnet(dag, ns, 'discrete', [1 4 5]);
bnet.CPD{5}=tabular_CPD(bnet, 5);
bnet.CPD{1}=tabular_CPD(bnet, 1);
bnet.CPD{2}=gaussian_CPD(bnet,2);
bnet.CPD{3}=gaussian_CPD(bnet,3);
bnet.CPD{4}=tabular_CPD(bnet, 4);
bnet = learn_params(bnet,data');
%Ojo!! En classification_evaluation (SLP), linea 13 cambiar rationplus por ratioplus 
%Intervalo de confianza del 95%:
[ratio, ratiominus, ratioplus] = classification_evaluation(bnet, data', class)

% TAN Y FAN %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; close all;
fprintf('\n=== Clasificadores FAN y TAN con SLP \n');
data=load('weather.data'); 
ns=max(data); class=5;
tmp=cputime;
dag = learn_struct_fan2(data', class, ns);
fprintf('Tiempo construccion FAN: %f\n',cputime-tmp);
tmp=cputime;
dag2=learn_struct_tan(data', class,1, ns,'mutual_info');
fprintf('Tiempo construccion TAN (inf. mutua): %f\n',cputime-tmp);
tmp=cputime;
dag3=learn_struct_tan(data', class,1, ns,'bic');
fprintf('Tiempo construccion TAN (bic): %f\n',cputime-tmp);
figure; 
subplot(1,3,1),draw_graph(dag);title('FAN: inf.mutua cond.');
subplot(1,3,2),draw_graph(dag2);title('TAN: inf.mutua cond.');
subplot(1,3,3),draw_graph(dag3);title('TAN: medida BIC');


