romspline/helpers.py
def conditional_median(x, y, k=0, repeat=0):
  """Compute the median of y conditioned on x.
  
  Input
  -----
    x      -- samples
    y      -- distributed values, possibly at same x samples
    k      -- number of nearest neighbors to condition the median on
              (default 0)
    repeat -- iterates the result of conditional_median a "repeat"
              number of times
              (default 0)
  
  Output
  ------
    x_unique  -- number of unique samples
    y_medians -- conditional median for each unique sample
  
  Comments
  --------
  When k is not zero then the median is conditioned on the 
  k nearest neighbors for each sample value. A non-zero k
  should be used when there are at most one y values specified
  for each sample.
  """
  
  assert len(x) == len(y), "Expecting arrays x and y to have the same length."
  
  x_unique = np.unique(x)
  x_args = np.arange(len(x))
  
  isort = np.argsort(x)
  y_sorted = y[isort]
  
  y_medians = np.zeros(len(x_unique), dtype='double')
  
  for rr in range(repeat+1):
    for ii in range(len(x_unique)):
      
      # Find the k nearest neighbors to xx
      mask = (abs(ii-x_args) <= k)
      
      # Compute the median of the y values for the k nearest neighbors
      y_medians[ii] = np.median(y_sorted[mask])
    
    y_sorted = y_medians
  
  return x_unique, y_medians
Compute the median of y conditioned on x.
  
  Input
  -----
    x      -- samples
    y      -- distributed values, possibly at same x samples
    k      -- number of nearest neighbors to condition the median on
              (default 0)
    repeat -- iterates the result of conditional_median a "repeat"
              number of times
              (default 0)
  
  Output
  ------
    x_unique  -- number of unique samples
    y_medians -- conditional median for each unique sample
  
  Comments
  --------
  When k is not zero then the median is conditioned on the 
  k nearest neighbors for each sample value. A non-zero k
  should be used when there are at most one y values specified
  for each sample.
  
Compute the median of y conditioned on x.
Input



Output

x_unique  -- number of unique samples
y_medians -- conditional median for each unique sample

Comments

When k is not zero then the median is conditioned on the
k nearest neighbors for each sample value. A non-zero k
should be used when there are at most one y values specified
for each sample.
Compute the median of y conditioned on x . Input Output x_unique -- number of unique samples y_medians -- conditional median for each unique sample Comments When k is not zero then the median is conditioned on the k nearest neighbors for each sample value . A non - zero k should be used when there are at most one y values specified for each sample .
def conditional_median(x, y, k=0, repeat=0):
  assert len(x) == len(y), "Expecting arrays x and y to have the same length."
  x_unique = np.unique(x)
  x_args = np.arange(len(x))
  isort = np.argsort(x)
  y_sorted = y[isort]
  y_medians = np.zeros(len(x_unique), dtype='double')
  for rr in range(repeat+1):
    for ii in range(len(x_unique)):
      mask = (abs(ii-x_args) <= k)
      y_medians[ii] = np.median(y_sorted[mask])
    y_sorted = y_medians
  return x_unique, y_medians
def conditional_median ( x , y , k = 0 , repeat = 0 ) : assert len ( x ) == len ( y ) , "Expecting arrays x and y to have the same length." x_unique = np . unique ( x ) x_args = np . arange ( len ( x ) ) isort = np . argsort ( x ) y_sorted = y [ isort ] y_medians = np . zeros ( len ( x_unique ) , dtype = 'double' ) for rr in range ( repeat + 1 ) : for ii in range ( len ( x_unique ) ) : mask = ( abs ( ii - x_args ) <= k ) y_medians [ ii ] = np . median ( y_sorted [ mask ] ) y_sorted = y_medians return x_unique , y_medians
Compute the median of y conditioned on x.
Compute the median of y conditioned on x .
"""Compute the median of y conditioned on x.
  
  Input
  -----
    x      -- samples
    y      -- distributed values, possibly at same x samples
    k      -- number of nearest neighbors to condition the median on
              (default 0)
    repeat -- iterates the result of conditional_median a "repeat"
              number of times
              (default 0)
  
  Output
  ------
    x_unique  -- number of unique samples
    y_medians -- conditional median for each unique sample
  
  Comments
  --------
  When k is not zero then the median is conditioned on the 
  k nearest neighbors for each sample value. A non-zero k
  should be used when there are at most one y values specified
  for each sample.
  """ # Find the k nearest neighbors to xx # Compute the median of the y values for the k nearest neighbors
x
y
k
repeat