Kernel underlying the bin-free implementation of conditional abundance matching. For the i^th element of y1, we define a window of length nwin surrounding the point y1[i], and another window surrounding y2[i2_match[i]]. We calculate the rank-order of y1[i] within the first window. Then we find the point in the second window with a matching rank-order and use this value as ynew[i]. The algorithm has been implemented so that the windows are only sorted once at the beginning, and as the windows slide along the arrays with increasing i, elements are popped in and popped out so as to preserve the sorted order.

When using add_subgrid_noise, the algorithm differs slightly. Rather than setting ynew[i] to the value in the second window with the matching rank-order, instead we assign a random uniform number from the range spanned by (y2_window[rank-1],y2_window[rank+1]). This reduces discreteness effects and comes at no loss of precision since the PDF is not known to an accuracy better than 1/nwin.

The arrays named sorted_cdf_values store the y-values in the two windows. The arrays correspondence_indx are responsible for the bookkeeping involved in maintaining a sorted order as elements are popped in and popped out. The way this works is that as the window slides along from left to right, the y value corresponding to the smallest x in the window should be the next one popped out. However, the position of this element within sorted_cdf_values can be anywhere, since there is no strict connection between the values of x and y. So the correspondence_indx array is used to keep track of the x-ordering of the y-values stored in the sorted_cdf_values windows. In particular, the value of the first element in correspondence_indx stores the position of the most-recently added element to sorted_cdf_values (i.e., the element with the largest x); the last element in correspondence_indx, element nwin-1, stores the position of the element of sorted_cdf_values that will be the next one popped out (i.e., the element with the largest x); the middle element of correspondence_indx, element nwin/2, stores the position of the middle of the sorted_cdf_values window. Since the position within sorted_cdf_values is the rank, then sorted_cdf_values[correspondence_indx[nwin/2]] stores the value of ynew[i].