veg / hyphy

HyPhy: Hypothesis testing using Phylogenies
http://www.hyphy.org
Other
209 stars 69 forks source link

Sampling a value from a customized distribution over a discrete set of values in HBL #565

Closed halabikeren closed 7 years ago

halabikeren commented 7 years ago

Hi,

I have a vector of values, V, and a vector of probabilities for each value, P, accordingly.

I would like to sample a single value from V according to the probabilities in P.

Can I do that in HBL?

Thanks! Keren

stevenweaver commented 7 years ago

Dear Keren,

Are these values and probabilities associated with a respective site or branch? There are a number of ways to achieve this.

Personally, I am a proponent of expressiveness, so I would opt for using an associative array constructed like the following.

test = {
"branch_name" : { "length" : 0.015, "pval": 0.04 }
};

fprintf(stdout, (test["branch_name"])["length"]);

You could also simply use a for loop and use the indices, but that is less expressive.

Best, Steven

halabikeren commented 7 years ago

Thanks!

These values are associated with characters (for example, amino acids).

So, I have a vector of amino acids, and a vector with probabilities to choose each amino acid.

I would like to sample one assignment of amino acid to a node, for example, according to the probabilities of the amino acids.

I could store the data in an associative array, rather than two vectors, but I'm not sure how to conduct the sampling itself.

Do you happen to know how I could do that? maybe I could define a discrete distribution over the amino acids using HBL's Category object and sample from it somehow?

Thanks again!

spond commented 7 years ago

Dear @halabikeren,

You can sample from a multinomial distribution (which is what you have here) like so:


sample_from = {
{1,0.5} // value, weight
{2,0.3}
{3,0.15}
{4,0.05}
};

sample = Random(sample_from, {"PDF":"Multinomial","ARG0":1});
// ARG0 is the number of samples

fprintf (stdout, sample, "\n");

/* resulting in something like, meaning that "1" got sampled

{
{1, 1}
{2, 0}
{3, 0}
{4, 0}
}

*/

Best, Sergei

halabikeren commented 7 years ago

Perfect, thank you!