Vector mean calculation?
I have a vector with min equal to 412. Why am I getting a negative mean? -- Thomas Davidoff Assistant Professor Haas School of Business UC Berkeley Berkeley, CA 94618 Phone: (510) 643-1425 Fax: (510) 643-7357 email: davidoff@haas.berkeley.edu web: http://faculty.haas.berkeley.edu/davidoff/
Quoting Thomas Davidoff <davidoff@haas.berkeley.edu>:
I have a vector with min equal to 412. Why am I getting a negative mean?
Without more details, it's quite difficult to be sure. Most likely, because it's very long and of integer type (ints are 32 bit objects, so you are effectively doing mod 2^31 arithmetic with negative wraparound). Cheers, f
On Apr 21, 2005, at 5:41 PM, Fernando.Perez@colorado.edu wrote:
Quoting Thomas Davidoff <davidoff@haas.berkeley.edu>:
I have a vector with min equal to 412. Why am I getting a negative mean?
Without more details, it's quite difficult to be sure. Most likely, because it's very long and of integer type (ints are 32 bit objects, so you are effectively doing mod 2^31 arithmetic with negative wraparound).
Why does Python allow modular arithmetic when the user doesn't request modular arithmetic? Lance
Lance Boyle wrote:
On Apr 21, 2005, at 5:41 PM, Fernando.Perez@colorado.edu wrote:
Quoting Thomas Davidoff <davidoff@haas.berkeley.edu>:
I have a vector with min equal to 412. Why am I getting a negative mean?
Without more details, it's quite difficult to be sure. Most likely, because it's very long and of integer type (ints are 32 bit objects, so you are effectively doing mod 2^31 arithmetic with negative wraparound).
Why does Python allow modular arithmetic when the user doesn't request modular arithmetic?
Well, it's just what happens in C always: regular ints are 32 bit objects, hence with a range of [-2**31,2**31-1]. If you start adding and go beyond the right endpoint of the interval, it will wrap around the left. Pure python shields you from this with silent promotion to python longs, which are arbitrary-length integers. But in Numeric/numarray, you are using raw C objects (for speed reasons), so you can't escape some of their limitations (which are in many cases precisely what gives you speed). Life is full of compromises... Best, f
On Apr 22, 2005, at 3:04 AM, Fernando.Perez@colorado.edu wrote:
Lance Boyle wrote:
On Apr 21, 2005, at 5:41 PM, Fernando.Perez@colorado.edu wrote:
Quoting Thomas Davidoff <davidoff@haas.berkeley.edu>:
I have a vector with min equal to 412. Why am I getting a negative mean?
Without more details, it's quite difficult to be sure. Most likely, because it's very long and of integer type (ints are 32 bit objects, so you are effectively doing mod 2^31 arithmetic with negative wraparound).
Why does Python allow modular arithmetic when the user doesn't request modular arithmetic?
Well, it's just what happens in C always: regular ints are 32 bit objects, hence with a range of [-2**31,2**31-1]. If you start adding and go beyond the right endpoint of the interval, it will wrap around the left. Pure python shields you from this with silent promotion to python longs, which are arbitrary-length integers. But in Numeric/numarray, you are using raw C objects (for speed reasons), so you can't escape some of their limitations (which are in many cases precisely what gives you speed). Life is full of compromises...
Best,
f
Well, I apologize for a bit of baiting. I get upset when a language lets me do something which I didn't intend, and when it is preventable in the design of the language. Python is 30 years newer than C but yet we have to put up with some of the same deficiencies of C (OK, so it was in a library). If the original poster was really suffering wrap-around (getting a mean value of a vector that is less than the minimum value), then he/she could be the poster boy/girl for poor language design. We might inquire about how much time he saved by using unchecked modular arithmetic versus how much time he wasted debugging his program, posting to this list, and making repairs. We might also ask him what might have been the consequences of getting such an incorrect result that he did not catch. Flame away. 8^) Lance
On Apr 23, 2005, at 2:06 AM, Lance Boyle wrote:
On Apr 22, 2005, at 3:04 AM, Fernando.Perez@colorado.edu wrote:
Lance Boyle wrote:
On Apr 21, 2005, at 5:41 PM, Fernando.Perez@colorado.edu wrote:
Quoting Thomas Davidoff <davidoff@haas.berkeley.edu>:
I have a vector with min equal to 412. Why am I getting a negative mean?
Without more details, it's quite difficult to be sure. Most likely, because it's very long and of integer type (ints are 32 bit objects, so you are effectively doing mod 2^31 arithmetic with negative wraparound).
Why does Python allow modular arithmetic when the user doesn't request modular arithmetic?
Well, it's just what happens in C always: regular ints are 32 bit objects, hence with a range of [-2**31,2**31-1]. If you start adding and go beyond the right endpoint of the interval, it will wrap around the left. Pure python shields you from this with silent promotion to python longs, which are arbitrary-length integers. But in Numeric/numarray, you are using raw C objects (for speed reasons), so you can't escape some of their limitations (which are in many cases precisely what gives you speed). Life is full of compromises...
Well, I apologize for a bit of baiting. I get upset when a language lets me do something which I didn't intend, and when it is preventable in the design of the language. Python is 30 years newer than C but yet we have to put up with some of the same deficiencies of C (OK, so it was in a library). If the original poster was really suffering wrap-around (getting a mean value of a vector that is less than the minimum value), then he/she could be the poster boy/girl for poor language design. We might inquire about how much time he saved by using unchecked modular arithmetic versus how much time he wasted debugging his program, posting to this list, and making repairs. We might also ask him what might have been the consequences of getting such an incorrect result that he did not catch.
30 years in what universe? C has been around for about 34 years (1971), and Python wasn't born yesterday (1990). Anyway, you could ask him if he would prefer a default implementation that ran 10x slower (probably worse) because it performed checked arithmetic. You get one or the other, and both is simply more work. If this function was 100% Python and didn't bother with Numeric/etc. it would've done the right thing out of the box, but it might still be calculating :) -bob
Bob Ippolito wrote:
On Apr 23, 2005, at 2:06 AM, Lance Boyle wrote:
On Apr 22, 2005, at 3:04 AM, Fernando.Perez@colorado.edu wrote:
Lance Boyle wrote:
On Apr 21, 2005, at 5:41 PM, Fernando.Perez@colorado.edu wrote:
Quoting Thomas Davidoff <davidoff@haas.berkeley.edu>:
I have a vector with min equal to 412. Why am I getting a negative mean?
Without more details, it's quite difficult to be sure. Most likely, because it's very long and of integer type (ints are 32 bit objects, so you are effectively doing mod 2^31 arithmetic with negative wraparound).
Why does Python allow modular arithmetic when the user doesn't request modular arithmetic?
Well, it's just what happens in C always: regular ints are 32 bit objects, hence with a range of [-2**31,2**31-1]. If you start adding and go beyond the right endpoint of the interval, it will wrap around the left. Pure python shields you from this with silent promotion to python longs, which are arbitrary-length integers. But in Numeric/numarray, you are using raw C objects (for speed reasons), so you can't escape some of their limitations (which are in many cases precisely what gives you speed). Life is full of compromises...
Well, I apologize for a bit of baiting. I get upset when a language lets me do something which I didn't intend, and when it is preventable in the design of the language. Python is 30 years newer than C but yet we have to put up with some of the same deficiencies of C (OK, so it was in a library). If the original poster was really suffering wrap-around (getting a mean value of a vector that is less than the minimum value), then he/she could be the poster boy/girl for poor language design. We might inquire about how much time he saved by using unchecked modular arithmetic versus how much time he wasted debugging his program, posting to this list, and making repairs. We might also ask him what might have been the consequences of getting such an incorrect result that he did not catch.
30 years in what universe? C has been around for about 34 years (1971), and Python wasn't born yesterday (1990).
Anyway, you could ask him if he would prefer a default implementation that ran 10x slower (probably worse) because it performed checked arithmetic. You get one or the other, and both is simply more work. If this function was 100% Python and didn't bother with Numeric/etc. it would've done the right thing out of the box, but it might still be calculating :)
-bob
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
From the original poster: the mean miscalculation was the least of my programming problems in this exercise (so far) - not a bad learning experience.
Lance Boyle wrote:
On Apr 21, 2005, at 5:41 PM, Fernando.Perez@colorado.edu wrote:
Quoting Thomas Davidoff <davidoff@haas.berkeley.edu>:
I have a vector with min equal to 412. Why am I getting a negative mean?
Without more details, it's quite difficult to be sure. Most likely, because it's very long and of integer type (ints are 32 bit objects, so you are effectively doing mod 2^31 arithmetic with negative wraparound).
Why does Python allow modular arithmetic when the user doesn't request modular arithmetic?
To get speed, scipy must assume that if you are using an integer array then you actually are requesting modular arithmetic (to the size of the integers in question). The other option would be to silently and automatically upcast when you get too large. I don't think that is the right way to go. It would be a good idea, however, to allow the user to specify a larger type within which to accomplish a reduction. Look for this in scipy.base (the new Numeric). I'm thinking of something like add.reduce(a, Int32) or add.reduce(a, Float64) which gives a return type for the array. -Travis
I'm interested in using scipy and probably matplotlib to analyse and plot data from a large-scale ocean model whose data are held in hdf5 format. The most obvious way to read (and write) data to hdf5 would seem to be through the pytables package. From my (admittedly cursory) reading it seems like pytables works with numarray rather than numeric. Again, I may have misunderstood, but I gather that scipy is incompatible with numarray but uses numeric. If this is indeed a problem, is it likely to be solved by the development of numeric3/scipy.base? George Nurser.
George Nurser wrote:
I'm interested in using scipy and probably matplotlib to analyse and plot data from a large-scale ocean model whose data are held in hdf5 format.
The most obvious way to read (and write) data to hdf5 would seem to be through the pytables package. From my (admittedly cursory) reading it seems like pytables works with numarray rather than numeric.
It needs numarray internally, but it can work with Numeric just fine.
Again, I may have misunderstood, but I gather that scipy is incompatible with numarray but uses numeric.
At the moment, yes.
If this is indeed a problem, is it likely to be solved by the development of numeric3/scipy.base?
Yes, that is the driving purpose behind this development effort. -- Robert Kern rkern@ucsd.edu "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter
Hi George! The interface of pytables does, at least in the specs, define the possibility to read the data, yielding the format-flavour numeric, as well as numarray and the basic python lists. Out of no known reason, I up until now have failed to get numeric arrays, but a simple workarround-method can convert lists to numeric types on the fly, so this is nothing to worry about. IMHO this is no de facto compatibility problem, but just a matter of a simple conversion. For me, and the high-energy physics data that I'm storing in hdf-files for analysis this combination works perfectly. regards, Gerald Richter. On Tue, Apr 26, 2005 at 02:19:43PM +0100, George Nurser wrote:
I'm interested in using scipy and probably matplotlib to analyse and plot data from a large-scale ocean model whose data are held in hdf5 format.
The most obvious way to read (and write) data to hdf5 would seem to be through the pytables package. From my (admittedly cursory) reading it seems like pytables works with numarray rather than numeric.
Again, I may have misunderstood, but I gather that scipy is incompatible with numarray but uses numeric.
If this is indeed a problem, is it likely to be solved by the development of numeric3/scipy.base?
George Nurser.
A Dimarts 26 Abril 2005 15:36, Gerald Richter va escriure:
Out of no known reason, I up until now have failed to get numeric arrays, but a simple workarround-method can convert lists to numeric types on the fly, so this is nothing to worry about.
That's weird... I've implemented a test suite for Numeric in PyTables that checks that and never gave problems. If you are willing to send me a small script exposing the problem I'll look into this. Cheers, -- Francesc Altet
A Dimarts 26 Abril 2005 15:19, George Nurser va escriure:
I'm interested in using scipy and probably matplotlib to analyse and plot data from a large-scale ocean model whose data are held in hdf5 format.
The most obvious way to read (and write) data to hdf5 would seem to be through the pytables package. From my (admittedly cursory) reading it seems like pytables works with numarray rather than numeric.
As Robert said, PyTables works internally with numarray, but supports Numeric by doing conversions. However the numarray --> Numeric (i.e. for reading operations with Numeric flavor) conversions implies a memory copy right now. Hopefully, this will be fixed sometime in the future, with the advent of Numeric 24.0 or scipy.base. Nevertheless, with the latest releases (those available in http://www.carabos.com/downloads/pytables/snapshots/), I've included a patch (based on a suggestion of Todd Miller) to optimize this conversion, and with that, someone has reported the next input speeds: """ On the SGI Altix with a Raid 5 disk array, up to 350 mb/s (using Numeric). With numarrays, I can approach 900 mb/s. BTW, got a decent speed up on my Mac OS X machine as well, close to 50x. """ So, I think you can use PyTables with Numeric and still expect a decent performance. Cheers, -- Francesc Altet
participants (9)
-
Bob Ippolito -
Fernando.Perez@colorado.edu -
Francesc Altet -
George Nurser -
Gerald Richter -
Lance Boyle -
Robert Kern -
Thomas Davidoff -
Travis Oliphant