Probabilistic choice: Difference between revisions

Added Easylang
(Added Easylang)
 
(9 intermediate revisions by 5 users not shown)
Line 942:
stdout.print(item, "\t", timesFound[item] / trials, "\t", probTable[item], "\n")
}</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
name$[] = [ "aleph " "beth " "gimel " "daleth" "he " "waw " "zayin " "heth " ]
probs[] = [ 1 / 5 1 / 6 1 / 7 1 / 8 1 / 9 1 / 10 1 / 11 0 ]
for i = 1 to 7
cum += probs[i]
cum[] &= cum
.
cum[] &= 1
probs[8] = 1 - cum[7]
len act[] 8
n = 1000000
for i to n
h = randomf
j = 1
while h > cum[j]
j += 1
.
act[j] += 1
.
print "Name Ratio Expected"
print "---------------------"
numfmt 4 6
for i to 8
print name$[i] & " " & act[i] / n & " " & probs[i]
.
</syntaxhighlight>
 
{{out}}
<pre>
Name Ratio Expected
---------------------
aleph 0.2000 0.2000
beth 0.1661 0.1667
gimel 0.1424 0.1429
daleth 0.1252 0.1250
he 0.1111 0.1111
waw 0.1003 0.1000
zayin 0.0913 0.0909
heth 0.0637 0.0635
</pre>
 
=={{header|Elixir}}==
Line 1,435 ⟶ 1,477:
he 0.110772 0.111111
waw 0.100236 0.100000
zayin> 0.090664 0.090909
heth 0.063412 0.063456
-------- --------
1.000000 1.000000
</pre>
 
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_elements = 8
 
local fn ProbabilisticChoice
double prob(_elements), cumulative(_elements)
Str15 item(_elements)
double r, p, sum = 0, checksum = 0
long i, j, samples = 1000000
item(1) = "aleph" : item(2) = "beth" : item(3) = "gimel" : item(4) = "daleth"
item(5) = "he" : item(6) = "waw" : item(7) = "zayin" : item(8) = "heth"
prob(1) = 1/5.0 : prob(2) = 1/6.0 : prob(3) = 1/7.0 : prob(4) = 1/8.0
prob(5) = 1/9.0 : prob(6) = 1/10.0 : prob(7) = 1/11.0 : prob(8) = 1759/27720
for i = 1 to _elements
sum += prob(i)
next
if abs(sum-1) > samples then print "Probabilities don't sum to 1." : exit fn
for i = 1 to samples
cln r = (((double)arc4random()/0x100000000));
p = 0
for j = 1 to _elements
p += prob(j)
if (r < p) then cumulative(j) += 1 : exit for
next
next
print
printf @"Item Actual Theoretical"
printf @"---- ------ -----------"
for i = 1 to _elements
printf @"%-7s %10.6f %12.6f", item(i), cumulative(i)/samples, prob(i)
checksum += cumulative(i)/samples
next
printf @" -------- -----------"
printf @"%17.6f %12.6f", checksum, 1.000000
end fn
 
fn ProbabilisticChoice
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Item Actual Theoretical
---- ------ -----------
aleph 0.199966 0.200000
beth 0.166505 0.166667
gimel 0.142958 0.142857
daleth 0.124827 0.125000
he 0.111451 0.111111
waw 0.100095 0.100000
zayin 0.090985 0.090909
heth 0.063213 0.063456
-------- -----------
1.000000 1.000000
</pre>
 
Line 1,521 ⟶ 1,626:
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import System.Random (newStdGen, randomRs)
 
dataBinCounts :: [Float] -> [Float] -> [Int]
dataBinCounts thresholds range =
zipWith
let sampleSize = length range
(-)
xs = ((-) sampleSize . length . flip filter range . (<)) <$> thresholds
in zipWith (-) (xs ++<> [sampleSize]) (0 : xs)
(0 : xs)
where
sampleSize = length range
xs =
(-) sampleSize . length
. flip filter range
. (<)
<$> thresholds
 
--------------------------- TEST -------------------------
main :: IO ()
main = do
g <- newStdGen
let fractions = recip <$> [5 .. 11] :: [Float]
expected = fractions ++<> [1 - sum fractions]
actual =
((/ 1000000.0) . fromIntegral) <$>
<$> dataBinCounts
dataBinCounts (scanl1 (+) expected) (take 1000000 (randomRs (0, 1) g))
(scanl1 (+) expected)
piv n = take n . (++take repeat1000000 (randomRs (0, '1) 'g))
piv n x = take n (x <> repeat ' ');
 
putStrLn " expected actual"
mapM_ putStrLn $
zipWith3
( \l s c -> piv 7 l ++ piv 13 (show s) ++ piv 12 (show c))
piv 7 l
["aleph", "beth", "gimel", "daleth", "he", "waw", "zayin", "heth"]
<> (piv 13 (show s) <> piv 12 (show c))
)
[ "aleph",
"beth",
"gimel",
"daleth",
"he",
"waw",
"zayin",
"heth"
]
expected
actual</syntaxhighlight>
Line 2,026 ⟶ 2,152:
def rpad($len): tostring | ($len - length) as $l | .+ ("0" * $l)[:$l];
 
# Input: a string of digits with up to one embedded "."
# Output: the corresponding string representation with exactly $n decimal digits but aligned at the period
def align_decimal($n):
tostring
Line 2,033 ⟶ 2,159:
| if $ix then capture("(?<i>[0-9]*[.])(?<j>[0-9]{0," + ($n|tostring) + "})") as {$i, $j}
| $i + ($j|rpad($n))
else . + ".0" |+ align_decimal("0" * $n)
end ;
 
Line 2,132 ⟶ 2,258:
heth : 63455.9 63348 0.18
</pre>
 
 
=={{header|Julia}}==
Line 3,870 ⟶ 3,995:
heth 0.068800 0.063456 0.005344
</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "randdist" )
@( description, "Given a mapping between items and their required" )
@( description, "probability of occurrence, generate a million items" )
@( description, "randomly subject to the given probabilities and compare" )
@( description, "the target probability of occurrence versus the" )
@( description, "generated values." )
@( description, "" )
@( description, "The total of all the probabilities should equal one." )
@( description, "(Because floating point arithmetic is involved this is" )
@( description, "subject to rounding errors). Use the following mapping" )
@( description, "to test your programs: aleph 1/5.0, beth 1/6.0," )
@( description, "gimel 1/7.0, daleth 1/8.0, he 1/9.0, waw 1/10.0" )
@( description, "zayin 1/11.0, heth 1759/27720 adjusted so that" )
@( description, "probabilities add to 1" )
@( see_also, "http://rosettacode.org/wiki/Probabilistic_choice" )
@( author, "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure randdist is
trials : constant positive := 1_000_000;
type outcome is (aleph, beth, gimel, daleth, he, waw, zayin, heth);
pr : constant array(aleph..heth) of float :=
(1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1 );
samples : array(aleph..heth) of natural := (0, 0, 0, 0, 0, 0, 0, 0);
random_value : float;
begin
for try in 1..trials loop
random_value := numerics.random;
for i in arrays.first( pr )..arrays.last( pr ) loop
if random_value <= pr(i) then
samples(i) := samples(i) + 1;
exit;
else
random_value := @ - pr(i);
end if;
end loop;
end loop;
-- Show results
for i in arrays.first( pr )..arrays.last( pr ) loop
put( i ) @ ( " " ) @ ( float( samples( i ) ) / float( trials ) );
if i = heth then
put_line( " rest" );
else
put_line( pr(i) );
end if;
end loop;
end randdist;</syntaxhighlight>
{{out}}
<pre>
$ spar randdist
aleph 2.00260000000000E-01 2.00000000000000E-01
beth 1.66376000000000E-01 1.66666666666667E-01
gimel 1.42698000000000E-01 1.42857142857143E-01
daleth 1.25408000000000E-01 1.25000000000000E-01
he 1.11311000000000E-01 1.11111111111111E-01
waw 9.97980000000000E-02 1.00000000000000E-01
zayin 9.09570000000000E-02 9.09090909090909E-02
heth 6.31920000000000E-02 rest</pre>
 
=={{header|Stata}}==
Line 4,018 ⟶ 4,207:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var letters = ["aleph", "beth", "gimel", "daleth", "he", "waw", "zayin", "heth"]
1,983

edits