Skip to content

Instantly share code, notes, and snippets.

@brydavis
Created March 15, 2016 23:32
Show Gist options
  • Save brydavis/3336d9048d43da21c8c3 to your computer and use it in GitHub Desktop.
Save brydavis/3336d9048d43da21c8c3 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
type Series []float64
func main() {
// s := Series{23.6, 76.1, 89.0, 12.8}
// sample := Series{206.0, 76.0, -224.0, 36.0, -94.0}
// sample := Series{26.0, 33.0, 65.0, 28.0, 34.0, 55.0, 25.0, 44.0, 50.0, 36.0, 26.0, 37.0, 43.0, 62.0, 35.0, 38.0, 45.0, 32.0, 28.0, 34.0}
// fmt.Println(sample.Mean())
// fmt.Println(sample.Len())
// fmt.Println(sample.StdDev())
// fmt.Println(sample.Z(65))
scores := Series{}
rand.Seed(time.Now().Unix())
for i := 0; i < 100000; i++ {
scores = append(scores, Round(rand.NormFloat64(), 1))
}
// freq := map[float64]int{}
// for _, z := range scores {
// freq[z] += 1
// }
// fmt.Println(scores.Freq())
// z := Round((rand.Float64()*6.)-3., 1)
z := Round(rand.NormFloat64(), 1)
fmt.Println(z)
hist := scores.Freq()
for x := -3.0; x <= 3.0; x += 0.1 {
v := int(hist[Round(x, 1)] / 100)
v2 := int(hist[Round(x+0.1, 1)] / 100)
s := ""
for j := 0; j < v; j++ {
if Round(x, 1) <= z {
s += "|_"
} else {
s += " "
}
}
fmt.Printf("\t%0.1f\t%s|\n", x, s)
s = ""
for j := 0; j < v2; j++ {
if Round(x, 1) < z {
s += " _"
} else {
s += " "
}
}
fmt.Printf("\t \t%s\n", s)
// if Round(x, 1) == z {
// fmt.Printf("\t%0.1f\t%sO\n", x, s)
// } else {
// }
}
}
func (s Series) Mean() float64 {
return s.Sum() / s.Len()
}
func (s Series) Sum() (total float64) {
for _, f := range s {
total += f
}
return
}
func (s Series) Len() float64 {
return float64(len(s))
}
func (s Series) SumSquares() (total float64) {
mean := s.Mean()
for _, f := range s {
total += math.Pow((f - mean), 2)
}
return
}
func (s Series) StdDev() float64 {
return math.Sqrt(s.SumSquares() / (s.Len()))
}
func (s Series) Z(x float64) float64 {
return (x - s.Mean()) / s.StdDev()
}
func Round(f float64, places int) float64 {
shift := math.Pow(10, float64(places))
f = f * shift
return math.Floor(f+.5) / shift
}
func (s Series) Freq() map[float64]int {
freq := map[float64]int{}
for _, z := range s {
freq[z] += 1
}
return freq
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment