Addition problems

Discussion in 'Python' started by vm, Apr 1, 2011.

  1. vm

    vm Guest

    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
     
    vm, Apr 1, 2011
    #1
    1. Advertising

  2. vm wrote in news:in4m1u$hsc$ 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.
     
    Rob Williscroft, Apr 1, 2011
    #2
    1. Advertising

  3. 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
     
    Roel Schroeven, Apr 1, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Midnight Java Junkie

    Office Development (feature addition)

    Midnight Java Junkie, Jun 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    460
    Midnight Java Junkie
    Jun 20, 2004
  2. lezah
    Replies:
    7
    Views:
    770
    Mike Treseler
    Mar 2, 2004
  3. Julian

    Addition of one

    Julian, Oct 18, 2004, in forum: VHDL
    Replies:
    13
    Views:
    3,703
    rickman
    Oct 21, 2004
  4. Clemens Bosch

    Beginners questions for addition

    Clemens Bosch, Dec 2, 2004, in forum: VHDL
    Replies:
    0
    Views:
    562
    Clemens Bosch
    Dec 2, 2004
  5. Jim Ellsworth via DotNetMonster.com

    ListBox addition problems

    Jim Ellsworth via DotNetMonster.com, Mar 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    391
    Matthias S.
    Mar 29, 2005
Loading...

Share This Page