calculating a self.value, self.randomnum = normalvariate(x, y)

Steven D'Aprano steve at REMOVETHIS.cybersource.com.au
Sat Jun 20 10:54:22 EDT 2009


Vincent Davis wrote:

>> # Clamp a normal distribution outcome

I don't know who you are quoting -- you should give attribution to them.


>> def clamp(input, min=0, max=100):
...
>> if input < min:
>> return min
>> elif input > max:
>> return max
>> else:
>> return input

An easier way to do this:

return min(100, max(0, input))

but again, I stress that this will strongly distort the random distribution.
It's probably not what you want.


> Why not have the def clamp inside the class? 

Why bother?


> I would prefer to keep 
> everything I need for the class together.

But you don't. You have the random.normalvariate in a completely different
module. I'm sure you have other operations like +, - etc as built-in
functions. Not everything is inside the class.



> I am new to classes but I have to say things like if __name__ ==
> "__main__": have no intuitive meaning to me. It is true I don't know
> what __name__ and __main__ do and I can look it up but I don't even
> have a guess based on the names and usage.


When you import a module with the line:

import mymodule

Python automatically creates a variable mymodule.__name__ and sets it to the
string "__mymodule__".

When you run a module as a script, by calling it from the shell, using (for
example):

$ python mymodule.py

Python automatically creates the variable mymodule.__name__ as before, but
this time sets its value to the string "__main__".

So the construction:

if __name__ == "__main__":
   do_stuff_here()


is a way to include code that will only be executed when running the module
as a script, not when it is imported as a module.



> I am Now not sure if that is what I want or If I want to redraw from
> the distribution. I am wanting to simulate test scores. My option see
> to be to draw from a normal (I don't want linear) distribution and
> scale it to 0-100 or clamp it as you (Xavier) suggested or draw from
> the distribution again (this is what I was thinking) I think this is
> still what I want but I should look up the implications of each. The
> problem I have with the clamp is that the tails in the sample could be
> large.

Strictly speaking, you want a different distribution, not normal. Possibly
the F-distribution? Anyway, whatever it is, unless you need very accurate
results, a truncated normal distribution shouldn't be *too* far off: close
enough for government work. Clamping will not be very good: it will result
in an excess of 0 and 100 scores. Imagine a graph that looks vaguely like
this:


000: **********
010: *
020: **
030: ****
040: *******
050: *********
060: **********
070: ********
080: *****
090: **
100: *************

That's what you'll get by clamping (possibly exaggerated for effect).

Truncating with a while loop will result in something closer to this:

000: *
010: *
020: **
030: ****
040: *******
050: *********
060: **********
070: ********
080: *****
090: **
100: *

which is far less distorted.



-- 
Steven




More information about the Python-list mailing list