Package net.sf.tweety.math.probability
Class ProbabilityFunction<T extends java.lang.Comparable<T>>
- java.lang.Object
-
- net.sf.tweety.math.probability.ProbabilityFunction<T>
-
- Type Parameters:
T- The class of the objects used.
- All Implemented Interfaces:
java.util.Map<T,Probability>
- Direct Known Subclasses:
ProbabilisticExtension,SubgraphProbabilityFunction
public class ProbabilityFunction<T extends java.lang.Comparable<T>> extends java.lang.Object implements java.util.Map<T,Probability>
This class represents a probability distribution over some set of objects- Author:
- Matthias Thimm
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<T,Probability>probabilitiesThe probabilities of the objects.private static java.util.RandomrandomFor random sampling.
-
Constructor Summary
Constructors Constructor Description ProbabilityFunction()Creates a new probability function.ProbabilityFunction(ProbabilityFunction<T> other)Creates a new probability function by copying the given one.
-
Method Summary
Modifier and Type Method Description voidclear()booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)static <S extends java.lang.Comparable<S>>
ProbabilityFunction<S>convexCombination(double[] factors, ProbabilityFunction<S>[] creators)Computes the convex combination of the given probability distributions P1,...,PN with parameters factors, i.e.ProbabilityFunction<T>convexCombination(double d, ProbabilityFunction<T> other)Computes the convex combination of this P1 and the given probability distribution P2 with parameter d, i.e.java.util.Set<java.util.Map.Entry<T,Probability>>entrySet()booleanequals(java.lang.Object obj)Probabilityget(java.lang.Object key)java.util.Vector<Probability>getProbabilityVector()Returns the vector of probabilities, depending on the order of the domain elements (which can be ordered as they implement Comparable).java.util.Vector<java.lang.Double>getProbabilityVectorAsDoubles()Returns the vector of probabilities, depending on the order of the domain elements (which can be ordered as they implement Comparable).static <S extends java.lang.Comparable<S>>
ProbabilityFunction<S>getUniformDistribution(java.util.Set<S> objects)Returns the uniform distribution on the given interpretations.inthashCode()booleanisEmpty()booleanisNormalized()Checks whether this probability function is normalized, i.e.java.util.Set<T>keySet()ProbabilityFunction<T>linearCombination(double d1, double d2, ProbabilityFunction<T> other)Makes a linear combination of this distribution "p1" and the given distribution "other" and the given parameters, i.e.voidnormalize()Normalizes this probability function to have mass 1.protected static voidnormalize(java.util.List<java.lang.Double> probabilities)Normalizes the given list of probabilities, i.e.Probabilityprobability(java.util.Collection<? extends T> objects)Gets the probability of the given object.Probabilityprobability(T w)Gets the probability of the given object.Probabilityput(T key, Probability value)voidputAll(java.util.Map<? extends T,? extends Probability> m)Probabilityremove(java.lang.Object key)Tsample()Samples one element from the domain of this probability function, depending on its probability.Tsample(java.util.Random random)Samples one element from the domain of this probability function, depending on its probability.intsize()java.lang.StringtoString()java.util.Collection<Probability>values()
-
-
-
Field Detail
-
random
private static java.util.Random random
For random sampling.
-
probabilities
private java.util.Map<T extends java.lang.Comparable<T>,Probability> probabilities
The probabilities of the objects.
-
-
Constructor Detail
-
ProbabilityFunction
public ProbabilityFunction()
Creates a new probability function.
-
ProbabilityFunction
public ProbabilityFunction(ProbabilityFunction<T> other)
Creates a new probability function by copying the given one.- Parameters:
other- another probability function
-
-
Method Detail
-
probability
public Probability probability(T w) throws java.lang.IllegalArgumentException
Gets the probability of the given object.- Parameters:
w- some object.- Returns:
- the probability of the given object.
- Throws:
java.lang.IllegalArgumentException- if the given object has no probability
-
probability
public Probability probability(java.util.Collection<? extends T> objects) throws java.lang.IllegalArgumentException
Gets the probability of the given object.- Parameters:
objects- some object.- Returns:
- the probability of the given object.
- Throws:
java.lang.IllegalArgumentException- if the given object has no probability
-
normalize
protected static void normalize(java.util.List<java.lang.Double> probabilities)
Normalizes the given list of probabilities, i.e. divides each probability by the sum of all probabilities.- Parameters:
probabilities- a list of probabilities
-
isNormalized
public boolean isNormalized()
Checks whether this probability function is normalized, i.e. the sum of all probabilities is 1.- Returns:
- "true" if this probability function is normalized.
-
normalize
public void normalize()
Normalizes this probability function to have mass 1.
-
convexCombination
public ProbabilityFunction<T> convexCombination(double d, ProbabilityFunction<T> other)
Computes the convex combination of this P1 and the given probability distribution P2 with parameter d, i.e. it returns a P with P(i)=d P1(i) + (1-d) P2(i) for every interpretation i.- Parameters:
d- a doubleother- a probability distribution- Returns:
- the convex combination of this P1 and the given probability distribution P2 with parameter d.
- Throws:
java.lang.IllegalArgumentException- if either d is not in [0,1] or this and the given probability distribution are not defined on the same set of interpretations.
-
linearCombination
public ProbabilityFunction<T> linearCombination(double d1, double d2, ProbabilityFunction<T> other)
Makes a linear combination of this distribution "p1" and the given distribution "other" and the given parameters, i.e. it returns a P with P(i)=d1 P1(i) + d2 P2(i) for every interpretation i. NOTE: P is normalized after combination.- Parameters:
d1- a double.d2- a double.other- a probability distribution.- Returns:
- a probability distribution.
-
convexCombination
public static <S extends java.lang.Comparable<S>> ProbabilityFunction<S> convexCombination(double[] factors, ProbabilityFunction<S>[] creators) throws java.lang.IllegalArgumentException
Computes the convex combination of the given probability distributions P1,...,PN with parameters factors, i.e. it returns a P with P(i)=d1 P1(i) + d2 P2(i)+ ... + dN PN(i) for every object i (with d1,...,dN=factors).- Type Parameters:
S- The object class- Parameters:
factors- a vector of doubles.creators- a vector of probability distributions.- Returns:
- the convex combination of the given distributions with parameters factors.
- Throws:
java.lang.IllegalArgumentException- if either the sum of factors d is not in 1, or this and the given probability distributions are not defined on the same set of objects, or the lengths of creators and factors differ.
-
getUniformDistribution
public static <S extends java.lang.Comparable<S>> ProbabilityFunction<S> getUniformDistribution(java.util.Set<S> objects)
Returns the uniform distribution on the given interpretations.- Type Parameters:
S- The interpretation class- Parameters:
objects- set of interpretations of type S.- Returns:
- the uniform distribution on the given interpretations.
-
sample
public T sample()
Samples one element from the domain of this probability function, depending on its probability.- Returns:
- a sample from this probability function.
-
sample
public T sample(java.util.Random random)
Samples one element from the domain of this probability function, depending on its probability.- Parameters:
random- the number generator used.- Returns:
- a sample from this probability function.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
containsKey
public boolean containsKey(java.lang.Object key)
- Specified by:
containsKeyin interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
containsValue
public boolean containsValue(java.lang.Object value)
- Specified by:
containsValuein interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
entrySet
public java.util.Set<java.util.Map.Entry<T,Probability>> entrySet()
- Specified by:
entrySetin interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
get
public Probability get(java.lang.Object key)
- Specified by:
getin interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
put
public Probability put(T key, Probability value)
- Specified by:
putin interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
putAll
public void putAll(java.util.Map<? extends T,? extends Probability> m)
- Specified by:
putAllin interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
remove
public Probability remove(java.lang.Object key)
- Specified by:
removein interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
size
public int size()
- Specified by:
sizein interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
values
public java.util.Collection<Probability> values()
- Specified by:
valuesin interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
keySet
public java.util.Set<T> keySet()
- Specified by:
keySetin interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>- Overrides:
hashCodein classjava.lang.Object
-
getProbabilityVector
public java.util.Vector<Probability> getProbabilityVector()
Returns the vector of probabilities, depending on the order of the domain elements (which can be ordered as they implement Comparable).- Returns:
- the vector of probabilities
-
getProbabilityVectorAsDoubles
public java.util.Vector<java.lang.Double> getProbabilityVectorAsDoubles()
Returns the vector of probabilities, depending on the order of the domain elements (which can be ordered as they implement Comparable).- Returns:
- the vector of probabilities as doubles
-
equals
public boolean equals(java.lang.Object obj)
- Specified by:
equalsin interfacejava.util.Map<T extends java.lang.Comparable<T>,Probability>- Overrides:
equalsin classjava.lang.Object
-
-