Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random methods
#11
(Sep-04-2022, 12:23 AM)Skaperen Wrote: but, i want it to look about like i am using that.
A bell curve has two parameters: the mean and the standard deviation. If you want something that looks like a bell curve, you need to choose the mean and the standard deviation. With your method, you always have s = sqrt(N/12).

Run the following code to visualize the distribution that my method generates for a mean value of 20 minutes and a standard dev of 1.5 minutes. The SIZE parameter is the size of the sample.
from scipy.stats import gaussian_kde
from numpy import linspace
import numpy as np
import random
from matplotlib import pyplot as plt

def sleeping_time(mu, sigma, min, max):
    while True:
        t = random.normalvariate(mu, sigma)
        if min <= t <= max:
            return t

# generate experimental data
SIZE = 20000
data = np.array([sleeping_time(20 * 60,  90, 16 * 60, 24 * 60)/60 for i in range(SIZE)])

# section to approximate the data's density curve
# this create the kernel, given an array it will estimate the probability over that values
kde = gaussian_kde( data )
# these are the values over wich your kernel will be evaluated
dist_space = linspace( min(data), max(data), 100 )
# plot the results
plt.plot( dist_space, kde(dist_space) )
plt.show()
   
Reply
#12
i don't understand lines 19 and up to know how to confine the curve to where i need it to be. i first learned the bell curve from code (mainframe assembly) that calculated an imperfect simulate of it that made a configured number (6) of calls to a function that returned a random integer in 0..(2**32) which was scaled to a float in 0.0..1.0 and all summed and multiplied by the configured value and returned. it was just simple stuff but it looked about right when i plotted a Fortran translation of it (i was not allowed to run assembly on that system back in 1971 and they didn't have Python, yet).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
(Sep-04-2022, 05:15 PM)Skaperen Wrote: i don't understand lines 19 and up to know how to confine the curve to where i need it to be.
lines 17 and up are only additional code that allow scipy to deduce graphically a density curve from a set of experimental data. You don't need these lines.

The sleeping_time() function is the useful function that generates the data. It simply uses a bell curve (mathematically called a gaussian function) to generate the points, and it confines the curve where it need to be simply by rejecting the values that are below the min or above the max. It amounts to simply truncating the bell curve.

To describe the rejection method, the average height of american males is about 175.3 cm according to Wikipedia. We take a random US citizen, if his height is between 160 and 190 cm we accept the value, otherwise we take another citizen until we find one in that range. It is a very simple idea.
Reply
#14
you can create a bell curve without specifying the standard deviation. i've done this around a range that runs from 0 to +N as well as around a range that runs from -N to +N. specifying the range is like specifying the mean except that it violates the fact that bell curve, a normal distribution, is really in the range -INF to +INF.

what i will call a "fake bell curve" is created by summing up uniform random numbers that run from -N to +N (i learned this by looking at IBM/360 assembler source code of a math library back in my earliest mainframe days when i was still in high school) and maybe dividing by the count of numbers summed (all the same range around mean 0). i don't know what the resulting SD is, but it seems to be small. the advantage of the fake bell curve is that it has a finite range.

maybe what i should do is get the result of a normal distribution and if that result is out of bounds of the desired range limit (which should be reasonably wide, like at least -SD to +SD), discard it and try for another. IMHO, it is unlikely to get stuck in a loop for very long unless the range is crazy narrow.

all my statistics knowledge is from over 40 years ago Sad
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#15
(Sep-26-2022, 05:42 AM)Skaperen Wrote: maybe what i should do is get the result of a normal distribution and if that result is out of bounds of the desired range limit (which should be reasonably wide, like at least -SD to +SD), discard it and try for another. IMHO, it is unlikely to get stuck in a loop for very long unless the range is crazy narrow.
It is exactly what my function does in the above code.
Reply
#16
doesn't the packaged-in-python module random provide a normal distribution function? random.normalvariate()?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#17
i think i was reading the wrong code. i see it now.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help on random methods jip31 2 1,976 Apr-29-2021, 12:15 PM
Last Post: jip31

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020