This module performs reasoning over Logic Programs with Annotated
Disjunctions and CP-Logic programs.
It reads probabilistic program and computes the probability of queries
using sampling.
See https://friguzzi.github.io/cplint/ for details.
Reexports cplint_util and clpr.
- author
- - Fabrizio Riguzzi
- copyright
- - Fabrizio Riguzzi
- license
- - Artistic License 2.0 https://opensource.org/licenses/Artistic-2.0
- mc_load(++File:atom) is det
- Loads File.lpad if it exists, otherwise loads File.cpl if it exists.
- mc_load_file(++FileWithExtension:atom) is det
- Loads FileWithExtension.
- mc_prob(:Query:atom, -Probability:float, +Options:list) is det
- The predicate computes the probability of the query Query
If Query is not ground, it considers it as an existential query
and returns the probability that there is a satisfying assignment to
the query.
Options is a list of options, the following are recognised by mc_prob/3:
- bar(-BarChart:dict)
- BarChart is a dict for rendering with c3 as a bar chart with a bar for the
probability of success and a bar for the probability of failure.
- mc_prob(:Query:conjunction_of_literals, -Probability:float) is det
- Equivalent to mc_prob/2 with an empty option list.
- mc_sample(:Query:conjunction_of_literals, +Samples:int, -Probability:float, +Options:list) is det
- The predicate samples Query a number of Samples times and returns
the resulting Probability (Successes/Samples)
If Query is not ground, it considers it as an existential query
Options is a list of options, the following are recognised by mc_sample/4:
- successes(-Successes:int)
- Number of successes
- failures(-Failures:int)
- Number of failures
- bar(-BarChart:dict)
- BarChart is a dict for rendering with c3 as a bar chart with a bar for the
number of successes and a bar for the number of failures.
- mc_sample(:Query:conjunction_of_literals, +Samples:int, -Probability:float) is det
- Equivalent to mc_sample/4 with an empty option list.
- mc_rejection_sample(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, +Samples:int, -Probability:float, +Options:list) is det
- The predicate samples Query a number of Samples times given that Evidence
is true and returns
the Probability of Query.
It performs rejection sampling: if in a sample Evidence is false, the
sample is discarded.
If Query/Evidence are not ground, it considers them an existential queries.
Options is a list of options, the following are recognised by mc_rejection_sample/5:
- successes(-Successes:int)
- Number of succeses
- failures(-Failures:int)
- Number of failueres
- mc_rejection_sample(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, +Samples:int, -Probability:float) is det
- Equivalent to mc_rejection_sample/5 with an empty option list.
- mc_gibbs_sample(:Query:conjunction_of_literals, +Samples:int, -Probability:float, +Options:list) is det
- The predicate samples Query a number of Mix+Samples (Mix is set with the options, default value 0)
times.
The first Mix (that is set with the options, default value 0) samples are discarded (mixing time).
It performs Gibbs sampling: each sample is obtained from the previous one by resampling
a variable given the values of the variables in its Markov blanket.
If Query/Evidence are not ground, it considers them as existential queries.
Options is a list of options, the following are recognised by mc_gibbs_sample/4:
- block +Block:int
- Perform blocked Gibbs: Block variables are sampled together, default value 1
- mix(+Mix:int)
- The first Mix samples are discarded (mixing time), default value 0
- successes(-Successes:int)
- Number of succeses
- failures(-Failures:int)
- Number of failueres
- mc_gibbs_sample(:Query:conjunction_of_literals, +Samples:int, -Probability:float) is det
- Equivalent to mc_gibbs_sample/4 with an empty option list.
- mc_gibbs_sample(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, +Samples:int, -Probability:float, +Options:list) is det
- The predicate samples Query a number of Mix+Samples (Mix is set with the options, default value 0) times given that
Evidence
is true and returns
the number of Successes, of Failures and the
Probability (Successes/Samples).
The first Mix (that is set with the options, default value 0) samples are discarded (mixing time).
It performs Gibbs sampling: each sample is obtained from the previous one by resampling
a variable given the values of the variables in its Markov blanket.
If Query/Evidence are not ground, it considers them as existential queries.
Options is a list of options, the following are recognised by mc_gibbs_sample/5:
- block +Block:int
- Perform blocked Gibbs: Block variables are sampled together, default value 1
- mix(+Mix:int)
- The first Mix samples are discarded (mixing time), default value 0
- successes(-Successes:int)
- Number of succeses
- failures(-Failures:int)
- Number of failueres
- mc_gibbs_sample_arg(:Query:conjunction_of_literals, +Samples:int, ?Arg:var, -Values:list, +Options:list) is det
- The predicate samples Query a number of Samples times.
Arg should be a variable in Query.
The predicate returns in Values a list of couples L-N where
L is the list of values of Arg for which Query succeeds in
a world sampled at random and N is the number of samples
returning that list of values.
The first Mix (that is set with the options, default value 0) samples are discarded (mixing time).
It performs Gibbs sampling: each sample is obtained from the previous one by resampling
a variable given the values of the variables in its Markov blanket.
Options is a list of options, the following are recognised by mc_gibbs_sample_arg/5:
- block +Block:int
- Perform blocked Gibbs: Block variables are sampled together, default value 1
- mix(+Mix:int)
- The first Mix samples are discarded (mixing time), default value 0
- bar(-BarChar:dict)
- BarChart is a dict for rendering with c3 as a bar chart with
a bar for each possible value of L,
the list of value of Arg for which Query succeeds in
a world sampled at random.
- mc_gibbs_sample_arg(:Query:conjunction_of_literals, +Samples:int, ?Arg:var, -Values:list) is det
- Equivalent to mc_gibbs_sample_arg/5 with an empty option list.
- mc_gibbs_sample_arg(:Query:conjunction_of_literals, :Evidence:conjunction_of_groundliterals, +Samples:int, ?Arg:var, -Values:list, +Options:list) is det
- The predicate samples Query a number of Samples times given that Evidence
is true.
Arg should be a variable in Query.
The predicate returns in Values a list of couples L-N where
L is the list of values of Arg for which Query succeeds in
a world sampled at random and N is the number of samples
returning that list of values.
The first Mix (that is set with the options, default value 0) samples are discarded (mixing time).
It performs Gibbs sampling: each sample is obtained from the previous one by resampling
a variable given the values of the variables in its Markov blanket.
Options is a list of options, the following are recognised by mc_gibbs_sample_arg/6:
- block +Block:int
- Perform blocked Gibbs: Block variables are sampled together, default value 1
- mix(+Mix:int)
- The first Mix samples are discarded (mixing time), default value 0
- bar(-BarChar:dict)
- BarChart is a dict for rendering with c3 as a bar chart with
a bar for each possible value of L,
the list of value of Arg for which Query succeeds in
a world sampled at random.
- mc_mh_sample(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, +Samples:int, -Probability:float, +Options:list) is det
- The predicate samples Query a number of Mix+Samples (Mix is set with the options, default value 0) times given that
Evidence
is true and returns
the number of Successes, of Failures and the
Probability (Successes/Samples).
The first Mix (that is set with the options, default value 0) samples are discarded (mixing time).
It performs Metropolis/Hastings sampling: between each sample, Lag (that is set with the options, default value 1) sampled
choices are forgotten and each sample is accepted with a certain probability.
If Query/Evidence are not ground, it considers them as existential queries.
Options is a list of options, the following are recognised by mc_mh_sample/5:
- mix(+Mix:int)
- The first Mix samples are discarded (mixing time), default value 0
- lag(+Lag:int)
- lag between each sample, Lag sampled choices are forgotten, default value 1
- successes(-Successes:int)
- Number of succeses
- failures(-Failures:int)
- Number of failueres
- mc_mh_sample(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, +Samples:int, -Probability:float) is det
- Equivalent to mc_mh_sample/5 with an empty option list.
- mc_sample_arg(:Query:conjunction_of_literals, +Samples:int, ?Arg:var, -Values:list, +Options:list) is det
- The predicate samples Query a number of Samples times.
Arg should be a variable in Query.
The predicate returns in Values a list of couples L-N where
L is the list of values of Arg for which Query succeeds in
a world sampled at random and N is the number of samples
returning that list of values.
Options is a list of options, the following are recognised by mc_sample_arg/5:
- bar(-BarChar:dict)
- BarChart is a dict for rendering with c3 as a bar chart with
a bar for each possible value of L,
the list of value of Arg for which Query succeeds in
a world sampled at random.
- mc_sample_arg(:Query:conjunction_of_literals, +Samples:int, ?Arg:var, -Values:list) is det
- Equivalent to mc_sample_arg/5 with an empty option list.
- mc_rejection_sample_arg(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, +Samples:int, ?Arg:var, -Values:list, +Options:list) is det
- The predicate samples Query a number of Samples times given that
Evidence is true.
Arg should be a variable in Query.
The predicate returns in Values a list of couples L-N where
L is the list of values of Arg for which Query succeeds in
a world sampled at random and N is the number of samples
returning that list of values.
Rejection sampling is performed.
Options is a list of options, the following are recognised by mc_rejection_sample_arg/6:
- bar(-BarChar:dict)
- BarChart is a dict for rendering with c3 as a bar chart with
a bar for each possible value of L,
the list of value of Arg for which Query succeeds in
a world sampled at random.
- mc_rejection_sample_arg(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, +Samples:int, ?Arg:var, -Values:list) is det
- Equivalent to mc_rejection_sample_arg/6 with an empty option list.
- mc_mh_sample_arg(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, +Samples:int, ?Arg:var, -Values:list, +Options:list) is det
- The predicate samples Query a number of Samples times given that Evidence
is true.
Arg should be a variable in Query.
The predicate returns in Values a list of couples L-N where
L is the list of values of Arg for which Query succeeds in
a world sampled at random and N is the number of samples
returning that list of values.
The first Mix (that is set with the options, default value 0) samples are discarded (mixing time).
It performs Metropolis/Hastings sampling: between each sample, Lag (that is set with the options, default value 1) sampled
choices are forgotten and each sample is accepted with a certain probability.
Options is a list of options, the following are recognised by mc_mh_sample_arg/6:
- mix(+Mix:int)
- The first Mix samples are discarded (mixing time), default value 0
- lag(+Lag:int)
- lag between each sample, Lag sampled choices are forgotten, default value 1
- bar(-BarChar:dict)
- BarChart is a dict for rendering with c3 as a bar chart with
a bar for each possible value of L,
the list of value of Arg for which Query succeeds in
a world sampled at random.
- mc_mh_sample_arg(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, +Samples:int, ?Arg:var, -Values:list) is det
- Equivalent to mc_mh_sample_arg/6 with an empty option list.
- mc_particle_sample(:Query:conjunction_of_literals, :Evidence:list, +Samples:int, -Prob:float) is det
- The predicate samples Query a number of Samples times given that Evidence
is true. Evidence is a list of goals.
The predicate returns in Prob the probability that the query is true.
It performs particle filtering with likelihood weighting:
each sample is weighted by the
likelihood of an element of the Evidence list and constitutes a particle.
After weighting, particles are resampled and the next element of Evidence
is considered.
- mc_particle_sample_arg(:Query:conjunction_of_literals, +Evidence:list, +Samples:int, ?Arg:term, -Values:list) is det
- The predicate samples Query a number of Samples times given that Evidence
is true.
It performs particle filtering with likelihood weighting:
each sample is weighted by the
likelihood of an element of the Evidence list and constitutes a particle.
After weighting, particles are resampled and the next element of Evidence
is considered.
Arg should be a variable in Query. Evidence is a list of goals.
Query can be either a single goal or a list of goals.
When Query is a single goal, the predicate returns in Values
a list of couples V-W where V is a value of Arg for which Query succeeds in
a particle in the last set of particles and W is the weight of the particle.
For each element of Evidence, the particles are obtained by sampling Query
in each current particle and weighting the particle by the likelihood
of the evidence element.
When Query is a list of goals, Arg is a list of variables, one for
each query of Query and Arg and Query must have the same length of Evidence.
Values is then list of the same length of Evidence and each of its
elements is a list of couples V-W where
V is a value of the corresponding element of Arg for which the corresponding
element of Query succeeds in a particle and W is the weight of the particle.
For each element of Evidence, the particles are obtained by sampling the
corresponding element of Query in each current particle and weighting
the particle by the likelihood of the evidence element.
- mc_particle_expectation(:Query:conjunction_of_literals, :Evidence:list, +N:int, ?Arg:var, -Exp:float) is det
- The predicate computes the expected value of Arg in Query given Evidence by
particle filtering.
It uses N particle and sums up the weighted value of Arg for
each particle. The overall sum is divided by the sum of weights to give Exp.
Arg should be a variable in Query.
- mc_lw_sample(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, +Samples:int, -Prob:float) is det
- The predicate samples Query a number of Samples times given that Evidence
is true.
The predicate returns in Prob the probability that the query is true.
It performs likelihood weighting: each sample is weighted by the
likelihood of evidence in the sample.
- mc_lw_sample_arg(:Query:atom, :Evidence:atom, +Samples:int, ?Arg:var, -Values:list) is det
- The predicate samples Query a number of Samples times given that Evidence
is true.
Arg should be a variable in Query.
The predicate returns in Values a list of couples V-W where
V is a value of Arg for which Query succeeds in
a world sampled at random and W is the weight of the sample.
It performs likelihood weighting: each sample is weighted by the
likelihood of evidence in the sample.
- mc_lw_sample_arg_log(:Query:atom, :Evidence:atom, +Samples:int, ?Arg:var, -Values:list) is det
- The predicate samples Query a number of Samples times given that Evidence
is true.
Arg should be a variable in Query.
The predicate returns in Values a list of couples V-W where
V is a value of Arg for which Query succeeds in
a world sampled at random and W is the natural logarithm of the weight of \
the sample.
It performs likelihood weighting: each sample is weighted by the
likelihood of evidence in the sample.
It differs from mc_lw_sample_arg/5 because the natural logarithm of the
weight is returned, useful when the evidence is very unlikely.
- mc_lw_expectation(:Query:atom, :Evidence:atom, +N:int, ?Arg:var, -Exp:float) is det
- The predicate computes the expected value of Arg in Query given Evidence by
likelihood weighting.
It takes N samples of Query and sums up the weighted value of Arg for
each sample. The overall sum is divided by the sum of weights to give Exp.
Arg should be a variable in Query.
- mc_sample_arg_first(:Query:atom, +Samples:int, ?Arg:var, -Values:list, +Options:list) is det
- The predicate samples Query a number of Samples times.
Arg should be a variable in Query.
The predicate returns in Values a list of couples V-N where
V is the value of Arg returned as the first answer by Query in
a world sampled at random and N is the number of samples
returning that value.
V is failure if the query fails.
Options is a list of options, the following are recognised by mc_sample_arg_first/5:
- bar(-BarChar:dict)
- BarChart is a dict for rendering with c3 as a bar chart with
with a bar for each value of Arg returned as a first answer by Query in
a world sampled at random.
The size of the bar is the number of samples that returned that value.
- mc_sample_arg_first(:Query:atom, +Samples:int, ?Arg:var, -Values:list) is det
- Equivalent to mc_sample_arg_first/5 with an empty option list.
- mc_sample_arg_one(:Query:atom, +Samples:int, ?Arg:var, -Values:list, +Options:list) is det
- The predicate samples Query a number of Samples times.
Arg should be a variable in Query.
The predicate returns in Values a list of couples V-N where
V is a value of Arg sampled with uniform probability from those returned
by Query in a world sampled at random and N is the number of samples
returning that value.
V is failure if the query fails.
Options is a list of options, the following are recognised by mc_sample_arg_one/5:
- bar(-BarChar:dict)
- BarChart is a dict for rendering with c3 as a bar chart
with a bar for each value of Arg returned by sampling with uniform
probability one answer from those returned by Query in a world sampled
at random.
The size of the bar is the number of samples.
- mc_sample_arg_one(:Query:atom, +Samples:int, ?Arg:var, -Values:list) is det
- Equivalent to mc_sample_arg_one/5 with an empty option list.
- mc_sample_arg_raw(:Query:atom, +Samples:int, ?Arg:var, -Values:list) is det
- The predicate samples Query a number of Samples times.
Arg should be a variable in Query.
The predicate returns in Values a list of values
of Arg returned as the first answer by Query in
a world sampled at random.
The value is failure if the query fails.
- mc_expectation(:Query:atom, +N:int, ?Arg:var, -Exp:float) is det
- The predicate computes the expected value of Arg in Query by
sampling.
It takes N samples of Query and sums up the value of Arg for
each sample. The overall sum is divided by N to give Exp.
Arg should be a variable in Query.
- mc_gibbs_expectation(:Query:atom, +N:int, ?Arg:var, -Exp:float, +Options:list) is det
- The predicate computes the expected value of Arg in Query by
sampling.
It takes N samples of Query and sums up the value of Arg for
each sample. The overall sum is divided by N to give Exp.
Arg should be a variable in Query.
Options is a list of options, the following are recognised by mc_mh_sample_arg/6:
- block +Block:int
- Perform blocked Gibbs: Block variables are sampled together, default value 1
- mix(+Mix:int)
- The first Mix samples are discarded (mixing time), default value 0
- mc_gibbs_expectation(:Query:atom, +N:int, ?Arg:var, -Exp:float) is det
- Equivalent to mc_gibbs_expectation/5 with an empty option list.
- mc_rejection_expectation(:Query:atom, :Evidence:atom, +N:int, ?Arg:var, -Exp:float) is det
- The predicate computes the expected value of Arg in Query by
sampling.
It takes N samples of Query and sums up the value of Arg for
each sample. The overall sum is divided by N to give Exp.
Arg should be a variable in Query.
- mc_gibbs_expectation(:Query:atom, :Evidence:atom, +N:int, ?Arg:var, -Exp:float, +Options:list) is det
- The predicate computes the expected value of Arg in Query by
Gibbs sampling.
It takes N samples of Query and sums up the value of Arg for
each sample. The overall sum is divided by N to give Exp.
Arg should be a variable in Query.
Options is a list of options, the following are recognised by mc_mh_expectation/6:
- block +Block:int
- Perform blocked Gibbs: Block variables are sampled together, default value 1
- mix(+Mix:int)
- The first Mix samples are discarded (mixing time), default value 0
- mc_mh_expectation(:Query:atom, :Evidence:atom, +N:int, ?Arg:var, -Exp:float, +Options:list) is det
- The predicate computes the expected value of Arg in Query by
Metropolis Hastings sampling.
It takes N samples of Query and sums up the value of Arg for
each sample. The overall sum is divided by N to give Exp.
Arg should be a variable in Query.
Options is a list of options, the following are recognised by mc_mh_expectation/6:
- mix(+Mix:int)
- The first Mix samples are discarded (mixing time), default value 0
- lag(+Lag:int)
- lag between each sample, Lag sampled choices are forgotten, default value 1
- mc_mh_expectation(:Query:atom, :Evidence:atom, +N:int, ?Arg:var, -Exp:float) is det
- Equivalent to mc_mh_expectation/6 with an empty option list.
- sample_head(+R:int, +Variables:list, +M:module, +HeadList:list, -HeadNumber:int) is det
- samples a head from rule R instantiated as indicated by Variables (list of
constants, one per variable. HeadList contains the head as a list.
HeadNumber is the number of the sample head.
Internal predicates used by the transformed input program
- take_a_sample(+R:int, +VC:list, +M:module, +Distr:term, -S:term) is det
- Returns in S a sample for a random variable with distribution Distr
associated to rule R with substitution VC. If the variable
has already been sampled, it retrieves the sampled value, otherwise
it takes a new sample and records it for rule R with substitution VC.
- gaussian(+Mean:float, +Variance:float, +M:module, -S:float) is det
- samples a value from a Gaussian with mean Mean and variance
Variance and returns it in S
- gaussian(+Mean:float, +Variance:float, +M:module, +S:float, -Density:float) is det
- Computes the probability density of value S according to a Gaussian with
mean Mean and variance Variance and returns it in Density.
- add_prob(?Prob:float, :Goal:atom, ?AnnGoal:atom) is det
- From Prob and Goal builds the annotated atom AnnGoal=Goal:Prob.
- set_mc(:Parameter:atom, +Value:term) is det
- The predicate sets the value of a parameter
For a list of parameters see
https://friguzzi.github.io/cplint/
- setting_mc(:Parameter:atom, ?Value:term) is det
- The predicate returns the value of a parameter
For a list of parameters see
https://friguzzi.github.io/cplint/
- set_sw(:Var:term, +List:lit) is det
- Sets the domain of the random variable Var to List.
This is a predicate for programs in the PRISM syntax
- msw(:Var:term, ?Value:term) is det
- Gets or tests the Value of the random variable Var.
This is a predicate for programs in the PRISM syntax
- swap(?Term1:term, ?Term2:term) is det
- If Term1 is of the form A:B, then Term2 is of the form B:A.
- :Term:term ~= +B:term is det
- equality predicate for distributional clauses
Undocumented predicates
The following predicates are exported, but not or incorrectly documented.
- bar(Arg1, Arg2)
- bar(Arg1, Arg2, Arg3)
- bar1(Arg1, Arg2)
- argbar(Arg1, Arg2)
- histogram(Arg1, Arg2)
- histogram(Arg1, Arg2, Arg3)
- densities(Arg1, Arg2, Arg3)
- densities(Arg1, Arg2, Arg3, Arg4)
- density(Arg1, Arg2)
- density(Arg1, Arg2, Arg3)
- density2d(Arg1, Arg2)
- density2d(Arg1, Arg2, Arg3)
- to_pair(Arg1, Arg2)
- key_pair(Arg1, Arg2)
- value_pair(Arg1, Arg2)
- bin(Arg1, Arg2, Arg3, Arg4, Arg5)
- beta(Arg1, Arg2)
- to_atom(Arg1, Arg2)
- average(Arg1, Arg2)
- variance(Arg1, Arg2)
- variance(Arg1, Arg2, Arg3)
- std_dev(Arg1, Arg2)
- std_dev(Arg1, Arg2, Arg3)
- agg_val(Arg1, Arg2, Arg3)
- swi_builtin(Arg1)
- {Arg1}
- maximize(Arg1)
- minimize(Arg1)
- inf(Arg1, Arg2)
- inf(Arg1, Arg2, Arg3, Arg4)
- sup(Arg1, Arg2)
- sup(Arg1, Arg2, Arg3, Arg4)
- bb_inf(Arg1, Arg2, Arg3)
- bb_inf(Arg1, Arg2, Arg3, Arg4, Arg5)
- ordering(Arg1)
- entailed(Arg1)
- clp_type(Arg1, Arg2)
- dump(Arg1, Arg2, Arg3)