Calculating difference statistics over a moving window Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Calculating and displaying number statisticsMoving average calculationMoving window with complete boundary in PythonOptimization for calculating statisticsNormalizing data over a distributionClass Design: Calculating statistics from weighted valuesCalculating exponential moving averages with multiples threadsStatistics mode functionModule for statisticsStatistics and calculations

Who's this lady in the war room?

The Nth Gryphon Number

How do you cope with tons of web fonts when copying and pasting from web pages?

How can I introduce the names of fantasy creatures to the reader?

Who's the Giant Batman in the back of this dark knights metal Batman picture?

Should man-made satellites feature an intelligent inverted "cow catcher"?

Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?

Is a copyright notice with a non-existent name be invalid?

Bash script to execute command with file from directory and condition

JImage - Set generated image quality

Is it possible to intall libXft.so.2 on WSL?

Centre cell vertically in tabularx across multiple multiline rows

How to achieve cat-like agility?

Table formatting with tabularx?

Maximum rotation made by a symmetric positive definite matrix?

Is there night in Alpha Complex?

3D Masyu - A Die

Understanding piped commands in GNU/Linux

What should one know about term logic before studying propositional and predicate logic?

Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?

As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?

The test team as an enemy of development? And how can this be avoided?

Which types of prepositional phrase is "toward its employees" in Philosophy guiding the organization's policies towards its employees is not bad?

Changing order of draw operation in PGFPlots



Calculating difference statistics over a moving window



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Calculating and displaying number statisticsMoving average calculationMoving window with complete boundary in PythonOptimization for calculating statisticsNormalizing data over a distributionClass Design: Calculating statistics from weighted valuesCalculating exponential moving averages with multiples threadsStatistics mode functionModule for statisticsStatistics and calculations



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3












$begingroup$


I'd like to calculate some statistics of given data (frequencies of difference among elements on various distances in percent multiplied by 10) using moving window within that data. Is it possible to speed up the code below? I noticed that some calculations are repeating. But I was not able to exclude them without additional slowness.



def get_dist_stat(pdata, pwin_length):
''' pdata - given data array
pwin_length - the lenght of window
the function returns stat table where
row represents the distance between elements
col represents the difference for that distance in percent multiplied by 10 (assume that maximum difference can be 20 percent)

'''

l_data = len(pdata)
l_win = pwin_length
print("l_data=", l_data)
print("l_win=", l_win)

# stat table
stat_table = np.zeros((l_win-1, 20*10), dtype = int)

# loop over all data
for k in range(l_data - l_win + 1):

win = pdata[k : k + l_win]
print('-' * 10)
print("k=", k, " kend=", k + l_win )

print("win=", win)

# loop over window
for i in range(1 , l_win):
b=win[i:]
a=win[:-i]
diff=(abs((b-a)/a*100 ) * 10).astype(int)
print("i=",i)
print("b=", b)
print("a=", a)
print("diff=",diff)

# storing found differences into stat table
apercents, acount = np.unique(diff, return_counts = True)
l_apercents = len(apercents)
for j in range(l_apercents):
stat_table[i-1, apercents[j]] += acount[j]
return stat_table

adata=np.array([1.1,1.2,1.3,1.4,1.5])
print("adata=", adata)

astat_table=get_dist_stat(adata,3)
print(astat_table)


And that is its output



adata= [1.1 1.2 1.3 1.4 1.5]
l_data= 5
l_win= 3
----------
k= 0 kend= 3
win= [1.1 1.2 1.3]
i= 1
b= [1.2 1.3]
a= [1.1 1.2]
diff= [90 83]
i= 2
b= [1.3]
a= [1.1]
diff= [181]
----------
k= 1 kend= 4
win= [1.2 1.3 1.4]
i= 1
b= [1.3 1.4]
a= [1.2 1.3]
diff= [83 76]
i= 2
b= [1.4]
a= [1.2]
diff= [166]
----------
k= 2 kend= 5
win= [1.3 1.4 1.5]
i= 1
b= [1.4 1.5]
a= [1.3 1.4]
diff= [76 71]
i= 2
b= [1.5]
a= [1.3]
diff= [153]
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]









share|improve this question











$endgroup$




bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.





















    3












    $begingroup$


    I'd like to calculate some statistics of given data (frequencies of difference among elements on various distances in percent multiplied by 10) using moving window within that data. Is it possible to speed up the code below? I noticed that some calculations are repeating. But I was not able to exclude them without additional slowness.



    def get_dist_stat(pdata, pwin_length):
    ''' pdata - given data array
    pwin_length - the lenght of window
    the function returns stat table where
    row represents the distance between elements
    col represents the difference for that distance in percent multiplied by 10 (assume that maximum difference can be 20 percent)

    '''

    l_data = len(pdata)
    l_win = pwin_length
    print("l_data=", l_data)
    print("l_win=", l_win)

    # stat table
    stat_table = np.zeros((l_win-1, 20*10), dtype = int)

    # loop over all data
    for k in range(l_data - l_win + 1):

    win = pdata[k : k + l_win]
    print('-' * 10)
    print("k=", k, " kend=", k + l_win )

    print("win=", win)

    # loop over window
    for i in range(1 , l_win):
    b=win[i:]
    a=win[:-i]
    diff=(abs((b-a)/a*100 ) * 10).astype(int)
    print("i=",i)
    print("b=", b)
    print("a=", a)
    print("diff=",diff)

    # storing found differences into stat table
    apercents, acount = np.unique(diff, return_counts = True)
    l_apercents = len(apercents)
    for j in range(l_apercents):
    stat_table[i-1, apercents[j]] += acount[j]
    return stat_table

    adata=np.array([1.1,1.2,1.3,1.4,1.5])
    print("adata=", adata)

    astat_table=get_dist_stat(adata,3)
    print(astat_table)


    And that is its output



    adata= [1.1 1.2 1.3 1.4 1.5]
    l_data= 5
    l_win= 3
    ----------
    k= 0 kend= 3
    win= [1.1 1.2 1.3]
    i= 1
    b= [1.2 1.3]
    a= [1.1 1.2]
    diff= [90 83]
    i= 2
    b= [1.3]
    a= [1.1]
    diff= [181]
    ----------
    k= 1 kend= 4
    win= [1.2 1.3 1.4]
    i= 1
    b= [1.3 1.4]
    a= [1.2 1.3]
    diff= [83 76]
    i= 2
    b= [1.4]
    a= [1.2]
    diff= [166]
    ----------
    k= 2 kend= 5
    win= [1.3 1.4 1.5]
    i= 1
    b= [1.4 1.5]
    a= [1.3 1.4]
    diff= [76 71]
    i= 2
    b= [1.5]
    a= [1.3]
    diff= [153]
    [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]









    share|improve this question











    $endgroup$




    bumped to the homepage by Community 3 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      3












      3








      3





      $begingroup$


      I'd like to calculate some statistics of given data (frequencies of difference among elements on various distances in percent multiplied by 10) using moving window within that data. Is it possible to speed up the code below? I noticed that some calculations are repeating. But I was not able to exclude them without additional slowness.



      def get_dist_stat(pdata, pwin_length):
      ''' pdata - given data array
      pwin_length - the lenght of window
      the function returns stat table where
      row represents the distance between elements
      col represents the difference for that distance in percent multiplied by 10 (assume that maximum difference can be 20 percent)

      '''

      l_data = len(pdata)
      l_win = pwin_length
      print("l_data=", l_data)
      print("l_win=", l_win)

      # stat table
      stat_table = np.zeros((l_win-1, 20*10), dtype = int)

      # loop over all data
      for k in range(l_data - l_win + 1):

      win = pdata[k : k + l_win]
      print('-' * 10)
      print("k=", k, " kend=", k + l_win )

      print("win=", win)

      # loop over window
      for i in range(1 , l_win):
      b=win[i:]
      a=win[:-i]
      diff=(abs((b-a)/a*100 ) * 10).astype(int)
      print("i=",i)
      print("b=", b)
      print("a=", a)
      print("diff=",diff)

      # storing found differences into stat table
      apercents, acount = np.unique(diff, return_counts = True)
      l_apercents = len(apercents)
      for j in range(l_apercents):
      stat_table[i-1, apercents[j]] += acount[j]
      return stat_table

      adata=np.array([1.1,1.2,1.3,1.4,1.5])
      print("adata=", adata)

      astat_table=get_dist_stat(adata,3)
      print(astat_table)


      And that is its output



      adata= [1.1 1.2 1.3 1.4 1.5]
      l_data= 5
      l_win= 3
      ----------
      k= 0 kend= 3
      win= [1.1 1.2 1.3]
      i= 1
      b= [1.2 1.3]
      a= [1.1 1.2]
      diff= [90 83]
      i= 2
      b= [1.3]
      a= [1.1]
      diff= [181]
      ----------
      k= 1 kend= 4
      win= [1.2 1.3 1.4]
      i= 1
      b= [1.3 1.4]
      a= [1.2 1.3]
      diff= [83 76]
      i= 2
      b= [1.4]
      a= [1.2]
      diff= [166]
      ----------
      k= 2 kend= 5
      win= [1.3 1.4 1.5]
      i= 1
      b= [1.4 1.5]
      a= [1.3 1.4]
      diff= [76 71]
      i= 2
      b= [1.5]
      a= [1.3]
      diff= [153]
      [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
      0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
      [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]









      share|improve this question











      $endgroup$




      I'd like to calculate some statistics of given data (frequencies of difference among elements on various distances in percent multiplied by 10) using moving window within that data. Is it possible to speed up the code below? I noticed that some calculations are repeating. But I was not able to exclude them without additional slowness.



      def get_dist_stat(pdata, pwin_length):
      ''' pdata - given data array
      pwin_length - the lenght of window
      the function returns stat table where
      row represents the distance between elements
      col represents the difference for that distance in percent multiplied by 10 (assume that maximum difference can be 20 percent)

      '''

      l_data = len(pdata)
      l_win = pwin_length
      print("l_data=", l_data)
      print("l_win=", l_win)

      # stat table
      stat_table = np.zeros((l_win-1, 20*10), dtype = int)

      # loop over all data
      for k in range(l_data - l_win + 1):

      win = pdata[k : k + l_win]
      print('-' * 10)
      print("k=", k, " kend=", k + l_win )

      print("win=", win)

      # loop over window
      for i in range(1 , l_win):
      b=win[i:]
      a=win[:-i]
      diff=(abs((b-a)/a*100 ) * 10).astype(int)
      print("i=",i)
      print("b=", b)
      print("a=", a)
      print("diff=",diff)

      # storing found differences into stat table
      apercents, acount = np.unique(diff, return_counts = True)
      l_apercents = len(apercents)
      for j in range(l_apercents):
      stat_table[i-1, apercents[j]] += acount[j]
      return stat_table

      adata=np.array([1.1,1.2,1.3,1.4,1.5])
      print("adata=", adata)

      astat_table=get_dist_stat(adata,3)
      print(astat_table)


      And that is its output



      adata= [1.1 1.2 1.3 1.4 1.5]
      l_data= 5
      l_win= 3
      ----------
      k= 0 kend= 3
      win= [1.1 1.2 1.3]
      i= 1
      b= [1.2 1.3]
      a= [1.1 1.2]
      diff= [90 83]
      i= 2
      b= [1.3]
      a= [1.1]
      diff= [181]
      ----------
      k= 1 kend= 4
      win= [1.2 1.3 1.4]
      i= 1
      b= [1.3 1.4]
      a= [1.2 1.3]
      diff= [83 76]
      i= 2
      b= [1.4]
      a= [1.2]
      diff= [166]
      ----------
      k= 2 kend= 5
      win= [1.3 1.4 1.5]
      i= 1
      b= [1.4 1.5]
      a= [1.3 1.4]
      diff= [76 71]
      i= 2
      b= [1.5]
      a= [1.3]
      diff= [153]
      [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
      0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
      [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]






      python performance numpy statistics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 7 '18 at 21:12







      Prokhozhii

















      asked Mar 7 '18 at 12:43









      ProkhozhiiProkhozhii

      163




      163





      bumped to the homepage by Community 3 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 3 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






















          1 Answer
          1






          active

          oldest

          votes


















          0












          $begingroup$

          You've already made the key observation here, which is that most of the work is redone. Each time you pick a window, most of the calculations are the same as the previous window.



          In fact it's much faster to do all the calculations ahead of time into one big ndarray, and then for each window, pick out the calculations that are relevant. So we don't need the temporary a and b lists.



          How many dimensions do we need? Just starting point and length. It's going to be a triangular array, so we'll waste some space.



          precomputed_results = np.zeros(l_win+1, l_data), dtype = int)
          # First pass
          for interval in range(1, l_win):
          for first_point_index in range(l_data-interval):
          # compute diff relative to elements [first_point_index] and [first_point_index+interval]
          # line will be similar to precomputed_results[...] = ...

          # Second pass
          for interval in range(1, l_win):
          for first_point_index in range(l_data-interval):
          # use slicing on precomputed_results





          share|improve this answer









          $endgroup$












          • $begingroup$
            It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
            $endgroup$
            – Prokhozhii
            Jul 26 '18 at 10:13











          • $begingroup$
            @Prokhozhii Um, yes we will: when interval is 1 (which is certainly in range(1, 7), and first_point_index is 8 (which is certainly in range(1, 10-1) then first_point_index + interval is 9. I didn't write all the code, but the comments indicate which elements are getting compared.
            $endgroup$
            – Snowbody
            Jul 27 '18 at 12:57












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189045%2fcalculating-difference-statistics-over-a-moving-window%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0












          $begingroup$

          You've already made the key observation here, which is that most of the work is redone. Each time you pick a window, most of the calculations are the same as the previous window.



          In fact it's much faster to do all the calculations ahead of time into one big ndarray, and then for each window, pick out the calculations that are relevant. So we don't need the temporary a and b lists.



          How many dimensions do we need? Just starting point and length. It's going to be a triangular array, so we'll waste some space.



          precomputed_results = np.zeros(l_win+1, l_data), dtype = int)
          # First pass
          for interval in range(1, l_win):
          for first_point_index in range(l_data-interval):
          # compute diff relative to elements [first_point_index] and [first_point_index+interval]
          # line will be similar to precomputed_results[...] = ...

          # Second pass
          for interval in range(1, l_win):
          for first_point_index in range(l_data-interval):
          # use slicing on precomputed_results





          share|improve this answer









          $endgroup$












          • $begingroup$
            It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
            $endgroup$
            – Prokhozhii
            Jul 26 '18 at 10:13











          • $begingroup$
            @Prokhozhii Um, yes we will: when interval is 1 (which is certainly in range(1, 7), and first_point_index is 8 (which is certainly in range(1, 10-1) then first_point_index + interval is 9. I didn't write all the code, but the comments indicate which elements are getting compared.
            $endgroup$
            – Snowbody
            Jul 27 '18 at 12:57
















          0












          $begingroup$

          You've already made the key observation here, which is that most of the work is redone. Each time you pick a window, most of the calculations are the same as the previous window.



          In fact it's much faster to do all the calculations ahead of time into one big ndarray, and then for each window, pick out the calculations that are relevant. So we don't need the temporary a and b lists.



          How many dimensions do we need? Just starting point and length. It's going to be a triangular array, so we'll waste some space.



          precomputed_results = np.zeros(l_win+1, l_data), dtype = int)
          # First pass
          for interval in range(1, l_win):
          for first_point_index in range(l_data-interval):
          # compute diff relative to elements [first_point_index] and [first_point_index+interval]
          # line will be similar to precomputed_results[...] = ...

          # Second pass
          for interval in range(1, l_win):
          for first_point_index in range(l_data-interval):
          # use slicing on precomputed_results





          share|improve this answer









          $endgroup$












          • $begingroup$
            It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
            $endgroup$
            – Prokhozhii
            Jul 26 '18 at 10:13











          • $begingroup$
            @Prokhozhii Um, yes we will: when interval is 1 (which is certainly in range(1, 7), and first_point_index is 8 (which is certainly in range(1, 10-1) then first_point_index + interval is 9. I didn't write all the code, but the comments indicate which elements are getting compared.
            $endgroup$
            – Snowbody
            Jul 27 '18 at 12:57














          0












          0








          0





          $begingroup$

          You've already made the key observation here, which is that most of the work is redone. Each time you pick a window, most of the calculations are the same as the previous window.



          In fact it's much faster to do all the calculations ahead of time into one big ndarray, and then for each window, pick out the calculations that are relevant. So we don't need the temporary a and b lists.



          How many dimensions do we need? Just starting point and length. It's going to be a triangular array, so we'll waste some space.



          precomputed_results = np.zeros(l_win+1, l_data), dtype = int)
          # First pass
          for interval in range(1, l_win):
          for first_point_index in range(l_data-interval):
          # compute diff relative to elements [first_point_index] and [first_point_index+interval]
          # line will be similar to precomputed_results[...] = ...

          # Second pass
          for interval in range(1, l_win):
          for first_point_index in range(l_data-interval):
          # use slicing on precomputed_results





          share|improve this answer









          $endgroup$



          You've already made the key observation here, which is that most of the work is redone. Each time you pick a window, most of the calculations are the same as the previous window.



          In fact it's much faster to do all the calculations ahead of time into one big ndarray, and then for each window, pick out the calculations that are relevant. So we don't need the temporary a and b lists.



          How many dimensions do we need? Just starting point and length. It's going to be a triangular array, so we'll waste some space.



          precomputed_results = np.zeros(l_win+1, l_data), dtype = int)
          # First pass
          for interval in range(1, l_win):
          for first_point_index in range(l_data-interval):
          # compute diff relative to elements [first_point_index] and [first_point_index+interval]
          # line will be similar to precomputed_results[...] = ...

          # Second pass
          for interval in range(1, l_win):
          for first_point_index in range(l_data-interval):
          # use slicing on precomputed_results






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 9 '18 at 3:21









          SnowbodySnowbody

          7,7871545




          7,7871545











          • $begingroup$
            It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
            $endgroup$
            – Prokhozhii
            Jul 26 '18 at 10:13











          • $begingroup$
            @Prokhozhii Um, yes we will: when interval is 1 (which is certainly in range(1, 7), and first_point_index is 8 (which is certainly in range(1, 10-1) then first_point_index + interval is 9. I didn't write all the code, but the comments indicate which elements are getting compared.
            $endgroup$
            – Snowbody
            Jul 27 '18 at 12:57

















          • $begingroup$
            It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
            $endgroup$
            – Prokhozhii
            Jul 26 '18 at 10:13











          • $begingroup$
            @Prokhozhii Um, yes we will: when interval is 1 (which is certainly in range(1, 7), and first_point_index is 8 (which is certainly in range(1, 10-1) then first_point_index + interval is 9. I didn't write all the code, but the comments indicate which elements are getting compared.
            $endgroup$
            – Snowbody
            Jul 27 '18 at 12:57
















          $begingroup$
          It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
          $endgroup$
          – Prokhozhii
          Jul 26 '18 at 10:13





          $begingroup$
          It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
          $endgroup$
          – Prokhozhii
          Jul 26 '18 at 10:13













          $begingroup$
          @Prokhozhii Um, yes we will: when interval is 1 (which is certainly in range(1, 7), and first_point_index is 8 (which is certainly in range(1, 10-1) then first_point_index + interval is 9. I didn't write all the code, but the comments indicate which elements are getting compared.
          $endgroup$
          – Snowbody
          Jul 27 '18 at 12:57





          $begingroup$
          @Prokhozhii Um, yes we will: when interval is 1 (which is certainly in range(1, 7), and first_point_index is 8 (which is certainly in range(1, 10-1) then first_point_index + interval is 9. I didn't write all the code, but the comments indicate which elements are getting compared.
          $endgroup$
          – Snowbody
          Jul 27 '18 at 12:57


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189045%2fcalculating-difference-statistics-over-a-moving-window%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

          Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar