Addition problems

V

vm

I'm analyzing some data in python. I have stumbled across unusual problem:

I have the function...

def fun1(params_used_below_except_lk_and_lk2):
lk = 0.0
lk2 = 0.0
for raw_data, hist, freq in raw_data_hist_list:
lk2 = lk2 + fun2(some_constants_and_params_from_this_scope)
q = fun2(same_args_as_before)
lk = lk + q
print lk, lk2

For some set of input parameters, This function with specific input
prints out the same constant twice (as expected):

15377.7424582 15377.7424582 (twice the same value)

If I comment out lines q = fun2.... and lk = lk + q, fun1, with the same
parameters, prints:

0.0 3.3469936856

First value is expected, but I cannot understand how come the second
value is different in these two cases!

Thanks in advance!


fun1 source:

def process_measurements_beta_lk(x, raw_data_hist_list):
lk = 0.0
lk2 = 0.0
(p,q,loc,scale) = x
for raw_data, hist, freq in raw_data_hist_list:
lk2 = lk2 + get_beta_func_fit_quality_hist_lk('beta',
(p,q,loc,scale), raw_data, hist)
# q = get_beta_func_fit_quality_hist_lk('beta',(p,q,loc,scale),
raw_data, hist)
# lk = lk + q

print lk, lk2
retrun lk
 
R

Rob Williscroft

vm wrote in in
gmane.comp.python.general:
def fun1(params_used_below_except_lk_and_lk2):
lk = 0.0
lk2 = 0.0
for raw_data, hist, freq in raw_data_hist_list:
lk2 = lk2 + fun2(some_constants_and_params_from_this_scope)
q = fun2(same_args_as_before)
lk = lk + q
print lk, lk2

For some set of input parameters, This function with specific input
prints out the same constant twice (as expected):

15377.7424582 15377.7424582 (twice the same value)

If I comment out lines q = fun2.... and lk = lk + q, fun1, with the
same parameters, prints:

0.0 3.3469936856

First value is expected, but I cannot understand how come the second
value is different in these two cases!

Clearly the value returned by fun2 (get_beta_func_fit_quality_hist_lk)
is different the second time you call it.

So it either depends on and modifies a global or a value referenced or
contained in one of the arguments.

Rob.
 
R

Roel Schroeven

Op 2011-04-01 16:04, vm schreef:
I'm analyzing some data in python. I have stumbled across unusual problem:

I have the function...

def fun1(params_used_below_except_lk_and_lk2):
lk = 0.0
lk2 = 0.0
for raw_data, hist, freq in raw_data_hist_list:
lk2 = lk2 + fun2(some_constants_and_params_from_this_scope)
q = fun2(same_args_as_before)
lk = lk + q
print lk, lk2

For some set of input parameters, This function with specific input
prints out the same constant twice (as expected):

15377.7424582 15377.7424582 (twice the same value)

If I comment out lines q = fun2.... and lk = lk + q, fun1, with the same
parameters, prints:

0.0 3.3469936856

First value is expected, but I cannot understand how come the second
value is different in these two cases!

Let's have a look at your source:
fun1 source:

def process_measurements_beta_lk(x, raw_data_hist_list):
lk = 0.0
lk2 = 0.0
(p,q,loc,scale) = x
for raw_data, hist, freq in raw_data_hist_list:
lk2 = lk2 + get_beta_func_fit_quality_hist_lk('beta',
(p,q,loc,scale), raw_data, hist)
# q = get_beta_func_fit_quality_hist_lk('beta',(p,q,loc,scale),
raw_data, hist)
# lk = lk + q
print lk, lk2
retrun lk

The lie with "lk2 = " uses the value of q. In the version with commented
lines, that value is never changed, while it is changed each iteration
in the version with the lines uncommented.

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top