Diversity prediction theorem: Difference between revisions

m
syntax highlighting fixup automation
(Ada version)
m (syntax highlighting fixup automation)
Line 43:
=={{header|11l}}==
{{trans|C++}}
<langsyntaxhighlight lang="11l">F average_square_diff(a, predictions)
R sum(predictions.map(x -> (x - @a) ^ 2)) / predictions.len
 
Line 53:
 
diversity_theorem(49.0, [Float(48), 47, 51])
diversity_theorem(49.0, [Float(48), 47, 51, 42])</langsyntaxhighlight>
{{out}}
<pre>
Line 66:
=={{header|Ada}}==
{{trans|C}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Command_Line;
 
Line 125:
Diversity (Truth, Estimates);
end;
end Diversity_Prediction;</langsyntaxhighlight>
{{out}}
<pre>
Line 140:
=={{header|C}}==
Accepts inputs from command line, prints out usage on incorrect invocation.
<syntaxhighlight lang="c">
<lang C>
 
#include<string.h>
Line 211:
return 0;
}
</syntaxhighlight>
</lang>
Invocation and Output :
<pre>
Line 225:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">
using System;
using System.Linq;
Line 248:
DiversityTheorem(49, new []{48d,47,51,42});
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 260:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
<lang Cpp>
#include <iostream>
#include <vector>
Line 302:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 315:
=={{header|Clojure}}==
John Lawrence Aspden's code posted on [http://www.learningclojure.com/2013/08/diversity-prediction-theorem.html Diversity Prediction Theorem].
<syntaxhighlight lang="clojure">
<lang Clojure>
(defn diversity-theorem [truth predictions]
(let [square (fn[x] (* x x))
Line 326:
(println (diversity-theorem 49 '(48 47 51)))
(println (diversity-theorem 49 '(48 47 51 42)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 335:
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.stdio;
 
Line 355:
diversityTheorem(49.0, [48.0, 47.0, 51.0]);
diversityTheorem(49.0, [48.0, 47.0, 51.0, 42.0]);
}</langsyntaxhighlight>
{{out}}
<pre>average-error: 3
Line 367:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: kernel math math.statistics math.vectors prettyprint ;
 
TUPLE: div avg-err crowd-err diversity ;
Line 376:
 
49 { 48 47 51 } diversity .
49 { 48 47 51 42 } diversity .</langsyntaxhighlight>
{{out}}
<pre>
Line 392:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 425:
fmt.Printf("Diversity : %6.3f\n\n", div)
}
}</langsyntaxhighlight>
 
{{out}}
Line 440:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class DiversityPredictionTheorem {
private static double square(double d) {
return d * d
Line 463:
println(diversityTheorem(49.0, [48.0, 47.0, 51.0, 42.0] as double[]))
}
}</langsyntaxhighlight>
{{out}}
<pre>average-error : 3.000
Line 475:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">mean :: (Fractional a, Foldable t) => t a -> a
mean lst = sum lst / fromIntegral (length lst)
 
Line 490:
putStrLn $ "CrowdError:\t" ++ show crowderr
let diversity = meanSq avg estimates
putStrLn $ "Diversity:\t" ++ show diversity</langsyntaxhighlight>
 
<pre>λ> diversityPrediction 49 [48,47,51]
Line 509:
Accepts inputs from command line, prints out usage on incorrect invocation.
Were this compressed adaptation from C the content of file <tt>d.ijs</tt>
<syntaxhighlight lang="c">
<lang C>
echo 'Use: ' , (;:inv 2 {. ARGV) , ' <reference value> <observations>'
 
Line 526:
 
exit 0
</syntaxhighlight>
</lang>
example uses follow
<pre>
Line 569:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class DiversityPredictionTheorem {
Line 596:
System.out.println(diversityTheorem(49.0, new double[]{48.0, 47.0, 51.0, 42.0}));
}
}</langsyntaxhighlight>
{{out}}
<pre>average-error : 3.000
Line 608:
=={{header|JavaScript}}==
===ES5===
<langsyntaxhighlight JavaScriptlang="javascript">'use strict';
 
function sum(array) {
Line 641:
console.log(diversityTheorem(49, [48,47,51]))
console.log(diversityTheorem(49, [48,47,51,42]))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 651:
 
===ES6===
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 722:
// MAIN ---
return main()
})();</langsyntaxhighlight>
{{Out}}
<pre>[
Line 739:
=={{header|Jsish}}==
From Typescript entry.
<langsyntaxhighlight lang="javascript">/* Diverisity Prediction Theorem, in Jsish */
"use strict";
 
Line 775:
diversityTheorem(49, [48,47,51,42]) ==> { "average-error":14.5, "crowd-error":4, diversity:10.5 }
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 785:
 
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq">def diversitytheorem($actual; $predicted):
def mean: add/length;
 
Line 791:
| { avgerr: ($predicted | map(. - $actual) | map(pow(.; 2)) | mean),
crderr: pow($mean - $actual; 2),
divers: ($predicted | map(. - $mean) | map(pow(.;2)) | mean) } ;</langsyntaxhighlight><syntaxhighlight lang ="jq"># The task:
([49, [48, 47, 51]],
[49, [48, 47, 51, 42]
])
| . as [$actual, $predicted]
| diversitytheorem($actual; $predicted)</langsyntaxhighlight>
 
{{out}}
Line 813:
=={{header|Julia}}==
{{works with|Julia|1.2}}
<langsyntaxhighlight lang="julia">import Statistics: mean
 
function diversitytheorem(truth::T, pred::Vector{T}) where T<:Number
Line 831:
diversity : $divers
""")
end</langsyntaxhighlight>
 
{{out}}
Line 845:
=={{header|Kotlin}}==
{{trans|TypeScript}}
<langsyntaxhighlight lang="scala">// version 1.1.4-3
 
fun square(d: Double) = d * d
Line 863:
println(diversityTheorem(49.0, doubleArrayOf(48.0, 47.0, 51.0)))
println(diversityTheorem(49.0, doubleArrayOf(48.0, 47.0, 51.0, 42.0)))
}</langsyntaxhighlight>
 
{{out}}
Line 878:
=={{header|Lua}}==
{{trans|C++}}
<langsyntaxhighlight lang="lua">function square(x)
return x * x
end
Line 912:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>average-error: 3
Line 922:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[DiversityPredictionTheorem]
DiversityPredictionTheorem[trueval_?NumericQ, estimates_List] :=
Module[{avg, avgerr, crowderr, diversity},
Line 938:
]
DiversityPredictionTheorem[49, {48, 47, 51}] // Dataset
DiversityPredictionTheorem[49, {48, 47, 51, 42}] // Dataset</langsyntaxhighlight>
{{out}}
<pre>TrueValue 49
Line 953:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, math, stats
 
func meanSquareDiff(refValue: float; estimates: seq[float]): float =
Line 973:
echo "Crowd error: ", (m - trueValue)^2
echo "Prediction diversity: ", meanSquareDiff(m, estimates)
echo ""</langsyntaxhighlight>
 
{{out}}
Line 989:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub diversity {
my($truth, @pred) = @_;
my($ae,$ce,$cp,$pd,$stats);
Line 1,012:
 
print diversity(49, qw<48 47 51>) . "\n";
print diversity(49, qw<48 47 51 42>);</langsyntaxhighlight>
{{out}}
<pre>average-error: 3.000
Line 1,023:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 1,051:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">49</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">48</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">47</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">51</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">49</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">48</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">47</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">51</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">42</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,063:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Define.f ref=49.0, mea
NewList argV.f()
 
Line 1,103:
LetArgV(48.0) : LetArgV(47.0) : LetArgV(51.0) : LetArgV(42.0)
mea=mean(argV()) : put
Return</langsyntaxhighlight>
{{out}}
<pre>[49] 48 47 51
Line 1,118:
By composition of pure functions:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Diversity prediction theorem'''
 
from itertools import chain
Line 1,376:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,405:
=={{header|R}}==
R's vectorisation shines here. The hardest part of this task was giving each estimate its own numbered column, which is little more than a printing luxury. The actual mathematics was trivial, with each part done in essentially one line.
<langsyntaxhighlight lang="rsplus">diversityStats <- function(trueValue, estimates)
{
collectivePrediction <- mean(estimates)
Line 1,415:
}
diversityStats(49, c(48, 47, 51))
diversityStats(49, c(48, 47, 51, 42))</langsyntaxhighlight>
{{out}}
<pre>> diversityStats(49, c(48, 47, 51))
Line 1,429:
{{trans|Clojure}}
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define (mean l)
Line 1,443:
(println (diversity-theorem 49 '(48 47 51)))
(println (diversity-theorem 49 '(48 47 51 42)))</langsyntaxhighlight>
 
{{out}}
Line 1,452:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub diversity-calc($truth, @pred) {
my $ae = avg-error($truth, @pred); # average individual error
my $cp = ([+] @pred)/+@pred; # collective prediction
Line 1,471:
 
.say for diversity-format diversity-calc(49, <48 47 51>);
.say for diversity-format diversity-calc(49, <48 47 51 42>);</langsyntaxhighlight>
 
{{out}}
Line 1,484:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX */
Numeric Digits 20
Call diversityTheorem 49,'48 47 51'
Line 1,514:
res=res+(x-a)**2 /* accumulate square of differences */
End
Return res/words(list) /* return the average */</langsyntaxhighlight>
{{out}}
<pre>average-error=3
Line 1,527:
Uses greater precision, but rounds the output to six decimal digits past the decimal
point &nbsp; (see the last comment in the program).
<langsyntaxhighlight lang="rexx">/*REXX program calculates the average error, crowd error, and prediction diversity. */
numeric digits 50 /*use precision of fifty decimal digits*/
call diversity 49, 48 47 51 /*true value and the crowd predictions.*/
Line 1,541:
say ' the crowd error: ' format( (true-a) **2, , 6) / 1
say 'prediction diversity: ' format( avgSD(a) , , 6) / 1; say; say
return /* └─── show 6 dec. digs.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,558:
=={{header|Ruby}}==
{{trans|D}}
<langsyntaxhighlight lang="ruby">def mean(a) = a.sum(0.0) / a.size
def mean_square_diff(a, predictions) = mean(predictions.map { |x| square(x - a)**2 })
Line 1,570:
diversity_theorem(49.0, [48.0, 47.0, 51.0])
diversity_theorem(49.0, [48.0, 47.0, 51.0, 42.0])</langsyntaxhighlight>
{{out}}
<pre>truth: 49.0, predictions [48.0, 47.0, 51.0]
Line 1,584:
=={{header|Scala}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="scala">object DiversityPredictionTheorem {
def square(d: Double): Double
= d * d
Line 1,605:
println(diversityTheorem(49.0, Array(48.0, 47.0, 51.0, 42.0)))
}
}</langsyntaxhighlight>
{{out}}
<pre>average-error : 3.000
Line 1,617:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func avg_error(m, v) {
v.map { (_ - m)**2 }.sum / v.len
}
Line 1,638:
 
diversity_format(diversity_calc(49, [48, 47, 51])).each{.say}
diversity_format(diversity_calc(49, [48, 47, 51, 42])).each{.say}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,650:
 
=={{header|TypeScript}}==
<syntaxhighlight lang="typescript">
<lang TypeScript>
function sum(array: Array<number>): number {
return array.reduce((a, b) => a + b)
Line 1,678:
console.log(diversityTheorem(49, [48,47,51]))
console.log(diversityTheorem(49, [48,47,51,42]))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,689:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Function Square(x As Double) As Double
Line 1,711:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>average-error: 3
Line 1,722:
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var averageSquareDiff = Fn.new { |f, preds|
Line 1,745:
Fmt.print("Crowd-error : $6.3f", res[1])
Fmt.print("Diversity : $6.3f\n", res[2])
}</langsyntaxhighlight>
 
{{out}}
Line 1,760:
=={{header|zkl}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="zkl">fcn avgError(m,v){ v.apply('wrap(n){ (n - m).pow(2) }).sum(0.0)/v.len() }
fcn diversityCalc(truth,pred){ //(Float,List of Float)
Line 1,771:
T("average-error","crowd-error","diversity").zip(stats)
.pump(String,Void.Xplode,"%13s :%7.3f\n".fmt)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">diversityCalc(49.0, T(48.0,47.0,51.0)) : diversityFormat(_).println();
diversityCalc(49.0, T(48.0,47.0,51.0,42.0)) : diversityFormat(_).println();</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits