Skip to content

Commit

Permalink
Fix linting warnings for normal distribution functions formatting and…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
montanaflynn committed Feb 18, 2020
1 parent 7ed61c7 commit 6ed7aba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 55 deletions.
46 changes: 22 additions & 24 deletions norm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func NormPpfRvs(loc float64, scale float64, size int) []float64 {
func NormBoxMullerRvs(loc float64, scale float64, size int) []float64 {
rand.Seed(time.Now().UnixNano())
var toReturn []float64
for i := 0; i < int(math.Floor(float64(size / 2)) + float64(size % 2)); i++ {
for i := 0; i < int(float64(size/2)+float64(size%2)); i++ {
// u1 and u2 are uniformly distributed random numbers between 0 and 1.
u1 := rand.Float64()
u2 := rand.Float64()
// x1 and x2 are normally distributed random numbers.
x1 := loc + (scale * (math.Sqrt(-2*math.Log(u1)) * math.Cos(2*math.Pi*u2)))
toReturn = append(toReturn, x1)
if (i + 1) * 2 <= size {
if (i+1)*2 <= size {
x2 := loc + (scale * (math.Sqrt(-2*math.Log(u1)) * math.Sin(2*math.Pi*u2)))
toReturn = append(toReturn, x2)
}
Expand All @@ -40,32 +40,32 @@ func NormBoxMullerRvs(loc float64, scale float64, size int) []float64 {

// NormPdf is the probability density function.
func NormPdf(x float64, loc float64, scale float64) float64 {
return (math.Pow(math.E, -(math.Pow(x-loc, 2)) / (2 * math.Pow(scale, 2)))) / (scale* math.Sqrt(2 * math.Pi))
return (math.Pow(math.E, -(math.Pow(x-loc, 2))/(2*math.Pow(scale, 2)))) / (scale * math.Sqrt(2*math.Pi))
}

// NormLogPdf is the log of the probability density function.
func NormLogPdf(x float64, loc float64, scale float64) float64 {
return math.Log((math.Pow(math.E, -(math.Pow(x-loc, 2)) / (2 * math.Pow(scale, 2)))) / (scale* math.Sqrt(2 * math.Pi)))
return math.Log((math.Pow(math.E, -(math.Pow(x-loc, 2))/(2*math.Pow(scale, 2)))) / (scale * math.Sqrt(2*math.Pi)))
}

// NormCdf is the cumulative distribution function.
func NormCdf(x float64, loc float64, scale float64) float64 {
return 0.5*(1 + math.Erf((x - loc) / (scale * math.Sqrt(2))))
return 0.5 * (1 + math.Erf((x-loc)/(scale*math.Sqrt(2))))
}

// NormLogCdf is the log of the cumulative distribution function.
func NormLogCdf(x float64, loc float64, scale float64) float64 {
return math.Log(0.5*(1 + math.Erf((x - loc) / (scale * math.Sqrt(2)))))
return math.Log(0.5 * (1 + math.Erf((x-loc)/(scale*math.Sqrt(2)))))
}

// NormSf is the survival function (also defined as 1 - cdf, but sf is sometimes more accurate).
func NormSf(x float64, loc float64, scale float64) float64 {
return 1 - 0.5*(1 + math.Erf((x - loc) / (scale * math.Sqrt(2))))
return 1 - 0.5*(1+math.Erf((x-loc)/(scale*math.Sqrt(2))))
}

// NormLogSf is the log of the survival function.
func NormLogSf(x float64, loc float64, scale float64) float64 {
return math.Log(1 - 0.5*(1 + math.Erf((x - loc) / (scale * math.Sqrt(2)))))
return math.Log(1 - 0.5*(1+math.Erf((x-loc)/(scale*math.Sqrt(2)))))
}

// NormPpf is the point percentile function.
Expand Down Expand Up @@ -145,15 +145,16 @@ func NormIsf(p float64, loc float64, scale float64) (x float64) {
// For more information please visit: https://math.stackexchange.com/questions/1945448/methods-for-finding-raw-moments-of-the-normal-distribution
func NormMoment(n int, loc float64, scale float64) float64 {
toReturn := 0.0
for i := 0; i < n + 1; i++ {
if (n-i) % 2 == 0 {
toReturn += float64(Ncr(n, i)) * (math.Pow(loc, float64(i))) * (math.Pow(scale, float64(n - i))) *
(float64(factorial(n - i)) / ((math.Pow(2.0, float64((n - i) / 2))) *
float64(factorial((n - i) / 2))))
for i := 0; i < n+1; i++ {
if (n-i)%2 == 0 {
toReturn += float64(Ncr(n, i)) * (math.Pow(loc, float64(i))) * (math.Pow(scale, float64(n-i))) *
(float64(factorial(n-i)) / ((math.Pow(2.0, float64((n-i)/2))) *
float64(factorial((n-i)/2))))
}
}
return toReturn
}

// NormStats returns the mean, variance, skew, and/or kurtosis.
// Mean(‘m’), variance(‘v’), skew(‘s’), and/or kurtosis(‘k’).
// Takes string containing any of 'mvsk'.
Expand All @@ -177,24 +178,23 @@ func NormStats(loc float64, scale float64, moments string) []float64 {

// NormEntropy is the differential entropy of the RV.
func NormEntropy(loc float64, scale float64) float64 {
return math.Log(scale * math.Sqrt(2 * math.Pi * math.E))
return math.Log(scale * math.Sqrt(2*math.Pi*math.E))
}

// NormFit returns the maximum likelihood estimators for the Normal Distribution.
// Takes array of float64 values.
// Returns array of Mean followed by Standard Deviation.
func NormFit(data []float64) [2]float64{
func NormFit(data []float64) [2]float64 {
sum := 0.00
mean := 0.00
for i := 0; i < len(data); i++ {
sum += data[i]
}
mean = sum / float64(len(data))
mean := sum / float64(len(data))
stdNumerator := 0.00
for i := 0; i < len(data); i++ {
stdNumerator += math.Pow(data[i]-mean, 2)
}
return [2]float64{mean , math.Sqrt((stdNumerator)/(float64(len(data))))}
return [2]float64{mean, math.Sqrt((stdNumerator) / (float64(len(data))))}
}

// NormMedian is the median of the distribution.
Expand All @@ -218,9 +218,9 @@ func NormStd(loc float64, scale float64) float64 {
}

// NormInterval finds endpoints of the range that contains alpha percent of the distribution.
func NormInterval(alpha float64, loc float64, scale float64 ) [2]float64 {
q1 := (1.0-alpha)/2
q2 := (1.0+alpha)/2
func NormInterval(alpha float64, loc float64, scale float64) [2]float64 {
q1 := (1.0 - alpha) / 2
q2 := (1.0 + alpha) / 2
a := NormPpf(q1, loc, scale)
b := NormPpf(q2, loc, scale)
return [2]float64{a, b}
Expand All @@ -231,7 +231,7 @@ func factorial(x int) int {
if x == 0 {
return 1
}
return x * factorial(x - 1)
return x * factorial(x-1)
}

// Ncr is an N choose R algorithm.
Expand All @@ -252,5 +252,3 @@ func Ncr(n, r int) int {
}
return ret
}


43 changes: 12 additions & 31 deletions norm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,6 @@ import (
"testing"
)

func testEq(a, b []float64) bool {

// If one is nil, the other must also be nil.
if (a == nil) != (b == nil) {
return false;
}

if len(a) != len(b) {
return false
}

for i := range a {
if a[i] != b[i] {
return false
}
}

return true
}

func TestNormPpf(t *testing.T) {
if NormPpf(0.5, 0, 1) != 0 {
t.Error("Input 0.5, Expected 0")
Expand All @@ -36,13 +16,14 @@ func TestNormPpf(t *testing.T) {
if NormPpf(0.002423, 0, 1) != -2.817096255323953 {
t.Error("Input 0.002423, Expected -2.817096255323953")
}
if NormPpf(1 - 0.002423, 0, 1) != 2.817096255323956 {
if NormPpf(1-0.002423, 0, 1) != 2.817096255323956 {
t.Error("Input 1 - 0.002423, Expected 2.817096255323956")
}
if NormPpf(1.1, 0, 1) == math.NaN() {

if !math.IsNaN(NormPpf(1.1, 0, 1)) {
t.Error("Input 1.1, Expected NaN")
}
if NormPpf(-0.1, 0, 1) == math.NaN() {
if !math.IsNaN(NormPpf(-1.1, 0, 1)) {
t.Error("Input -0.1, Expected Nan")
}
if NormPpf(0, 0, 1) != -math.Inf(1) {
Expand Down Expand Up @@ -126,34 +107,34 @@ func TestNormMoment(t *testing.T) {
}

func TestNormStats(t *testing.T) {
if !reflect.DeepEqual(NormStats(0, 1, "m"), []float64{0}){
if !reflect.DeepEqual(NormStats(0, 1, "m"), []float64{0}) {
t.Error("Input 'm' , Expected 0")
}
if !reflect.DeepEqual(NormStats(0, 1, "v"), []float64{1}){
if !reflect.DeepEqual(NormStats(0, 1, "v"), []float64{1}) {
t.Error("Input 'v' , Expected 1")
}
if !reflect.DeepEqual(NormStats(0, 1, "s"), []float64{0}){
if !reflect.DeepEqual(NormStats(0, 1, "s"), []float64{0}) {
t.Error("Input 's' , Expected 0")
}
if !reflect.DeepEqual(NormStats(0, 1, "k"), []float64{0}){
if !reflect.DeepEqual(NormStats(0, 1, "k"), []float64{0}) {
t.Error("Input 'k' , Expected 0")
}
}

func TestNormEntropy(t *testing.T) {
if NormEntropy( 0, 1) != 1.4189385332046727 {
if NormEntropy(0, 1) != 1.4189385332046727 {
t.Error("Input ( 0 , 1 ), Expected 1.4189385332046727")
}
}

func TestNormFit(t *testing.T) {
if !reflect.DeepEqual(NormFit( []float64{0,2,3,4}), [2]float64{2.25, 1.479019945774904}) {
if !reflect.DeepEqual(NormFit([]float64{0, 2, 3, 4}), [2]float64{2.25, 1.479019945774904}) {
t.Error("Input (0,2,3,4), Expected {2.25, 1.479019945774904}")
}
}

func TestNormInterval(t *testing.T) {
if !reflect.DeepEqual(NormInterval(0.5, 0, 1), [2]float64{-0.6744897501960818, 0.674489750196082}){
if !reflect.DeepEqual(NormInterval(0.5, 0, 1), [2]float64{-0.6744897501960818, 0.674489750196082}) {
t.Error("Input (50 % ), Expected {-0.6744897501960818, 0.674489750196082}")
}
}
Expand Down Expand Up @@ -198,7 +179,7 @@ func TestNcr(t *testing.T) {
if Ncr(4, 1) != 4 {
t.Error("Input 4 choose 1, Expected 4")
}
if Ncr(4, 3) != 4{
if Ncr(4, 3) != 4 {
t.Error("Input 4 choose 3, Expected 4")
}
}

0 comments on commit 6ed7aba

Please sign in to comment.