ISO help in probability



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
---
.



Relevant Pages

  • Re: ISO help in probability
    ... >probability by multiplying a random value with the associated weight, ... compute the cumulative sum ... Tweak the algorithm above as needed depending on the required ...
    (comp.lang.tcl)
  • Re: Looking for an Introduction to Statistics
    ... Statistics from the viewpoint of Set Theory? ... uses sets, except through probability. ... (a weight need not be an integer) ... discrete or limits of discrete, ...
    (sci.math)
  • Re: Context modelling
    ... context modelling in statistical analysis? ... I wrote a technical report describing PAQ6. ... where w is the mixing weight, x is the input probability from a model, ... p is the output probability, and y is the actual bit, and a is ...
    (comp.compression)
  • i have problem with probability of undetected error equation, help me please..
    ... on probability of undetected error equation, there was A (the weight ... distribution function of the CRC code depending of the generator).. ...
    (sci.electronics.basics)
  • i have problem with "probability of undetected error" equation
    ... on probability of undetected error equation, there was A (the weight ... distribution function of the CRC code depending of the generator).. ...
    (sci.math)