Issue
Let me I from the question in an easy way:
I usually have about k=3000€ per month. This month had n=26 working days (in July as you see in the following picture), and generally, I work something between [100,120]€ each day.
Note: k could be +/- x€ if needed, but it should be as minimum as possible.
what I tried to generate n numbers within [a,b] interval, but it should be very close to the k:
import numpy as np
#rng = np.random.default_rng(123)
#arr1 = rng.uniform(100, 120,26)
arr1 = np.random.randint(100,120,26)
#array([107, 115, 116, 105, 104, 110, 110, 107, 116, 110, 101, 112, 109,
# 111, 118, 102, 108, 113, 101, 112, 111, 116, 111, 109, 110, 107])
total = np.sum(arr1)
print(f'Sum of all the elements is {total}')
#Sum of all the elements is 2851
I don't have any clue to fulfil the condition. The summation of generated random numbers should be close to k [k, k+i] i=minimum e.g. [3000€, 3050€].
Edit1: I tried to compare the distribution quality of generated values offered by plotting/fitting offered solutions from @Murali & @btilly in the form of PDF as below:
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
h = arr1
h.sort()
hmean = np.mean(h)
hstd = np.std(h)
pdf = stats.norm.pdf(h, hmean, hstd)
#plt.hist(arr1)
plt.plot(h, pdf,'-o',alpha=0.4) # including h here is crucial
So clearly one has a skew, but the other is the normal distribution.
Solution
You can use a Gaussian distribution for a given mean and standard deviation
mu = 3000/26
sigma = 5 ## allowed deviation from mean +- 5 from mean i.e [110.4,120.4]
arr1 = np.random.normal(mu, sigma, 26)
print(np.sum(arr1))
# 3011.268333226019
You can also play with other distributions and see which fits your purpose.
Answered By - Murali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.