/* Simulatior for Probability Distribution */ % 19 Nov 2002. % 21 Dec 2011. minor bugfix (last/2 was worngly used in gen_histogram/4. why?) :-write('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'),nl, write(' A Simulatior for Probabilities'),nl, write('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'),nl, write('type ``gen_dist(uniform,[0,1],100,P)." or ``menu."'),nl. wn(Z):-write(Z),nl. nw(Z):-nl,write(Z). menu:- nl, tab(2),wn('1. model a distribution.'), tab(2),wn('2. calculate the frequency (i.e., build the histogram).'), tab(2),wn('3. draw a graph of the histogram.'), tab(2),wn('4. exit this menu.'), tab(1),write('input number>'), read(Y), ( ( member([Y,A],[[1,dist],[2,freq],[3,draw],[4,exit]]), write('You selected '),write((Y,(A))),write('. OK? (y/n)'), read(y) ) -> ( (Y=1->set_gen_dist(D,[L,U],_N,_P);true), ((\+var(L),\+var(U))->(G=..[D,L,U],write(G));true), (Y=2->set_gen_histogram(G,_Class,_M,cum);true), (Y=3->draw_histogram;true), (Y=4->!, fail; true) % to exit the menu (modified: Dec 2011) ) ; menu ), menu. select_dist:-select_dist(_). select_dist(N):- nl, tab(2),wn('1. uniform distribution.'), tab(2),wn('2. trianglar distribution.'), tab(2),wn('3. binominal distribution.'), tab(2),wn('4. normal distribution.'), tab(1),write('input number>'), read(Y), ( ( member([Y,A],[[1,uniform],[2,triangle],[3,binominal],[4,normal]]), write('Will you generate a '),write(A),write(' distribution?(y/n)'), read(y) ) ->set_gen_dist(A,_,_,_) ; select_dist(N) ), menu. p(dummy,nul,0,0). p_init:- abolish(p,4), assert(p(dummy,nul,0,0)). sampling_limit(1000). set_gen_dist(X,no,no,abend):- \+ member(X,[uniform]), wn('Not yet.'). set_gen_dist(A,B,N,D):- var(N), write('Type the sample size. '), read(N), set_gen_dist(A,B,N,D). set_gen_dist(_,_,N,abend):- sampling_limit(T), N > T, wn('Too large sample size. Please try a smaller.'). set_gen_dist(A,[L,U],N,D):- %(var(L);var(U)), var(L), write('Type the lower bound. '), read(Y), L is integer(Y), set_gen_dist(A,[L,U],N,D). set_gen_dist(A,[L,U],N,D):- %(var(L);var(U)), var(U), write('Type the upper bound. '), read(Y), U is integer(Y),wn(gen_dist(A,[L,U],N,D)), gen_dist(A,[L,U],N,D). gen_dist(uniform,[L,U],_,abend):- \+ (integer(L),integer(U)),write([L,U]), wn('Each of the lower and upper bounds must be an integer.'). gen_dist(uniform,[L,U],N,ok):- sampling_limit(T), N =< T, integer(U), integer(L), length(X,N), p_init, forall( nth1(K,X,A), ( G=..[p,uniform,[L,U],K,A], B is integer(U - L + 1), A is random(B) + L, %wn(A), assert(G) ) ), trim_stacks, listing(p). set_gen_histogram(_G,_Class,_M,_cum):- \+ (clause(p(X,_,_,_),_),X \= dummy), wn('You have need to create a distribution before do this.'). set_gen_histogram(G,Class,M,cum):- G=..[uniform,L,_U], var(L), write('Type the lower bound. '), read(Y), L is integer(Y), set_gen_histogram(G,Class,M,cum). set_gen_histogram(G,Class,M,cum):- G=..[uniform,_L,U], var(U), write('Type the upper bound. '), read(Y), U is integer(Y), set_gen_histogram(G,Class,M,cum). set_gen_histogram(G,Class,M,cum):- var(Class), write('Input the boundaries as a list.'), read(Y), (length(Y,_)->(Class=Y,gen_histogram(G,Class,M,cum)); fail). gen_histogram(G,Class,M,cum):- abolish(histogram,4), (G=uniform(L,U)->true;(wn('illegal goal format.'),fail)), ((Class=[Lc|_], Lc >= L)->true;(wn('illegal for the lower bound.'),fail)), ((last(Class, Uc), Uc =< U)->true;(wn('illegal for the upper boud.'),fail)), (length(Class,M)->true;(wn('illegal partitioning.'),fail)), bagof(K,A^p(uniform,[L,U],K,A),All), length(All,N),wn(num_of_samples(N)), forall( ( nth1(J,Class,Ub)%,wn(ub(Ub)) ), ( bagof(K1,B^(p(uniform,[L,U],K1,B),B =< Ub),Ks), length(Ks,Freq),%wn(lev(Ks,Freq)), Perc is integer(Freq * 100 / N), assert(histogram(J,Ub,Freq,Perc)) ) ), trim_stacks, listing(histogram). draw_histogram:- \+clause(histogram(_J,_B,_F,_P),_), wn('Before do this, you have need to create a histogram.'). draw_histogram:- wn([class,frequency,percentile]), write_ruler(10), findall(J,histogram(J,_B,_F,_P),Js), sort(Js,N),%wn(N), forall( ( member(J,N)%,write(J) ), ( histogram(J,B,F,P), iteratively_draw(P,'*'), wn((B,F,P)) ) ),trim_stacks. iteratively_draw(M,X):- length(L,50), forall(nth1(K,L,_),(M >= 2*K->write(X);true)), write('|'). write_ruler(N):- length(L,N), forall(member(_A,L),iteratively_draw(8,'-')), nl. counter(N,M,L):- length(L,_), findall(M,member(M,L),Mx), length(Mx,N). % end