-1

I have integers in the range of 1..10. I want to choose one based on a distribution of x^2.

What I want is something like:

def random_from_exp(min, max, exponent): ... 

I'm not familiar with maths or stats and I don't know how to interpret previous answers to this question (lambdas, inversions and whatnot) in the context of my problem above. Please provide a simple explanation with an example.

5
  • "I want to choose one based on a distribution of x^2." I don't quite understand. Do you want to really generate data on the basis of the distribution of x^2? This isn't commonly referred to as the Exponential Distribution. Commented Mar 18, 2014 at 22:14
  • Perhaps this isn't clear. What I want is a weighted choice of xs based on x^2. So 5 has a weighting of 25; 10 has a weighting of 100. Would the solution be to generate those weightings in a random weighted choice algorithm? Commented Mar 18, 2014 at 22:18
  • 1
    What you are looking for is precisely this: stackoverflow.com/questions/11373192/… Commented Mar 18, 2014 at 22:26
  • You might also find this useful: stackoverflow.com/questions/14556451/… Instead of squaring a random number, it uses the random number as the power. Commented Mar 18, 2014 at 22:33
  • @cyrus: Do you want to generate random integers, or floating point values? Commented Mar 18, 2014 at 22:37

2 Answers 2

1

If I'm right, you want to simply generate a number that is on the exponential function (x^2) and is between 0 to 10?

That's pretty simple. Get a random number from 0 to the square root of 10 and square it.

import random import math print random.uniform(0, math.sqrt(10))**2 

Or in your case :

# I have no idea what the exp means. I'm assuming it means exponent. def random_from_exp(min, max, exp): return random.uniform(min, math.sqrt(max))**exp 

This will print (min <= RESULT <= max) ^ exp

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! The distribution of values is actually flipped though (so '10' is selected the least). Subtracting that value from the max seems to work though.
@cyrus The distribution of x^2 when x is uniformly distributed is actually 1/2 1/sqrt(x). Maybe 1 - x^2 works for you anyway. The link given by Nitish above tells how to get exactly the right distribution, if that matters.
The point that I wanted to show here is that if you are able to generate the range of values that you require, then you can feed it to a function. In the case that you want to generate a set of values that have a normal distribution, I believe scikit will surely have a method to do that.
This should help you out : stackoverflow.com/questions/14266717/…
0
# https://miptstats.github.io/courses/python/07_random.html import numpy as np from numpy import random import matplotlib.pyplot as plt scale= 4 size= 1_000_000 x= np.random.exponential(scale, size) plt.hist(x, bins=50, density=True) plt.show() 

a random number on an exponential distribution

do you really mean several same-parametrized distributions? I suppose, from that x_distr (with params given above) you can make sample from size repeats in such a way:

from scipy.stats import rv_discrete random_state = 1234 Iterations= 2000 # ? - probably, just size of newly generated sample! https://mipt-stats.gitlab.io/courses/python/07_scipy_stats.html ##x = np.random.randn(100_000) unique, counts = np.unique(x, return_counts=True) print(unique, counts) # create object (use rv_continuous for continuous data) r = rv_discrete(name='some_distribution', values= (unique, counts/counts.sum()) ) #, size= Iterations) # make sample sample = r.rvs(size= Iterations, random_state= 123456) plt.hist(sample, bins= 20, density= True) plt.show() 

P.S. or single exp distr can be taken also from scipy.stats:

from scipy.stats import expon lamb = 1 # The middle 80% of probability mass is located betweenthe 0.1 and 0.9 quantiles rv = expon(scale = 1 / lamb) q1 = rv.ppf(0.1) q2 = rv.ppf(0.9) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.