Generate n random values from a specific mean and range values [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to generate 100 random values from a specific mean and specific range values.
Do you know a function that do that in R or Python ?
Thanks a lot.

that R solution could meet your expectations
first (if needed) use install.packages("truncnorm") to install package
then
library(truncnorm)
rtruncnorm(n=100, a=0, b= 10, mean=5, sd=5)

This depends on what distribution you want your random values to come from. Another option would be a uniform distribution. In R you could do
n <- 100
mu <- 10
myrange <- 10
runif(n, mu - myrange/2, mu + myrange/2)
to get 100 draws from a uniform around the mean 10.

Related

Finding all possible combinations of sum product to reach a given target [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
There has been a solution to find the possible combination of numbers to reach a given target number. However, I have a different situation below, where a,b, and c are product types and I like to find the combination of sum products of a,b and c to reach the target total.
a = 50sqft
b = 70sqft
c = 100sqft
Total = 5000sqft
I like to find all possible combinations of numbers (integer solution) of a,b,c to get to 5000, and how can I create a python function for that?
Results :
(100a,0b,0c)=5000
(23a,5b,8c)=5000
...
...
Thanks in advance!!
I got a solution :
a=50
b=70
c=100
for i in range(101): # This si 101 here to give 100a=5000
for j in range(100):
for k in range(100):
if i*a + j*b + k*c == 5000:
print('({}a,{}b,{}c)=5000'.format(i,j,k))

More Pythonic way to loop through a range [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
A friend asked me to make her coding more "pythonic" but I'm pretty new at it myself. This is what I came up with, and I'm a little concerned that it won't hit all of the numbers (6, 7, 8, 9, 10 and 11). I also KNOW that there is a better way, but I just don't know what it is. Can you help?
prob = 0
for r in range(6,11):
prob += binom.pmf(k=r, n=11, p=0.2)
print(‘The probability is {}’,format(prob))
I think that your code is perfectly pythonic, for what that's worth
You could make it a little more compact and more performant with a list comprehension, the sum() function, and an f-string. Also, I took into account that you wanted 11 inclusive, so your range should be from 6 to 12.
probs = [binom.pmf(k=r, n=11, p=0.2) for r in range(6, 12)]
print(f'The probability is {sum(probs)}')
Use list comprenhension:
def binom(n,r,p):
# dummy function
return n+r+p
prob = sum([binom(r,12, 0.2) for r in range(6,11)])
print(f"The probability is {prob}")
Edit:
To make 11 inclusive, increase your range to 12
Edit 2:
As suggested by OneCricketeer, if only need to compute the sum, you can remove the square brackets, like so:
prob = sum(binom(r,12, 0.2) for r in range(6,11))

Generate list of numbers from a list with probability weights [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How would one go about generating a list of random numbers from a list of 35 numbers with given probabilities:
Probability of number in range (1,5) - p1 = 0.5
Probability of number in range (6,15) - p2 = 0.25
Probability of number in range (16,35) - p3 = 0.25
I tried using numpy.random.choice() but I have no idea how to jam in possible lists of numbers.
If you want to select uniformly from each group:
p=np.array([0.5/5]*5+[0.25/30]*30)
np.random.choice(np.arange(1,36),p=p/p.sum())
UPDATE:
and if you would like to select a list of random numbers (you can also set with or without replacement flag):
np.random.choice(np.arange(1,36), size=N, replace=True, p=p/p.sum())
You could accomplish this by selecting the appropriate range with the specified probabilities, then use the chosen range as an argument to random.choice:
range_set = [range(1,6), range(6, 16), range(16, 36)]
random.choice(*random.choices(range_set, [0.5, 0.25, 0.25]))
This assumes that you want a uniform choice within a given range.

Can we generate random integers basing on Mean and SD in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have already generated data using np.random.normal but I am getting float values. I need to get Integers instead.
Could anyone help me out?
In order to get integers instead of float, you can try using int() to cast the value you are getting. Assuming you want a normal distribution of a thousand samples between -100 and 100, with a mean of 0 and standard deviation of 1, you'd try this:
y = [int(x * 100) for x in np.random.normal(0, 1, 100)]
Here variable y holds the normal integer distribution you want.

How do I calculate Pr(model|data) in Bayesian inference with extremely small numbers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm doing Bayesian inference (manually, using a grid search) in Python. I want to calculate the probability of each model given the data. The problem is I can only calculate the 'evidence' in log, otherwise its 0.
So, even though its between 0-1, I can't get the results for:
Pr(data|model1) / (Pr(data|model1) + Pr(data|model2))
Since each term is 0 in its non-log form.
Any ideas?
Thanks
Let logpr1 and logpr2 be log(data|model1) and log(data|model2), respectively, and suppose
In [57]: logpr1 = -802
In [58]: logpr2 = -800
If you try to express those as probabilities (not logarithms of probabilities), you get 0:
In [59]: np.exp(logpr2)
Out[59]: 0.0
Now you want to compute
log(Pr(data|model1) / (Pr(data|model1) + Pr(data|model2))),
which you can also write as
log(Pr(data|model1)) - log(Pr(data|model1) + Pr(data|model2)).
For the last term, you can use the function numpy.logaddexp (which is the essential tip in this answer; see also scipy.misc.logsumexp). So your calculation is:
In [60]: logp = logpr1 - np.logaddexp(logpr1, logpr2)
In [61]: logp
Out[61]: -2.1269280110429918
In this case, that number is not very small. In fact, you can express it as a plain probability:
In [62]: np.exp(logp)
Out[62]: 0.11920292202211526

Categories