Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. # test input
  2. _x = [1,1,2,4,2,4,6,8,9,1,2,3,1,2,1,5]
  3. _mu = 3
  4.  
  5. # define function to handle CUSUM
  6. def add_by_one(input_array, my_mu):
  7.  
  8.   # initialize new array that we will return with cumulative sum values
  9.   new_array = []
  10.  
  11.   # S0 = 0 by definition
  12.   new_array.append(0)
  13.  
  14.   # loop through the input array values, arrays start at 0
  15.  
  16.   for index in range(len(input_array)):
  17.     # calc cumulative value, store in temp value
  18.     cumulative_value = new_array[index] + input_array[index] - my_mu
  19.  
  20.     # take max of cumulative value and zero
  21.     max_value = max(0, cumulative_value)
  22.  
  23.     # append to the the array
  24.     new_array.append(max_value)
  25.  
  26.   # return new array, first 0 is S0 that we set to 0 by default
  27.   # you can optionally pop this off before return if you need to
  28.   return new_array
  29.  
  30. # print result with test array
  31. print(add_by_one(_x, _mu))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement