Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
47
Task/Probabilistic-choice/Nimrod/probabilistic-choice.nimrod
Normal file
47
Task/Probabilistic-choice/Nimrod/probabilistic-choice.nimrod
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import tables, math, strutils, times
|
||||
|
||||
const
|
||||
num_trials = 1000000
|
||||
precsn = 6
|
||||
|
||||
var start = cpuTime()
|
||||
|
||||
var probs = initTable[string,float](16)
|
||||
probs.add("aleph", 1/5.0)
|
||||
probs.add("beth", 1/6.0)
|
||||
probs.add("gimel", 1/7.0)
|
||||
probs.add("daleth", 1/8.0)
|
||||
probs.add("he", 1/9.0)
|
||||
probs.add("waw", 1/10.0)
|
||||
probs.add("zayin", 1/11.0)
|
||||
probs.add("heth", 1759/27720)
|
||||
|
||||
var samples = initTable[string,int](16)
|
||||
for i, j in pairs(probs):
|
||||
samples.add(i,0)
|
||||
|
||||
randomize()
|
||||
for i in 1 .. num_trials:
|
||||
var z = random(1.0)
|
||||
|
||||
for j,k in pairs(probs):
|
||||
if z < probs[j]:
|
||||
samples[j] = samples[j] + 1
|
||||
break
|
||||
else:
|
||||
z = z - probs[j]
|
||||
|
||||
var s1, s2: float
|
||||
|
||||
echo("Item ","\t","Target ","\t","Results ","\t","Difference")
|
||||
echo("==== ","\t","====== ","\t","======= ","\t","==========")
|
||||
for i, j in pairs(probs):
|
||||
s1 += samples[i]/num_trials*100.0
|
||||
s2 += probs[i]*100.0
|
||||
echo( i,
|
||||
"\t", formatFloat(probs[i],ffDecimal,precsn),
|
||||
"\t", formatFloat(samples[i]/num_trials,ffDecimal,precsn),
|
||||
"\t", formatFloat(100.0*(1.0-(samples[i]/num_trials)/probs[i]),ffDecimal,precsn),"%")
|
||||
echo("======","\t","======= ","\t","======== ")
|
||||
echo("Total:","\t",formatFloat(s2,ffDecimal,2)," \t",formatFloat(s1,ffDecimal,2))
|
||||
echo("\n",formatFloat(cpuTime()-start,ffDecimal,2)," secs")
|
||||
|
|
@ -1,27 +1,19 @@
|
|||
constant TRIALS = 1e4;
|
||||
|
||||
sub prob_choice_picker (%options is copy) {
|
||||
my $n = 0;
|
||||
$_ = ($n += $_) for values %options;
|
||||
return sub {
|
||||
my $r = rand;
|
||||
(first { $r < .value }, %options).key;
|
||||
};
|
||||
}
|
||||
|
||||
my %ps = (
|
||||
(map {$^w => 1/$^n}, (5 .. 11 Z <aleph beth gimel daleth he waw zayin>)),
|
||||
heth => 0
|
||||
);
|
||||
my %ps = <aleph beth gimel daleth he waw zayin> Z=> 1 «/« (5 .. 11);
|
||||
%ps<heth> = 1 - [+] values %ps;
|
||||
|
||||
&picker = prob_choice_picker %ps;
|
||||
my %results;
|
||||
++%results{picker} for ^TRIALS;
|
||||
for ^TRIALS {
|
||||
%results{.key}++ given
|
||||
first { .value > state $ = rand },
|
||||
state % = %ps.keys Z=> [\+] %ps.values;
|
||||
}
|
||||
|
||||
say 'Event Occurred Expected Difference';
|
||||
for sort { .value }, %results {
|
||||
for sort *.value, %results {
|
||||
my ($occurred, $expected) = .value/TRIALS, %ps{.key};
|
||||
printf "%-6s %f %f %f\n",
|
||||
.key, .value/TRIALS, %ps{.key},
|
||||
abs( .value/TRIALS - %ps{.key} );
|
||||
.key, $occurred, $expected,
|
||||
abs( $occurred - $expected );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
ordered_keys = ["aleph", "beth", "gimel", "daleth", "he", "waw", "zayin", "heth"]
|
||||
probabilities = {
|
||||
"aleph" => 1/5.0,
|
||||
"beth" => 1/6.0,
|
||||
|
|
@ -8,10 +7,11 @@ probabilities = {
|
|||
"waw" => 1/10.0,
|
||||
"zayin" => 1/11.0,
|
||||
}
|
||||
probabilities["heth"] = probabilities.each_value.inject(1) {|heth, value| heth -= value}
|
||||
probabilities["heth"] = 1.0 - probabilities.each_value.inject(:+)
|
||||
ordered_keys = probabilities.keys
|
||||
|
||||
sums = {}
|
||||
ordered_keys.each.inject(0) do |sum, key|
|
||||
sum, sums = 0.0, {}
|
||||
ordered_keys.each do |key|
|
||||
sum += probabilities[key]
|
||||
sums[key] = sum
|
||||
end
|
||||
|
|
@ -29,7 +29,9 @@ samples.times do
|
|||
end
|
||||
end
|
||||
|
||||
printf "%-6s %-19s %s\n", "key", "expected", "actual"
|
||||
puts "key expected actual diff"
|
||||
for k in ordered_keys
|
||||
printf "%-6s %.17f %.6f\n", k, probabilities[k], Float(actual[k])/samples
|
||||
act = Float(actual[k]) / samples
|
||||
val = probabilities[k]
|
||||
printf "%-8s%.8f %.8f %6.3f %%\n", k, val, act, 100*(act-val)/val
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue