ISO help in probability
- From: hgiese@xxxxxxxxxxxxx (Helmut Giese)
- Date: Fri, 09 Dec 2005 11:39:57 GMT
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
---
#
# $lst is a list of 'name / weight' pairs, like <a 20 b 10 c 5>
#
proc selectProb {lst} {
# turn list into array
array set weight $lst
# get the sum of the weights
set sum 0
foreach n [array names weight] {
incr sum $weight($n)
}
# make a pair 'weighted random value / name' for each
foreach n [array names weight] {
set prob($n) [list [expr {(rand() * $weight($n)) / $sum}] $n]
}
# sort by probability
foreach n [array names prob] {
lappend pLst $prob($n)
}
set pLst [lsort -decreasing $pLst]
# but return the names
foreach p $pLst {
lappend res [lindex $p 1]
}
return $res
}
# test
foreach n {a b c d} {
set cnt($n) 0
}
for {set i 0} {$i < 100} {incr i} {
set res [selectProb {a 50 b 30 c 10 d 10}]
# count the "winner"
incr cnt([lindex $res 0])
}
parray cnt
---
.
- Follow-Ups:
- Re: ISO help in probability
- From: Helmut Giese
- Re: ISO help in probability
- From: Helmut Giese
- Re: ISO help in probability
- From: Joe English
- Re: ISO help in probability
- From: Uwe Klein
- Re: ISO help in probability
- From: Michael Schlenker
- Re: ISO help in probability
- Prev by Date: Re: Unix "find" vs. ::fileutil::find
- Next by Date: Re: problem with status bar
- Previous by thread: How to query remote cups system for printer list?
- Next by thread: Re: ISO help in probability
- Index(es):
Relevant Pages
|