Last active
December 10, 2015 01:09
-
-
Save melpomene/4356281 to your computer and use it in GitHub Desktop.
Calculates the probability of a distribution of multidimensional stocastic variables (binominal distrubution).
This file contains hidden or 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
| package probability | |
| import scala.math | |
| /*** | |
| * Calculates the probability of a distribution of multidimensional stocastic variables (binominal distrubution). | |
| * | |
| * param n: number of events | |
| * param probabilityList: Array of the probabilites of the different event | |
| * param occurance: Array of number of occurances we want to know the probability of | |
| * | |
| * called with i.e. multidimensionalBinProb(12, Array(0.5, 0.5), Array(6,6)) | |
| */ | |
| object utils { | |
| def factorial(number : Int) : Int = { | |
| number match { | |
| case 1 => 1 | |
| case 0 => 0 | |
| case x if x < 0 => throw new IllegalArgumentException("Only values larger than 0") | |
| case _ => number * factorial(number - 1) | |
| } | |
| } | |
| def multidimensionalBinProb(n: Int, probabilityList : Array[Double], occurance : Array[Int]) : Double = { | |
| assert(probabilityList.length == occurance.length, println("Not of equal length")) | |
| val totalK : Double = occurance.map(factorial(_)).foldLeft(1)(_*_) | |
| var probList : Array[Double] = Array() | |
| for (i <- 0 until probabilityList.length) probList = probList :+ math.pow(probabilityList(i),occurance(i)) | |
| val totalProb = probList.foldLeft(1.0)(_*_) | |
| (factorial(n) / totalK ) * totalProb | |
| } | |
| def main(string : Array[String]) { | |
| println(multidimensionalBinProb(12, Array(0.5, 0.5), Array(6,6))) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment