Jump to content

Statistics/Basic: Difference between revisions

Go solution
(J: closer reading of task requirements)
(Go solution)
Line 119:
}
}</lang>
=={{header|Go}}==
<lang go>package main
 
import (
"fmt"
"math"
"rand"
"strings"
)
 
func main() {
sample(100)
sample(1000)
sample(10000)
}
 
func sample(n int) {
// generate data
d := make([]float64, n)
for i := range d {
d[i] = rand.Float64()
}
// show mean, standard deviation
var sum, ssq float64
for _, s := range d {
sum += s
ssq += s * s
}
fmt.Println(n, "numbers")
m := sum / float64(n)
fmt.Println("Mean: ", m)
fmt.Println("Stddev:", math.Sqrt(ssq/float64(n)-m*m))
// show histogram
h := make([]int, 10)
for _, s := range d {
h[int(s*10)]++
}
for _, c := range h {
fmt.Println(strings.Repeat("*", c*205/int(n)))
}
fmt.Println()
}</lang>
Output:
<pre>
100 numbers
Mean: 0.5231064889267764
Stddev: 0.292668237816841
****************
****************
************************
**********************
******************
******************
****************
**************************
************************
********************
 
1000 numbers
Mean: 0.496026080160094
Stddev: 0.2880988956436907
*********************
********************
*****************
***********************
******************
**********************
********************
*********************
******************
*******************
 
10000 numbers
Mean: 0.5009091903581223
Stddev: 0.289269693719711
*******************
********************
********************
********************
*********************
********************
*******************
*******************
********************
*********************
</pre>
For the extra, here is an outline of a map reduce strategy. The main task indicated that numbers should be generated before doing any computations on them. Consistent with that, The function getSegment returns data based on a starting and ending index, as if it were accessing some large data store.
<lang go>package main
 
import (
"fmt"
"math"
"rand"
"strings"
)
 
func main() {
bigSample(1e7)
}
 
func bigSample(n int64) {
sum, ssq, h := reduce(0, n)
// compute final statistics and output as above
fmt.Println(n, "numbers")
m := sum / float64(n)
fmt.Println("Mean: ", m)
fmt.Println("Stddev:", math.Sqrt(ssq/float64(n)-m*m))
for _, c := range h {
fmt.Println(strings.Repeat("*", c*205/int(n)))
}
fmt.Println()
}
 
const threshold = 1e6
 
func reduce(start, end int64) (sum, ssq float64, h []int) {
n := end - start
if n < threshold {
d := getSegment(start, end)
return computeSegment(d)
}
// map to two sub problems
half := (start + end) / 2
sum1, ssq1, h1 := reduce(start, half)
sum2, ssq2, h2 := reduce(half, end)
// combine results
for i, c := range h2 {
h1[i] += c
}
return sum1 + sum2, ssq1 + ssq2, h1
}
 
func getSegment(start, end int64) []float64 {
d := make([]float64, end-start)
for i := range d {
d[i] = rand.Float64()
}
return d
}
 
func computeSegment(d []float64) (sum, ssq float64, h []int) {
for _, s := range d {
sum += s
ssq += s * s
}
h = make([]int, 10)
for _, s := range d {
h[int(s*10)]++
}
return
}</lang>
Output:
<pre>
10000000 numbers
Mean: 0.4999673191148989
Stddev: 0.2886663876567514
********************
********************
********************
********************
********************
********************
********************
********************
********************
********************
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
1,707

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.