Skip to content

Commit

Permalink
Merge branch 'master' of github.com:friguzzi/cplint
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrizio Riguzzi committed Nov 4, 2018
2 parents da8b6a5 + aef8ec0 commit 2c7bc4d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
10 changes: 9 additions & 1 deletion prolog/examples/bitcoin_attack.pl
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
Computing the probability of success of an attack to the Bitcoin protocol
From Damiano Azzolini, Fabrizio Riguzzi, Evelina Lamma, Elena Bellodi,
and Riccardo Zese.
Modeling bitcoin protocols with probabilistic logic programming.
PLP 2018 http://ceur-ws.org/Vol-2219/paper6.pdf
*/
:- use_module(library(mcintyre)).
:- mc.
:- begin_lpad.
Expand Down Expand Up @@ -43,4 +51,4 @@
/** <examples>
?- mc_prob(success_poisson,Prob).
?- mc_prob(success_pascal,Prob).
*/
*/
11 changes: 9 additions & 2 deletions prolog/examples/bitcoin_two_phase.pl
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
% Empty with mcintyre loaded
/*
Computing the probability of mining the next block with a two-phase
Proof-of-Work protocol in a Bitcoin network
From Damiano Azzolini, Fabrizio Riguzzi, Evelina Lamma, Elena Bellodi,
and Riccardo Zese.
Modeling bitcoin protocols with probabilistic logic programming.
PLP 2018 http://ceur-ws.org/Vol-2219/paper6.pdf
*/
:- use_module(library(mcintyre)).

:- if(current_predicate(use_rendering/1)).
Expand Down Expand Up @@ -95,4 +102,4 @@
?- mc_sample(reach(a0,0,a2),100,PA).
?- mc_sample(reach(b0,0,b2),100,PB).
?- mc_prob(reach(a0,0,a2),PA).
*/
*/
66 changes: 66 additions & 0 deletions prolog/examples/h_index.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Computation of the h-index of an author given his number of papers
assuming that the citations per paper follow a Poisson distribution with
given average.
It shows that the expected h-index is directly proportional to the
average number of citations (expected h-index=avg. n. cit. +5)
*/
:- use_module(library(mcintyre)).
:- if(current_predicate(use_rendering/1)).
:- use_rendering(c3).
:- endif.
:- mc.

:- begin_lpad.

citations(AverageCit,_,Cit):poisson(Cit,AverageCit).

h_index(NumPapers,AverageCit,HIndex):-
numlist(1,NumPapers,Papers),
maplist(citations(AverageCit),Papers,Citations),
sort(0, @>=, Citations, Sorted),
compute_index(Sorted,1,HIndex).

compute_index([0|_],_,0):-!.

compute_index([],I0,I):-!,
I is I0-1.

compute_index([I|_T],I,I):-!.

compute_index([H|_T],I0,I):-
H<I0,!,
I is I0-1.

compute_index([H|T],I0,I):-
H>I0,
I1 is I0+1,
compute_index(T,I1,I).



:- end_lpad.

h_vs_avg_cit(Papers,MaxAvg,Chart):-
findall(E,(
between(1,MaxAvg,N),mc_expectation(h_index(Papers,N,T),10,T,E)
),V),
numlist(1,MaxAvg,X),
findall(N,(between(1,MaxAvg,N0),N is 5+N0),Dep),
Chart=c3{data:_{x:x, columns:[[x|X],['Expected h-index'|V],['Dependency'|Dep]]}}.

/** <examples> Your example queries go here, e.g.
?- mc_sample_arg_first(h_index(200,10,H),1000,H,HList),density(HList,Dens,[nbins(20)]).
compute the distribution of the h_index given that the authors wrote
200 papers and each paper receives on average 10 citations
?- mc_expectation(h_index(200,10,H),1000,H,HExp).
compute the expected value of the h_index given that the authors wrote
200 papers and each paper receives on average 10 citations
?- mc_sample_arg_first(citations(10,200,Cit),1000,Cit,CitList),density(CitList,Dens,[nbins(20)]).
?- h_vs_avg_cit(100,40,Chart).
Plot of the depedency of the expected h-index as a function of the average
number of citations per article (max 40) given that there are 100 papers.
The graphs shows that the expected h-index is directly proportional to the
average number of citations (expected h-index=avg. n. cit. +5)
*/

0 comments on commit 2c7bc4d

Please sign in to comment.