I get slightly different results when I repeat a calculation. In a
long simulation the differences snowball and swamp the effects I am
trying to measure.

In the attach script there are three tests.

In test1, I construct matrices x and y and then repeatedly calculate z
= calc(x,y). The result z is the same every time. So this test passes.

In test2, I construct matrices x and y each time before calculating z
= calc(x,y). Sometimes z is slightly different---of the order of 1e-21
to 1e-18. But the x's test to be equal and so do the y's. This test
fails. (It doesn't fail on my friend's windows box; I'm running
linux.)

test3 is the same as test2 but I calculate z like this: z = calc(100*x
,y) / (100 * 100). This test passes.

What is going on?

Here is some sample output:

import repeat
repeat.all()
4 z different 8.47032947254e-22
5 z different 8.47032947254e-22
7 z different 8.47032947254e-22
9 z different 8.47032947254e-22
10 z different 8.47032947254e-22
16 z different 8.47032947254e-22
24 z different 8.47032947254e-22
25 z different 8.47032947254e-22
26 z different 8.47032947254e-22
27 z different 8.47032947254e-22
30 z different 8.47032947254e-22
32 z different 8.47032947254e-22
34 z different 8.47032947254e-22
35 z different 8.47032947254e-22
36 z different 8.47032947254e-22
39 z different 8.47032947254e-22
40 z different 8.47032947254e-22
41 z different 8.47032947254e-22
45 z different 8.47032947254e-22
46 z different 8.47032947254e-22
50 z different 8.47032947254e-22
52 z different 8.47032947254e-22
53 z different 8.47032947254e-22
55 z different 8.47032947254e-22
56 z different 8.47032947254e-22
58 z different 8.47032947254e-22
66 z different 8.47032947254e-22
67 z different 8.47032947254e-22
71 z different 8.47032947254e-22
73 z different 8.47032947254e-22
74 z different 8.47032947254e-22
83 z different 8.47032947254e-22
86 z different 8.47032947254e-22
87 z different 8.47032947254e-22
88 z different 8.47032947254e-22
89 z different 8.47032947254e-22
90 z different 8.47032947254e-22
92 z different 8.47032947254e-22

test1:  0  differences
test2:  38  differences
test3:  0  differences

Repeated runs tend to give me the same number of differences in test2
for several runs. Then I get a new number of differences which last
for severals runs...
import numpy.matlib as M
    
def load():

    # x data
    x = M.zeros((3,3))
    x[0,0] = 0.00301404794991108
    x[0,1] = 0.00264742266711118
    x[0,2] = -0.00112705028731085
    x[1,0] = 0.0228605377994491
    x[1,1] = 0.00337153112741583
    x[1,2] = -0.00823674912992519
    x[2,0] = 0.00447839875836716
    x[2,1] = 0.00274880280576514
    x[2,2] = -0.00161133933606597
         
    # y data
    y = M.zeros((3,1))
    y[0,0] = 0.000885398
    y[1,0] = 0.00667193
    y[2,0] = 0.000324727
    
    return x, y
    
def calc(x, y): 
    return x * (x.T * y)                             

def test1(nsim=100):
    x, y = load() 
    z0 = calc(x, y)
    ndiff = 0
    for i in xrange(nsim):
        z = calc(x, y)
        if (z != z0).any():
            ndiff += 1
            print i, 'z different', abs(z - z0).max()       
    return ndiff         
            
def test2(nsim=100):
    x0, y0 = load() 
    z0 = calc(x0, y0)
    ndiff = 0
    for i in xrange(nsim):
        x, y = load() 
        z = calc(x, y)
        if (x != x0).any():
            print i, 'x different', abs(x - x0).max()
        if (y != y0).any():
            print i, 'y different', abs(y - y0).max()
        if (z != z0).any():
            print i, 'z different', abs(z - z0).max()
            ndiff += 1  
    return ndiff  
    
def test3(nsim=100):
    x0, y0 = load() 
    z0 = calc(100*x0, y0) / (100 * 100)
    ndiff = 0
    for i in xrange(nsim):
        x, y = load() 
        z = calc(100*x, y) / (100 * 100)
        if (x != x0).any():
            print i, 'x different', abs(x - x0).max()
        if (y != y0).any():
            print i, 'y different', abs(y - y0).max()
        if (z != z0).any():
            print i, 'z different', abs(z - z0).max()
            ndiff += 1      
    return ndiff         
    
def all():
    n1 = test1()
    n2 = test2() 
    n3 = test3()
    print
    print 'test1: ', n1, ' differences'
    print 'test2: ', n2, ' differences'                            
    print 'test3: ', n3, ' differences'
_______________________________________________
Numpy-discussion mailing list
[email protected]
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to