Statistics/Basic: Difference between revisions

Content added Content deleted
(Added Kotlin)
Line 1,870: Line 1,870:
0.8: **************************************************
0.8: **************************************************
0.9: **************************************************</pre>
0.9: **************************************************</pre>

=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<lang scala>// version 1.1.2

val rand = java.util.Random()

fun basicStats(sampleSize: Int) {
if (sampleSize < 1) return
val r = DoubleArray(sampleSize)
val h = IntArray(10) // all zero by default
/*
Generate 'sampleSize' random numbers in the interval [0, 1)
and calculate in which box they will fall when drawing the histogram
*/
for (i in 0 until sampleSize) {
r[i] = rand.nextDouble()
h[(r[i] * 10).toInt()]++
}

// adjust one of the h[] values if necessary to ensure they sum to sampleSize
val adj = sampleSize - h.sum()
if (adj != 0) {
for (i in 0..9) {
h[i] += adj
if (h[i] >= 0) break
h[i] -= adj
}
}

val mean = r.average()
val sd = Math.sqrt(r.map { (it - mean) * (it - mean) }.average())
// Draw a histogram of the data with interval 0.1
var numStars: Int
// If sample size > 500 then normalize histogram to 500
val scale = if (sampleSize <= 500) 1.0 else 500.0 / sampleSize
println("Sample size $sampleSize\n")
println(" Mean ${"%1.6f".format(mean)} SD ${"%1.6f".format(sd)}\n")
for (i in 0..9) {
print(" %1.2f : ".format(i / 10.0))
print("%5d ".format(h[i]))
numStars = (h[i] * scale + 0.5).toInt()
println("*".repeat(numStars))
}
println()
}

fun main(args: Array<String>) {
val sampleSizes = intArrayOf(100, 1_000, 10_000, 100_000)
for (sampleSize in sampleSizes) basicStats(sampleSize)
}</lang>
Sample run:
{{out}}
<pre>
Sample size 100

Mean 0.489679 SD 0.286151

0.00 : 12 ************
0.10 : 7 *******
0.20 : 13 *************
0.30 : 9 *********
0.40 : 10 **********
0.50 : 8 ********
0.60 : 14 **************
0.70 : 10 **********
0.80 : 8 ********
0.90 : 9 *********

Sample size 1000

Mean 0.497003 SD 0.290002

0.00 : 104 ****************************************************
0.10 : 92 **********************************************
0.20 : 107 ******************************************************
0.30 : 109 *******************************************************
0.40 : 96 ************************************************
0.50 : 111 ********************************************************
0.60 : 87 ********************************************
0.70 : 79 ****************************************
0.80 : 117 ***********************************************************
0.90 : 98 *************************************************

Sample size 10000

Mean 0.505243 SD 0.288944

0.00 : 991 **************************************************
0.10 : 938 ***********************************************
0.20 : 1034 ****************************************************
0.30 : 958 ************************************************
0.40 : 963 ************************************************
0.50 : 1003 **************************************************
0.60 : 1081 ******************************************************
0.70 : 995 **************************************************
0.80 : 1001 **************************************************
0.90 : 1036 ****************************************************

Sample size 100000

Mean 0.500501 SD 0.288766

0.00 : 10015 **************************************************
0.10 : 9844 *************************************************
0.20 : 10012 **************************************************
0.30 : 10160 ***************************************************
0.40 : 10051 **************************************************
0.50 : 9938 **************************************************
0.60 : 9934 **************************************************
0.70 : 9914 **************************************************
0.80 : 10057 **************************************************
0.90 : 10075 **************************************************
</pre>


=={{header|Lasso}}==
=={{header|Lasso}}==