Statistics/Normal distribution: Difference between revisions
Content added Content deleted
(Added Kotlin) |
|||
Line 1,207: | Line 1,207: | ||
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 normalStats(sampleSize: Int) { |
|||
if (sampleSize < 1) return |
|||
val r = DoubleArray(sampleSize) |
|||
val h = IntArray(12) // all zero by default |
|||
/* |
|||
Generate 'sampleSize' normally distributed random numbers with mean 0.5 and SD 0.25 |
|||
and calculate in which box they will fall when drawing the histogram |
|||
*/ |
|||
for (i in 0 until sampleSize) { |
|||
r[i] = 0.5 + rand.nextGaussian() / 4.0 |
|||
when { |
|||
r[i] < 0.0 -> h[0]++ |
|||
r[i] >= 1.0 -> h[11]++ |
|||
else -> h[1 + (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..11) { |
|||
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 > 300 then normalize histogram to 300 |
|||
val scale = if (sampleSize <= 300) 1.0 else 300.0 / sampleSize |
|||
println("Sample size $sampleSize\n") |
|||
println(" Mean ${"%1.6f".format(mean)} SD ${"%1.6f".format(sd)}\n") |
|||
for (i in 0..11) { |
|||
when (i) { |
|||
0 -> print("< 0.00 : ") |
|||
11 -> print(">=1.00 : ") |
|||
else -> 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) normalStats(sampleSize) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Sample size 100 |
|||
Mean 0.525211 SD 0.266316 |
|||
< 0.00 : 3 *** |
|||
0.10 : 1 * |
|||
0.20 : 3 *** |
|||
0.30 : 11 *********** |
|||
0.40 : 14 ************** |
|||
0.50 : 13 ************* |
|||
0.60 : 15 *************** |
|||
0.70 : 13 ************* |
|||
0.80 : 10 ********** |
|||
0.90 : 11 *********** |
|||
1.00 : 4 **** |
|||
>=1.00 : 2 ** |
|||
Sample size 1000 |
|||
Mean 0.500948 SD 0.255757 |
|||
< 0.00 : 29 ********* |
|||
0.10 : 35 *********** |
|||
0.20 : 70 ********************* |
|||
0.30 : 71 ********************* |
|||
0.40 : 138 ***************************************** |
|||
0.50 : 139 ****************************************** |
|||
0.60 : 168 ************************************************** |
|||
0.70 : 123 ************************************* |
|||
0.80 : 110 ********************************* |
|||
0.90 : 62 ******************* |
|||
1.00 : 32 ********** |
|||
>=1.00 : 23 ******* |
|||
Sample size 10000 |
|||
Mean 0.501376 SD 0.248317 |
|||
< 0.00 : 240 ******* |
|||
0.10 : 305 ********* |
|||
0.20 : 617 ******************* |
|||
0.30 : 927 **************************** |
|||
0.40 : 1291 *************************************** |
|||
0.50 : 1554 *********************************************** |
|||
0.60 : 1609 ************************************************ |
|||
0.70 : 1319 **************************************** |
|||
0.80 : 983 ***************************** |
|||
0.90 : 609 ****************** |
|||
1.00 : 324 ********** |
|||
>=1.00 : 222 ******* |
|||
Sample size 100000 |
|||
Mean 0.499427 SD 0.250533 |
|||
< 0.00 : 2341 ******* |
|||
0.10 : 3246 ********** |
|||
0.20 : 6005 ****************** |
|||
0.30 : 9718 ***************************** |
|||
0.40 : 13247 **************************************** |
|||
0.50 : 15595 *********************************************** |
|||
0.60 : 15271 ********************************************** |
|||
0.70 : 13405 **************************************** |
|||
0.80 : 9653 ***************************** |
|||
0.90 : 5990 ****************** |
|||
1.00 : 3257 ********** |
|||
>=1.00 : 2272 ******* |
|||
</pre> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |