3

I am using scipy and want to create an an array of legnth n with a particular average.

Suppose I want an random arrays of length 3 with an average of 2.5 hence the possible options could be: [1.5, 3.5, 2.5] [.25, 7.2, .05] and so on and so forth...

I need to create many such arrays with varying lengths and different averages(specified) for each, so a generalized solution will be welcome.

3
  • 1
    Does the average have to be exact, or can you just re-centre your random numbers around your desired average, i.e. if the long term average of your standard random float [0,1) is 0.5, then you can shift the centre to 2.5 by adding 2... Commented Mar 19, 2013 at 4:51
  • Are the numbers in list restricted to be non-negative? If there is no restriction, I think you can just fill in the list all with random number except the last one, then calculate the last number based on other numbers and the average value. Commented Mar 19, 2013 at 5:00
  • Yes the numbers need to be non-negative. Commented Mar 19, 2013 at 5:03

3 Answers 3

10

Just generate numbers over the range you want (0...10 in this case)

>>> import random
>>> nums = [10*random.random() for x in range(5)]

Work out the average

>>> sum(nums)/len(nums)
4.2315222659844824

Shift the average to where you want it

>>> nums = [x - 4.2315222659844824 + 2.5 for x in nums]
>>> nums
[-0.628013346633133, 4.628537956666447, -1.7219257458163257, 7.617565127420011, 2.6038360083629986]
>>> sum(nums)/len(nums)
2.4999999999999996

You can use whichever distribution/range you like. By shifting the average this way it will always get you an average of 2.5 (or very close to it)

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

Comments

2

You haven't specified what distribution you want.

It's also not clear whether you want the average of the actual array to be 2.5, or the amortized average over all arrays to be 2.5.

The simplest solution—three random numbers in an even distribution from 0 to 2*avg—is this:

return 2*avg * np.random.rand(3)

If you want to guarantee that the average of the array is 2.5, that's a pretty simple constraint, but there are many different ways you could satisfy it, and you need to describe which way you want. For example:

n0 = random.random() * 2*avg
n1 = random.random() * (2*avg - n0)
n2 = random.random() * (2*avg - n0 - n1)
return np.array((n0, n1, n2))

Comments

-2

I found a solution to the problem.

numpy.random.triangular(left, mode, right, size=None)

Visit: http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.triangular.html#numpy.random.triangular

However, the minor problem is that it forces a triangular distribution on the samples.

1 Comment

But what kind of distribution do you want?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.