Skip to content

Instantly share code, notes, and snippets.

@shubham0204
Created April 24, 2021 14:17
Show Gist options
  • Save shubham0204/13fbb554a32982a2ea30c9c227f106d0 to your computer and use it in GitHub Desktop.
Save shubham0204/13fbb554a32982a2ea30c9c227f106d0 to your computer and use it in GitHub Desktop.
// A Gaussian Distribution which will be constructed for every feature, given the mean and standard deviation.
class GaussianDistribution( var mean : Float , var stdDev : Float ) {
private val p = 1f / ( sqrt( 2.0 * Math.PI ) ).toFloat()
// Get the likelihood for given x.
fun getProb( x : Float ) : Float {
val exp = exp( -0.5 * ((x - mean) / stdDev ).pow( 2 ) ).toFloat()
return ( 1 / stdDev ) * p * exp
}
// Get the log of the likelihood for given x.
fun getLogProb( x : Float ) : Float {
return log10( getProb( x ) )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment