Biostar Beta. Not for public use.
Python: I have a list of probabilities, and I want to use them to generate mutations in a protein sequence
Entering edit mode
17 months ago

I have a list of probabilities that I have generated. Here is a short snippet of what this variable looks like

[0.62, 0.20. 0.0, 0.69, 0.24, 0.50, ...]

These are probabilities that an amino acid substitution will happen in a protein sequence. What I want to do is to take this list of probabilities and generate a list of either True or False values based on the probability. I have tried to do this using the numpy.random.choice module in python

numpy.random.choice(a, size=None, replace=True, p=None)

However, when I enter my value for p (the probability distribution) as the variable probabilities which is my list of probabilities, the interpreter gives the error that p does not sum to 1. I know that p does not sum to 1, so I don't think this is the right python module to use for my purposes. Is there a different way to simply go through a list of probabilities and use those probabilities to either generate True or False?

Entering edit mode
17 months ago
Asaf 5.6k

As simple as:

import random
a = [0.62, 0.20. 0.0, 0.69, 0.24, 0.50]
b = [random.random()<x for x in a]

You sample a number in the range [0,1) and ask if the number is smaller than the value, for each value in a.


Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.3.1