Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -12,11 +12,8 @@ void main() {
|
|||
|
||||
double[pr.length] counts = 0.0;
|
||||
auto rnd = Xorshift(unpredictableSeed);
|
||||
foreach (immutable _; 0 .. nTrials) {
|
||||
immutable rnd01 = rnd.front / double(rnd.max);
|
||||
rnd.popFront;
|
||||
counts[cumulatives[].countUntil!(c => c >= rnd01)]++;
|
||||
}
|
||||
foreach (immutable _; 0 .. nTrials)
|
||||
counts[cumulatives[].countUntil!(c => c >= rnd.uniform01)]++;
|
||||
|
||||
writeln("Item Target prob Attained prob");
|
||||
foreach (name, p, co; zip(items, pr, counts[]))
|
||||
|
|
|
|||
31
Task/Probabilistic-choice/Erlang/probabilistic-choice.erl
Normal file
31
Task/Probabilistic-choice/Erlang/probabilistic-choice.erl
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
-module(probabilistic_choice).
|
||||
|
||||
-export([test/0]).
|
||||
|
||||
-define(TRIES, 1000000).
|
||||
|
||||
test() ->
|
||||
Probs =
|
||||
[{aleph,1/5},
|
||||
{beth,1/6},
|
||||
{gimel,1/7},
|
||||
{daleth,1/8},
|
||||
{he,1/9},
|
||||
{waw,1/10},
|
||||
{zayin,1/11},
|
||||
{heth,1759/27720}],
|
||||
random:seed(now()),
|
||||
Trials =
|
||||
[get_choice(Probs,random:uniform()) || _ <- lists:seq(1,?TRIES)],
|
||||
[{Glyph,Expected,(length([Glyph || Glyph_ <- Trials, Glyph_ == Glyph])/?TRIES)}
|
||||
|| {Glyph,Expected} <- Probs].
|
||||
|
||||
get_choice([{Glyph,_}],_) ->
|
||||
Glyph;
|
||||
get_choice([{Glyph,Prob}|T],Ran) ->
|
||||
case (Ran < Prob) of
|
||||
true ->
|
||||
Glyph;
|
||||
false ->
|
||||
get_choice(T,Ran - Prob)
|
||||
end.
|
||||
11
Task/Probabilistic-choice/MATLAB/probabilistic-choice-1.m
Normal file
11
Task/Probabilistic-choice/MATLAB/probabilistic-choice-1.m
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function probChoice
|
||||
choices = {'aleph' 'beth' 'gimel' 'daleth' 'he' 'waw' 'zayin' 'heth'};
|
||||
w = [1/5 1/6 1/7 1/8 1/9 1/10 1/11 1759/27720];
|
||||
R = randsample(length(w), 1e6, true, w);
|
||||
T = tabulate(R);
|
||||
fprintf('Value\tCount\tPercent\tGoal\n')
|
||||
for k = 1:size(T, 1)
|
||||
fprintf('%6s\t%.f\t%.2f%%\t%.2f%%\n', ...
|
||||
choices{k}, T(k, 2), T(k, 3), 100*w(k))
|
||||
end
|
||||
end
|
||||
17
Task/Probabilistic-choice/MATLAB/probabilistic-choice-2.m
Normal file
17
Task/Probabilistic-choice/MATLAB/probabilistic-choice-2.m
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
function probChoice
|
||||
choices = {'aleph' 'beth' 'gimel' 'daleth' 'he' 'waw' 'zayin' 'heth'};
|
||||
w = [1/5 1/6 1/7 1/8 1/9 1/10 1/11 1759/27720];
|
||||
nSamp = 1e6;
|
||||
nChoice = length(w);
|
||||
R = rand(nSamp, 1);
|
||||
wCS = cumsum(w);
|
||||
results = zeros(1, nChoice);
|
||||
fprintf('Value\tCount\tPercent\tGoal\n')
|
||||
for k = 1:nChoice
|
||||
choiceKIdxs = R < wCS(k);
|
||||
R(choiceKIdxs) = k;
|
||||
results(k) = sum(choiceKIdxs);
|
||||
fprintf('%6s\t%.f\t%.2f%%\t%.2f%%\n', ...
|
||||
choices{k}, results(k), 100*results(k)/nSamp, 100*w(k))
|
||||
end
|
||||
end
|
||||
|
|
@ -1,19 +1,21 @@
|
|||
constant TRIALS = 1e4;
|
||||
|
||||
my %ps = <aleph beth gimel daleth he waw zayin> Z=> 1 «/« (5 .. 11);
|
||||
%ps<heth> = 1 - [+] values %ps;
|
||||
my @event = <aleph beth gimel daleth he waw zayin heth>;
|
||||
|
||||
my %results;
|
||||
my @P = 1 «/« (5 .. 11), 1759/27720;
|
||||
my @cP = [\+] @P;
|
||||
|
||||
my @results;
|
||||
for ^TRIALS {
|
||||
%results{.key}++ given
|
||||
first { .value > state $ = rand },
|
||||
state % = %ps.keys Z=> [\+] %ps.values;
|
||||
@results[
|
||||
first { @cP[$_] > state $ = rand }, ^@P;
|
||||
]++;
|
||||
}
|
||||
|
||||
say 'Event Occurred Expected Difference';
|
||||
for sort *.value, %results {
|
||||
my ($occurred, $expected) = .value/TRIALS, %ps{.key};
|
||||
printf "%-6s %f %f %f\n",
|
||||
.key, $occurred, $expected,
|
||||
say 'Event Occurred Expected Difference';
|
||||
for ^@results {
|
||||
my ($occurred, $expected) = @results[$_], @P[$_]*TRIALS;
|
||||
printf "%-9s%8.0f%9.1f%12.1f\n",
|
||||
@event[$_], $occurred, $expected,
|
||||
abs( $occurred - $expected );
|
||||
}
|
||||
|
|
|
|||
64
Task/Probabilistic-choice/Scala/probabilistic-choice.scala
Normal file
64
Task/Probabilistic-choice/Scala/probabilistic-choice.scala
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
object ProbabilisticChoice extends App {
|
||||
import scala.collection.mutable.LinkedHashMap
|
||||
|
||||
def weightedProb[A](prob: LinkedHashMap[A,Double]): A = {
|
||||
require(prob.forall{case (_, p) => p > 0 && p < 1})
|
||||
assume(prob.values.sum == 1)
|
||||
def weighted(todo: Iterator[(A,Double)], rand: Double, accum: Double = 0): A = todo.next match {
|
||||
case (s, i) if rand < (accum + i) => s
|
||||
case (_, i) => weighted(todo, rand, accum + i)
|
||||
}
|
||||
weighted(prob.toIterator, scala.util.Random.nextDouble)
|
||||
}
|
||||
|
||||
def weightedFreq[A](freq: LinkedHashMap[A,Int]): A = {
|
||||
require(freq.forall{case (_, f) => f >= 0})
|
||||
require(freq.values.sum > 0)
|
||||
def weighted(todo: Iterator[(A,Int)], rand: Int, accum: Int = 0): A = todo.next match {
|
||||
case (s, i) if rand < (accum + i) => s
|
||||
case (_, i) => weighted(todo, rand, accum + i)
|
||||
}
|
||||
weighted(freq.toIterator, scala.util.Random.nextInt(freq.values.sum))
|
||||
}
|
||||
|
||||
// Tests:
|
||||
|
||||
val probabilities = LinkedHashMap(
|
||||
'aleph -> 1.0/5,
|
||||
'beth -> 1.0/6,
|
||||
'gimel -> 1.0/7,
|
||||
'daleth -> 1.0/8,
|
||||
'he -> 1.0/9,
|
||||
'waw -> 1.0/10,
|
||||
'zayin -> 1.0/11,
|
||||
'heth -> 1759.0/27720
|
||||
)
|
||||
|
||||
val frequencies = LinkedHashMap(
|
||||
'aleph -> 200,
|
||||
'beth -> 167,
|
||||
'gimel -> 143,
|
||||
'daleth -> 125,
|
||||
'he -> 111,
|
||||
'waw -> 100,
|
||||
'zayin -> 91,
|
||||
'heth -> 63
|
||||
)
|
||||
|
||||
def check[A](original: LinkedHashMap[A,Double], results: Seq[A]) {
|
||||
val freq = results.groupBy(x => x).mapValues(_.size.toDouble/results.size)
|
||||
original.foreach{case (k, v) =>
|
||||
val a = v/original.values.sum
|
||||
val b = freq(k)
|
||||
val c = if (Math.abs(a - b) < 0.001) "ok" else "**"
|
||||
println(f"$k%10s $a%.4f $b%.4f $c")
|
||||
}
|
||||
println(" "*10 + f" ${1}%.4f ${freq.values.sum}%.4f")
|
||||
}
|
||||
|
||||
println("Checking weighted probabilities:")
|
||||
check(probabilities, for (i <- 1 to 1000000) yield weightedProb(probabilities))
|
||||
println
|
||||
println("Checking weighted frequencies:")
|
||||
check(frequencies.map{case (a, b) => a -> b.toDouble}, for (i <- 1 to 1000000) yield weightedFreq(frequencies))
|
||||
}
|
||||
45
Task/Probabilistic-choice/Scheme/probabilistic-choice.ss
Normal file
45
Task/Probabilistic-choice/Scheme/probabilistic-choice.ss
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
(use-modules (ice-9 format))
|
||||
|
||||
(define (random-choice probs)
|
||||
(define choice (random 1.0))
|
||||
(define (helper val prob-lis)
|
||||
(let ((nval (- val (cadar prob-lis))))
|
||||
(if
|
||||
(< nval 0)
|
||||
(caar prob-lis)
|
||||
(helper nval (cdr prob-lis)))))
|
||||
(helper choice probs))
|
||||
|
||||
(define (add-result result delta table)
|
||||
(cond
|
||||
((null? table) (list (list result delta)))
|
||||
((eq? (caar table) result)
|
||||
(cons (list result (+ (cadar table) delta)) (cdr table)))
|
||||
(#t (cons (car table) (add-result result delta (cdr table))))))
|
||||
|
||||
(define (choices trials probs)
|
||||
(define (helper trial-num freq-table)
|
||||
(if
|
||||
(= trial-num trials)
|
||||
freq-table
|
||||
(helper
|
||||
(+ trial-num 1)
|
||||
(add-result (random-choice probs) (/ 1 trials) freq-table))))
|
||||
(helper 0 '()))
|
||||
|
||||
(define (format-results probs results)
|
||||
(for-each
|
||||
(lambda (x)
|
||||
(format
|
||||
#t
|
||||
"~10a~10,5f~10,5f~%"
|
||||
(car x)
|
||||
(cadr x)
|
||||
(cadr (assoc (car x) results))))
|
||||
probs))
|
||||
|
||||
(define probs
|
||||
'((aleph 1/5) (beth 1/6) (gimel 1/7) (daleth 1/8)
|
||||
(he 1/9) (waw 1/10) (zayin 1/11) (heth 1759/27720)))
|
||||
|
||||
(format-results probs (choices 1000000 probs))
|
||||
Loading…
Add table
Add a link
Reference in a new issue