Paste: stratified sampling

Author: erg
Mode: factor
Date: Sat, 17 Nov 2012 19:10:40
Plain Text |
ERROR: probability-sum-not-one seq ;

: check-probabilities ( seq -- seq )
    dup sum 1.0 .00000000001 ~ [ probability-sum-not-one ] unless ;

MACRO: probabilities-quot ( seq -- quot )
    check-probabilities [ >float ] map cum-sum
    '[
        _ random-unit '[ _ > ] find drop
    ] ;

: stratified-sample ( stratified-sequences probability-sequence -- elt )
    probabilities-quot swap nth random ; inline

New Annotation

Summary:
Author:
Mode:
Body: