-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #723 from p2t2/DEV5.0
Dev5.0
- Loading branch information
Showing
487 changed files
with
10,369 additions
and
5,507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=4.1.0.0 | ||
version=5.0.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
Figaro/src/main/scala/com/cra/figaro/algorithm/AnytimeBoundsProbQuery.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* AnytimeBoundsProbQuery.scala | ||
* Anytime algorithms that compute bounds on conditional probabilities of query elements. | ||
* | ||
* Created By: William Kretschmer (kretsch@mit.edu) | ||
* Creation Date: Jun 23, 2017 | ||
* | ||
* Copyright 2017 Avrom J. Pfeffer and Charles River Analytics, Inc. | ||
* See http://www.cra.com or email figaro@cra.com for information. | ||
* | ||
* See http://www.github.com/p2t2/figaro for a copy of the software license. | ||
*/ | ||
package com.cra.figaro.algorithm | ||
|
||
import com.cra.figaro.language._ | ||
import akka.pattern.ask | ||
|
||
/** | ||
* One-time algorithms that compute bounds on conditional probabilities of query elements. A class that implements this | ||
* trait must implement initialize, runStep, computeAllProbabilityBounds, and computeExpectationBounds methods. | ||
*/ | ||
trait AnytimeBoundsProbQuery extends BoundsProbQueryAlgorithm with AnytimeProbQuery { | ||
/** | ||
* A message instructing the handler to compute all probability bounds of the target element. | ||
*/ | ||
case class ComputeAllProbabilityBounds[T](target: Element[T]) extends Service | ||
|
||
/** | ||
* A message from the handler containing all probability bounds of the previously requested element. | ||
*/ | ||
case class AllProbabilityBounds[T](bounds: Stream[(Double, Double, T)]) extends Response | ||
|
||
/** | ||
* A message instructing the handler to compute bounds on the expectation of the target element under the given function. | ||
*/ | ||
case class ComputeExpectationBounds[T](target: Element[T], function: T => Double, bounds: Option[(Double, Double)]) extends Service | ||
|
||
/** | ||
* A message from the handler containing the bounds on the expectation of the previously requested element and function. | ||
*/ | ||
case class ExpectationBounds[T](bounds: (Double, Double)) extends Response | ||
|
||
/** | ||
* A message instructing the handler to compute bounds on the probability of the predicate for the target element. | ||
*/ | ||
case class ComputeProbabilityBounds[T](target: Element[T], predicate: T => Boolean) extends Service | ||
|
||
/** | ||
* A message from the handler containing the bounds on the probability of the previously requested element and predicate. | ||
*/ | ||
case class ProbabilityBounds[T](bounds: (Double, Double)) extends Response | ||
|
||
override def handle(service: Service): Response = | ||
service match { | ||
case ComputeDistribution(target) => | ||
Distribution(computeDistribution(target)) | ||
case ComputeExpectation(target, function) => | ||
Expectation(computeExpectation(target, function)) | ||
case ComputeProbability(target, predicate) => | ||
Probability(computeProbability(target, predicate)) | ||
case ComputeProjection(target) => | ||
Projection(computeProjection(target)) | ||
case ComputeAllProbabilityBounds(target) => | ||
AllProbabilityBounds(computeAllProbabilityBounds(target)) | ||
case ComputeExpectationBounds(target, function, bounds) => | ||
ExpectationBounds(computeExpectationBounds(target, function, bounds)) | ||
case ComputeProbabilityBounds(target, predicate) => | ||
ProbabilityBounds(computeProbabilityBounds(target, predicate)) | ||
} | ||
|
||
protected def doAllProbabilityBounds[T](target: Element[T]): Stream[(Double, Double, T)] = { | ||
awaitResponse(runner ? Handle(ComputeAllProbabilityBounds(target)), messageTimeout.duration) match { | ||
case AllProbabilityBounds(result) => result.asInstanceOf[Stream[(Double, Double, T)]] | ||
case _ => Stream() | ||
} | ||
} | ||
|
||
protected def doExpectationBounds[T](target: Element[T], function: T => Double, bounds: Option[(Double, Double)]): (Double, Double) = { | ||
awaitResponse(runner ? Handle(ComputeExpectationBounds(target, function, bounds)), messageTimeout.duration) match { | ||
case ExpectationBounds(result) => result | ||
case _ => bounds.getOrElse((Double.NegativeInfinity, Double.PositiveInfinity)) | ||
} | ||
} | ||
|
||
protected def doProbabilityBounds[T](target: Element[T], predicate: T => Boolean): (Double, Double) = { | ||
awaitResponse(runner ? Handle(ComputeProbabilityBounds(target, predicate)), messageTimeout.duration) match { | ||
case ProbabilityBounds(result) => result | ||
case _ => (0.0, 1.0) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
Figaro/src/main/scala/com/cra/figaro/algorithm/BoundsProbQueryAlgorithm.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* BoundsProbQueryAlgorithm.scala | ||
* Algorithms that compute bounds on conditional probabilities of queries. | ||
* | ||
* Created By: William Kretschmer (kretsch@mit.edu) | ||
* Creation Date: Jun 23, 2017 | ||
* | ||
* Copyright 2017 Avrom J. Pfeffer and Charles River Analytics, Inc. | ||
* See http://www.cra.com or email figaro@cra.com for information. | ||
* | ||
* See http://www.github.com/p2t2/figaro for a copy of the software license. | ||
*/ | ||
package com.cra.figaro.algorithm | ||
|
||
import com.cra.figaro.language._ | ||
|
||
/** | ||
* Algorithms that compute bounds on conditional probabilities of queries over elements in a universe. The regular | ||
* ProbQuery methods are also available, but these methods may throw an exception if the algorithm cannot produce an | ||
* exact answer. | ||
*/ | ||
trait BoundsProbQueryAlgorithm extends ProbQueryAlgorithm { | ||
/** | ||
* Return an estimate of the marginal probability distribution over the target that lists each value with its | ||
* probability bounds. Each entry is a triple (lower, upper, value). The result is a lazy stream. It is up to the | ||
* algorithm how the stream is ordered. | ||
*/ | ||
def computeAllProbabilityBounds[T](target: Element[T]): Stream[(Double, Double, T)] | ||
|
||
/** | ||
* Return an estimate of the bounds on the expectation of the function under the marginal probability distribution | ||
* of the target. The function is assumed to be bounded between the specified lower and upper bounds, if provided. | ||
* Otherwise, the lower and upper bounds of the function using the current known values of the target are used. | ||
*/ | ||
def computeExpectationBounds[T](target: Element[T], function: T => Double, bounds: Option[(Double, Double)]): (Double, Double) | ||
|
||
/** | ||
* Return an estimate of the probability of the bounds on the predicate under the marginal probability distribution | ||
* of the target. | ||
*/ | ||
def computeProbabilityBounds[T](target: Element[T], predicate: T => Boolean): (Double, Double) = { | ||
computeExpectationBounds(target, (t: T) => if(predicate(t)) 1.0 else 0.0, Some((0.0, 1.0))) | ||
} | ||
|
||
|
||
/* | ||
* The following methods are defined in either the onetime or anytime versions of this class, | ||
* and do not need to be defined by particular algorithm implementations. | ||
*/ | ||
|
||
protected def doAllProbabilityBounds[T](target: Element[T]): Stream[(Double, Double, T)] | ||
|
||
protected def doExpectationBounds[T](target: Element[T], function: T => Double, bounds: Option[(Double, Double)]): (Double, Double) | ||
|
||
protected def doProbabilityBounds[T](target: Element[T], predicate: (T) => Boolean): (Double, Double) | ||
|
||
/** | ||
* Return an estimate of the marginal probability distribution over the target that lists each value with its | ||
* probability bounds. Each entry is a triple (lower, upper, value). The result is a lazy stream. It is up to the | ||
* algorithm how the stream is ordered. | ||
* @param target Element for which to compute bounds. | ||
* @throws NotATargetException if called on a target that is not in the list of targets of the algorithm. | ||
* @throws AlgorithmInactiveException if the algorithm is inactive. | ||
* @return Bounds on the probability of each value for this element. | ||
*/ | ||
def allProbabilityBounds[T](target: Element[T]): Stream[(Double, Double, T)] = { | ||
check(target) | ||
doAllProbabilityBounds(target) | ||
} | ||
|
||
/** | ||
* Return an estimate of the bounds on the expectation of the function under the marginal probability distribution | ||
* of the target. The function is assumed to be bounded between the specified lower and upper bounds. | ||
* @param target Element for which to compute bounds. | ||
* @param function Function whose expectation is computed. | ||
* @param lower Lower bound on the function. | ||
* @param upper Upper bound on the function. | ||
* @throws NotATargetException if called on a target that is not in the list of targets of the algorithm. | ||
* @throws AlgorithmInactiveException if the algorithm is inactive. | ||
* @throws IllegalArgumentException if the bounds given on the function are tighter than the actual bounds on the | ||
* function, using the current known values of the target. | ||
* @return Bounds on the expectation of this function for this element. | ||
*/ | ||
def expectationBounds[T](target: Element[T], function: T => Double, lower: Double, upper: Double): (Double, Double) = { | ||
check(target) | ||
doExpectationBounds(target, function, Some((lower, upper))) | ||
} | ||
|
||
/** | ||
* Return an estimate of the bounds on the expectation of the function under the marginal probability distribution | ||
* of the target. The function is assumed to be bounded according to the currently known values of the target. Thus, | ||
* one should generally only use this when the range of the target is finite and known beforehand. Otherwise, one can | ||
* use the overloaded version of this method that specifies explicit bounds on the function. | ||
* @param target Element for which to compute bounds. | ||
* @param function Function whose expectation is computed. | ||
* @throws NotATargetException if called on a target that is not in the list of targets of the algorithm. | ||
* @throws AlgorithmInactiveException if the algorithm is inactive. | ||
* @return Bounds on the expectation of this function for this element. | ||
*/ | ||
def expectationBounds[T](target: Element[T], function: T => Double): (Double, Double) = { | ||
check(target) | ||
doExpectationBounds(target, function, None) | ||
} | ||
|
||
/** | ||
* Return an estimate of the probability of the bounds on the predicate under the marginal probability distribution | ||
* of the target. | ||
* @param target Element for which to compute bounds. | ||
* @param predicate Function whose probability of evaluating to true is computed. | ||
* @throws NotATargetException if called on a target that is not in the list of targets of the algorithm. | ||
* @throws AlgorithmInactiveException if the algorithm is inactive. | ||
* @return Bounds on the probability of this function for this element. | ||
*/ | ||
def probabilityBounds[T](target: Element[T], predicate: T => Boolean): (Double, Double) = { | ||
check(target) | ||
doProbabilityBounds(target, predicate) | ||
} | ||
|
||
def probabilityBounds[T](target: Element[T], value: T): (Double, Double) = { | ||
check(target) | ||
doProbabilityBounds(target, (t: T) => t == value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
Figaro/src/main/scala/com/cra/figaro/algorithm/OneTimeBoundsProbQuery.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* OneTimeBoundsProbQuery.scala | ||
* One-time algorithms that compute bounds on conditional probabilities of query elements. | ||
* | ||
* Created By: William Kretschmer (kretsch@mit.edu) | ||
* Creation Date: Jun 23, 2017 | ||
* | ||
* Copyright 2017 Avrom J. Pfeffer and Charles River Analytics, Inc. | ||
* See http://www.cra.com or email figaro@cra.com for information. | ||
* | ||
* See http://www.github.com/p2t2/figaro for a copy of the software license. | ||
*/ | ||
package com.cra.figaro.algorithm | ||
|
||
import com.cra.figaro.language._ | ||
|
||
/** | ||
* One-time algorithms that compute bounds on conditional probabilities of query elements. A class that implements this | ||
* trait must implement run, computeAllProbabilityBounds, and computeExpectationBounds methods. | ||
*/ | ||
trait OneTimeBoundsProbQuery extends BoundsProbQueryAlgorithm with OneTimeProbQuery { | ||
protected def doAllProbabilityBounds[T](target: Element[T]): Stream[(Double, Double, T)] = { | ||
computeAllProbabilityBounds(target) | ||
} | ||
|
||
protected def doExpectationBounds[T](target: Element[T], function: T => Double, bounds: Option[(Double, Double)]): (Double, Double) = { | ||
computeExpectationBounds(target, function, bounds) | ||
} | ||
|
||
protected def doProbabilityBounds[T](target: Element[T], predicate: T => Boolean): (Double, Double) = { | ||
computeProbabilityBounds(target, predicate) | ||
} | ||
} |
Oops, something went wrong.