Re: ISO help in probability
- From: Michael Schlenker <schlenk@xxxxxxxxxxxxxxxx>
- Date: Fri, 09 Dec 2005 13:50:22 +0100
Helmut Giese wrote:
> Hello out there,
> I have a set of variables each of which has a certain weight or
> probability assigned. Say
> a -> 50
> b -> 30
> c -> 10
> d -> 10
>
> I want an algorithm which (in the above example) selects 'a' in every
> other run and 'c' in 10% of the runs (approximately of course).
>
> My attempt below fails miserably. Using the weights above, 'a' scores
> way too high, mostly at the expense of 'c' and 'd', which are far away
> from their expected 10% shares.
> Evidently my logic is flawed: Foreach variable I get the current
> probability by multiplying a random value with the associated weight,
> and then select the one with the highest result.
>
> Any advice on the 101 of probability applicable here will be greatly
> appreciated.
> Best regards
> Helmut Giese
Basically it works like this:
You divide your probability space [1-100] (if you use percentage) into
intervals based on the weights you have. So for your example you map the
letters to this intervals:
a -> [1 50]
b -> [51 80]
c -> [81 90]
d -> [91 100]
Now you generate a random number between 1 and 100 and lookup in which
interval it is, thats the winner.
If you only allow integer weights, you can get lazy like this:
proc create_wtable {var weights} {
upvar 1 $var table
set table [list {}]
foreach {key weight} $weights {
for {set i 0} {$i < $weight} {incr i} {
lappend table $key
}
}
# return the value for probability 1.0
return [expr {[llength $table]-1}]
}
proc pick_random {var max} {
upvar 1 $var table
lindex $table [math::random 1 $max]
}
# test
set max [create_wtable wtab {a 50 b 30 c 10 d 10} ]
puts [pick_random wtab $max]
Michael
.
- References:
- ISO help in probability
- From: Helmut Giese
- ISO help in probability
- Prev by Date: Re: problem with status bar
- Next by Date: status bar with two labels
- Previous by thread: ISO help in probability
- Next by thread: Re: ISO help in probability
- Index(es):
Relevant Pages
|