2008-09-30 00:02:31 +01:00
|
|
|
%
|
|
|
|
% The world famous EM algorithm, in a nutshell
|
|
|
|
%
|
|
|
|
|
2008-10-22 00:44:02 +01:00
|
|
|
:- module(clpbn_em, [em/5]).
|
2008-09-30 00:02:31 +01:00
|
|
|
|
|
|
|
:- use_module(library(lists),
|
|
|
|
[append/3]).
|
|
|
|
|
2008-10-22 00:44:02 +01:00
|
|
|
:- use_module(library(clpbn),
|
2008-11-01 11:52:54 +00:00
|
|
|
[clpbn_init_solver/5,
|
|
|
|
clpbn_run_solver/4,
|
|
|
|
clpbn_flag/2]).
|
2008-10-22 00:44:02 +01:00
|
|
|
|
|
|
|
:- use_module(library('clpbn/dists'),
|
|
|
|
[get_dist_domain_size/2,
|
|
|
|
empty_dist/2,
|
2008-11-01 11:52:54 +00:00
|
|
|
dist_new_table/2,
|
2008-11-01 21:09:08 +00:00
|
|
|
get_dist_key/2,
|
|
|
|
randomise_all_dists/0,
|
|
|
|
uniformise_all_dists/0]).
|
2008-10-22 00:44:02 +01:00
|
|
|
|
2008-10-23 22:17:45 +01:00
|
|
|
:- use_module(library('clpbn/connected'),
|
|
|
|
[clpbn_subgraphs/2]).
|
|
|
|
|
2008-09-30 00:02:31 +01:00
|
|
|
:- use_module(library('clpbn/learning/learn_utils'),
|
|
|
|
[run_all/1,
|
|
|
|
clpbn_vars/2,
|
2008-10-22 00:44:02 +01:00
|
|
|
normalise_counts/2,
|
2008-10-31 15:11:27 +00:00
|
|
|
compute_likelihood/3,
|
|
|
|
soften_sample/2]).
|
2008-10-22 00:44:02 +01:00
|
|
|
|
|
|
|
:- use_module(library(lists),
|
|
|
|
[member/2]).
|
|
|
|
|
|
|
|
:- use_module(library(matrix),
|
|
|
|
[matrix_add/3,
|
|
|
|
matrix_to_list/2]).
|
|
|
|
|
2008-11-01 11:52:54 +00:00
|
|
|
:- use_module(library(rbtrees),
|
|
|
|
[rb_new/1,
|
|
|
|
rb_insert/4,
|
|
|
|
rb_lookup/3]).
|
|
|
|
|
2008-10-23 22:17:45 +01:00
|
|
|
:- use_module(library('clpbn/utils'),
|
|
|
|
[
|
|
|
|
check_for_hidden_vars/3,
|
|
|
|
sort_vars_by_key/3]).
|
2008-10-22 00:44:02 +01:00
|
|
|
|
|
|
|
:- meta_predicate em(:,+,+,-,-), init_em(:,-).
|
2008-09-30 00:02:31 +01:00
|
|
|
|
|
|
|
em(Items, MaxError, MaxIts, Tables, Likelihood) :-
|
|
|
|
init_em(Items, State),
|
2008-10-22 00:44:02 +01:00
|
|
|
em_loop(0, 0.0, State, MaxError, MaxIts, Likelihood, Tables).
|
2008-09-30 00:02:31 +01:00
|
|
|
|
|
|
|
% This gets you an initial configuration. If there is a lot of evidence
|
|
|
|
% tables may be filled in close to optimal, otherwise they may be
|
|
|
|
% close to uniform.
|
|
|
|
% it also gets you a run for random variables
|
2008-10-22 00:44:02 +01:00
|
|
|
|
|
|
|
% state collects all Info we need for the EM algorithm
|
|
|
|
% it includes the list of variables without evidence,
|
|
|
|
% the list of distributions for which we want to compute parameters,
|
|
|
|
% and more detailed info on distributions, namely with a list of all instances for the distribution.
|
2008-11-01 11:52:54 +00:00
|
|
|
init_em(Items, state( AllDists, AllDistInstances, MargVars, SolverVars)) :-
|
2008-09-30 00:02:31 +01:00
|
|
|
run_all(Items),
|
2008-11-01 21:09:08 +00:00
|
|
|
% randomise_all_dists,
|
|
|
|
uniformise_all_dists,
|
2008-10-22 00:44:02 +01:00
|
|
|
attributes:all_attvars(AllVars0),
|
2008-10-23 22:17:45 +01:00
|
|
|
sort_vars_by_key(AllVars0,AllVars1,[]),
|
2008-10-22 00:44:02 +01:00
|
|
|
% remove variables that do not have to do with this query.
|
2008-10-23 22:17:45 +01:00
|
|
|
check_for_hidden_vars(AllVars1, AllVars1, AllVars),
|
2008-10-22 00:44:02 +01:00
|
|
|
different_dists(AllVars, AllDists, AllDistInstances, MargVars),
|
2008-11-01 11:52:54 +00:00
|
|
|
clpbn_flag(em_solver, Solver),
|
|
|
|
clpbn_init_solver(Solver, MargVars, AllVars, _, SolverVars).
|
2008-09-30 00:02:31 +01:00
|
|
|
|
|
|
|
% loop for as long as you want.
|
2008-10-22 00:44:02 +01:00
|
|
|
em_loop(Its, Likelihood0, State, MaxError, MaxIts, LikelihoodF, FTables) :-
|
|
|
|
estimate(State, LPs),
|
|
|
|
maximise(State, Tables, LPs, Likelihood),
|
2008-10-31 15:11:27 +00:00
|
|
|
writeln(Likelihood:Likelihood0:Tables),
|
2008-09-30 00:02:31 +01:00
|
|
|
(
|
2008-10-22 00:44:02 +01:00
|
|
|
(
|
2008-11-02 15:58:29 +00:00
|
|
|
abs((Likelihood - Likelihood0)/Likelihood) < MaxError
|
2008-10-22 00:44:02 +01:00
|
|
|
;
|
2008-09-30 00:02:31 +01:00
|
|
|
Its == MaxIts
|
2008-10-22 00:44:02 +01:00
|
|
|
)
|
2008-09-30 00:02:31 +01:00
|
|
|
->
|
2008-10-22 00:44:02 +01:00
|
|
|
ltables(Tables, FTables),
|
2008-09-30 00:02:31 +01:00
|
|
|
LikelihoodF = Likelihood
|
|
|
|
;
|
|
|
|
Its1 is Its+1,
|
2008-10-22 00:44:02 +01:00
|
|
|
em_loop(Its1, Likelihood, State, MaxError, MaxIts, LikelihoodF, FTables)
|
2008-09-30 00:02:31 +01:00
|
|
|
).
|
|
|
|
|
2008-10-22 00:44:02 +01:00
|
|
|
ltables([], []).
|
2008-11-01 11:52:54 +00:00
|
|
|
ltables([Id-T|Tables], [Key-LTable|FTables]) :-
|
2008-10-22 00:44:02 +01:00
|
|
|
matrix_to_list(T,LTable),
|
2008-11-01 11:52:54 +00:00
|
|
|
get_dist_key(Id, Key),
|
2008-10-22 00:44:02 +01:00
|
|
|
ltables(Tables, FTables).
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-09-30 00:02:31 +01:00
|
|
|
% collect the different dists we are going to learn next.
|
2008-10-22 00:44:02 +01:00
|
|
|
different_dists(AllVars, AllDists, AllInfo, MargVars) :-
|
|
|
|
all_dists(AllVars, Dists0),
|
2008-09-30 00:02:31 +01:00
|
|
|
sort(Dists0, Dists1),
|
2008-11-01 11:52:54 +00:00
|
|
|
group(Dists1, AllDists, AllInfo, MargVars0, []),
|
|
|
|
sort(MargVars0, MargVars).
|
2008-10-22 00:44:02 +01:00
|
|
|
|
|
|
|
all_dists([], []).
|
|
|
|
all_dists([V|AllVars], [i(Id, [V|Parents], Cases, Hiddens)|Dists]) :-
|
|
|
|
clpbn:get_atts(V, [dist(Id,Parents)]),
|
|
|
|
generate_hidden_cases([V|Parents], CompactCases, Hiddens),
|
|
|
|
uncompact_cases(CompactCases, Cases),
|
|
|
|
all_dists(AllVars, Dists).
|
|
|
|
|
|
|
|
generate_hidden_cases([], [], []).
|
|
|
|
generate_hidden_cases([V|Parents], [P|Cases], Hiddens) :-
|
|
|
|
clpbn:get_atts(V, [evidence(P)]), !,
|
|
|
|
generate_hidden_cases(Parents, Cases, Hiddens).
|
|
|
|
generate_hidden_cases([V|Parents], [Cases|MoreCases], [V|Hiddens]) :-
|
|
|
|
clpbn:get_atts(V, [dist(Id,_)]),
|
|
|
|
get_dist_domain_size(Id, Sz),
|
|
|
|
gen_cases(0, Sz, Cases),
|
|
|
|
generate_hidden_cases(Parents, MoreCases, Hiddens).
|
|
|
|
|
|
|
|
gen_cases(Sz, Sz, []) :- !.
|
|
|
|
gen_cases(I, Sz, [I|Cases]) :-
|
|
|
|
I1 is I+1,
|
|
|
|
gen_cases(I1, Sz, Cases).
|
|
|
|
|
|
|
|
uncompact_cases(CompactCases, Cases) :-
|
|
|
|
findall(Case, is_case(CompactCases, Case), Cases).
|
|
|
|
|
|
|
|
is_case([], []).
|
|
|
|
is_case([A|CompactCases], [A|Case]) :-
|
|
|
|
integer(A), !,
|
|
|
|
is_case(CompactCases, Case).
|
|
|
|
is_case([L|CompactCases], [C|Case]) :-
|
|
|
|
member(C, L),
|
|
|
|
is_case(CompactCases, Case).
|
|
|
|
|
|
|
|
group([], [], []) --> [].
|
|
|
|
group([i(Id,Ps,Cs,[])|Dists1], [Id|Ids], [Id-[i(Id,Ps,Cs,[])|Extra]|AllInfo]) --> !,
|
|
|
|
same_id(Dists1, Id, Extra, Rest),
|
|
|
|
group(Rest, Ids, AllInfo).
|
|
|
|
group([i(Id,Ps,Cs,Hs)|Dists1], [Id|Ids], [Id-[i(Id,Ps,Cs,Hs)|Extra]|AllInfo]) -->
|
|
|
|
[Hs],
|
2008-09-30 00:02:31 +01:00
|
|
|
same_id(Dists1, Id, Extra, Rest),
|
2008-10-22 00:44:02 +01:00
|
|
|
group(Rest, Ids, AllInfo).
|
2008-09-30 00:02:31 +01:00
|
|
|
|
2008-10-22 00:44:02 +01:00
|
|
|
same_id([i(Id,Vs,Cases,[])|Dists1], Id, [i(Id, Vs, Cases, [])|Extra], Rest) --> !,
|
2008-09-30 00:02:31 +01:00
|
|
|
same_id(Dists1, Id, Extra, Rest).
|
2008-10-22 00:44:02 +01:00
|
|
|
same_id([i(Id,Vs,Cases,Hs)|Dists1], Id, [i(Id, Vs, Cases, Hs)|Extra], Rest) --> !,
|
|
|
|
[Hs],
|
|
|
|
same_id(Dists1, Id, Extra, Rest).
|
|
|
|
same_id(Dists, _, [], Dists) --> [].
|
|
|
|
|
|
|
|
|
2008-11-01 11:52:54 +00:00
|
|
|
compact_mvars([], []).
|
|
|
|
compact_mvars([X1,X2|MargVars], CMVars) :- X1 == X2, !,
|
|
|
|
compact_mvars([X2|MargVars], CMVars).
|
|
|
|
compact_mvars([X|MargVars], [X|CMVars]) :- !,
|
|
|
|
compact_mvars(MargVars, CMVars).
|
|
|
|
|
|
|
|
estimate(state(_, _, Margs, SolverState), LPs) :-
|
|
|
|
clpbn_flag(em_solver, Solver),
|
|
|
|
clpbn_run_solver(Solver, Margs, LPs, SolverState).
|
|
|
|
|
|
|
|
maximise(state(_,DistInstances,MargVars,_), Tables, LPs, Likelihood) :-
|
|
|
|
rb_new(MDistTable0),
|
|
|
|
create_mdist_table(MargVars,LPs,MDistTable0,MDistTable),
|
|
|
|
compute_parameters(DistInstances, Tables, MDistTable, 0.0, Likelihood).
|
|
|
|
|
|
|
|
create_mdist_table([],[],MDistTable,MDistTable).
|
|
|
|
create_mdist_table([Vs|MargVars],[Ps|LPs],MDistTable0,MDistTable) :-
|
|
|
|
rb_insert(MDistTable0, Vs, Ps, MDistTableI),
|
|
|
|
create_mdist_table(MargVars, LPs, MDistTableI ,MDistTable).
|
2008-10-22 00:44:02 +01:00
|
|
|
|
2008-11-01 11:52:54 +00:00
|
|
|
compute_parameters([], [], _, Lik, Lik).
|
|
|
|
compute_parameters([Id-Samples|Dists], [Id-NewTable|Tables], MDistTable, Lik0, Lik) :-
|
2008-10-22 00:44:02 +01:00
|
|
|
empty_dist(Id, Table0),
|
2008-11-01 11:52:54 +00:00
|
|
|
add_samples(Samples, Table0, MDistTable),
|
2008-10-31 15:11:27 +00:00
|
|
|
soften_sample(Table0, SoftenedTable),
|
|
|
|
normalise_counts(SoftenedTable, NewTable),
|
2008-10-22 00:44:02 +01:00
|
|
|
compute_likelihood(Table0, NewTable, DeltaLik),
|
|
|
|
dist_new_table(Id, NewTable),
|
|
|
|
NewLik is Lik0+DeltaLik,
|
2008-11-01 11:52:54 +00:00
|
|
|
compute_parameters(Dists, Tables, MDistTable, NewLik, Lik).
|
2008-10-22 00:44:02 +01:00
|
|
|
|
2008-11-01 11:52:54 +00:00
|
|
|
add_samples([], _, _).
|
|
|
|
add_samples([i(_,_,[Case],[])|Samples], Table, MDistTable) :- !,
|
2008-10-22 00:44:02 +01:00
|
|
|
matrix_add(Table,Case,1.0),
|
2008-11-01 11:52:54 +00:00
|
|
|
add_samples(Samples, Table, MDistTable).
|
|
|
|
add_samples([i(_,_,Cases,Hiddens)|Samples], Table, MDistTable) :-
|
|
|
|
rb_lookup(Hiddens, Ps, MDistTable),
|
2008-10-22 00:44:02 +01:00
|
|
|
run_sample(Cases, Ps, Table),
|
2008-11-01 11:52:54 +00:00
|
|
|
add_samples(Samples, Table, MDistTable).
|
2008-10-22 00:44:02 +01:00
|
|
|
|
|
|
|
run_sample([], [], _).
|
|
|
|
run_sample([C|Cases], [P|Ps], Table) :-
|
|
|
|
matrix_add(Table, C, P),
|
|
|
|
run_sample(Cases, Ps, Table).
|
2008-09-30 00:02:31 +01:00
|
|
|
|
|
|
|
|