Created
November 28, 2016 07:02
-
-
Save akastrin/48fcbd5b97185dcce3db1a8c5b4c7129 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Input 1: a vector of n probabilities each 0 < p < 1 such that sum of all p = k | |
# Input 2: k - integer | |
# Output: all possible products of Pi where k of Pi are selected as such, the other n-k as (1-Pi) | |
# Run as: awk -f sampling.R | |
BEGIN { | |
n = 5; | |
k = 2; | |
p[1] = 0.15; | |
p[2] = 0.22; | |
p[3] = 0.36; | |
p[4] = 0.56; | |
p[5] = 0.71; | |
p_prod(1, k, 1, ""); | |
} | |
function p_prod(start, kk, curr_prod, path) { | |
if (start > n) { | |
if (kk <= 0) { | |
printf "%10.8f; %s\n\r", curr_prod, path; | |
} | |
} | |
else { | |
if (kk > 0) { | |
cc = curr_prod * (p[start]+0); | |
pp = path " (+" start ", " p[start] ", " curr_prod ")"; | |
p_prod(start+1, kk-1, cc, pp); | |
} | |
cc = curr_prod * (1 - (p[start]+0)); | |
pp = path " (-" start ", " (1 - p[start]) ", " curr_prod ")"; | |
p_prod(start+1, kk, cc, pp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment