-
Notifications
You must be signed in to change notification settings - Fork 153
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 #495 from gtakata/DEV3.2.1.1
Add DataReduction methods to reduce distribution
- Loading branch information
Showing
7 changed files
with
120 additions
and
22 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
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
53 changes: 53 additions & 0 deletions
53
Figaro/src/main/scala/com/cra/figaro/util/visualization/reduction/DataReduction.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,53 @@ | ||
package com.cra.figaro.util.visualization.reduction | ||
|
||
import scala.collection._ | ||
|
||
/** | ||
* @author gtakata | ||
*/ | ||
object DataReduction { | ||
def binToDistribution(data: List[(Double, Double)]): List[(Double, Double)] = { | ||
if (data.size > 50) { | ||
var mean = 0.0 | ||
var totalProb = 0.0 | ||
var count = 0 | ||
var min = Double.MaxValue | ||
var max = Double.MinValue | ||
var ss = 0.0 | ||
|
||
for ((prob, value) <- data) { | ||
totalProb += prob | ||
mean += prob * value | ||
min = math.min(min, value) | ||
max = math.max(max, value) | ||
ss += prob * value * value | ||
count += 1 | ||
} | ||
|
||
val variance = ss - mean * mean | ||
val sd = math.sqrt(variance) | ||
|
||
val distMax = mean + 3 * sd | ||
val distMin = mean - 3 * sd | ||
|
||
val nInterval = math.min(count, 300) | ||
val interval = (distMax - distMin) / nInterval | ||
var dist = Array.fill[Double](nInterval)(0) | ||
|
||
for ((prob, value) <- data) { | ||
val pos = math.max(math.min(math.floor((value - distMin) / interval).toInt, nInterval - 1), 0) | ||
val posProb = dist(pos) | ||
dist(pos) = posProb + prob | ||
} | ||
|
||
val probDist = for (i <- 1 to nInterval) yield { | ||
val value = distMin + i * interval | ||
(dist(i - 1), value) | ||
} | ||
|
||
probDist.toList | ||
} else { | ||
data | ||
} | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
FigaroExamples/src/main/scala/com/cra/figaro/example/visualization/Regression.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,48 @@ | ||
/* | ||
* Regression.scala | ||
* A Bayesian network example with visualization | ||
* | ||
* Created By: Glenn Takata (gtakata@cra.com) | ||
* Creation Date: Jul 7, 2015 | ||
* | ||
* Copyright 2015 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.example.visualization | ||
|
||
import com.cra.figaro.language.{Universe} | ||
import com.cra.figaro.algorithm.sampling.{Importance} | ||
import com.cra.figaro.library.atomic.continuous.{Normal, Uniform} | ||
import com.cra.figaro.util.visualization.ResultsGUI | ||
|
||
/** | ||
* @author Glenn Takata (gtakata@cra.com) | ||
*/ | ||
object Regression { | ||
Universe.createNew() | ||
|
||
private val mean = Uniform(0, 1) | ||
|
||
for (_ <- 0 until 100) { | ||
val n = Normal(mean, 1.0) | ||
n.addConstraint((m: Double) => m + 5) | ||
} | ||
|
||
|
||
def main(args: Array[String]) { | ||
val gui = ResultsGUI | ||
gui.startup(args) | ||
|
||
val alg = Importance(1000, mean) | ||
alg.start() | ||
|
||
val dist = alg.distribution(mean) | ||
println("Probability of mean: " + dist.toList) | ||
|
||
gui.addResult("mean", alg.distribution(mean).toList) | ||
|
||
alg.kill | ||
} | ||
} |