Python: I have a list of probabilities, and I want to use them to generate mutations in a protein sequence
1
0
Entering edit mode
5.2 years 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?

python PDB dssp • 1.3k views
ADD COMMENT
1
Entering edit mode
5.2 years ago
Asaf 10k

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.

ADD COMMENT

Login before adding your answer.

Traffic: 2415 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6