Project Name: facebook+prophet
Language: Python
Statement Type: Arithmetic Assignment

Source Code:
def rolling_median_by_h(x, h, w, name):
    """Compute a rolling median of x, after first aggregating by h.

    Right-aligned. Computes a single median for each unique value of h. Each
    median is over at least w samples.

    For each h where there are fewer than w samples, we take samples from the previous h,
    moving backwards. (In other words, we ~ assume that the x's are shuffled within each h.)

    Parameters
    ----------
    x: Array.
    h: Array of horizon for each value in x.
    w: Integer window size (number of elements).
    name: Name for metric in result dataframe

    Returns
    -------
    Dataframe with columns horizon and name, the rolling median of x.
    """
    # Aggregate over h
    df = pd.DataFrame({'x': x, 'h': h})
    grouped = df.groupby('h')
    df2 = grouped.size().reset_index().sort_values('h')
    hs = df2['h']

    res_h = []
    res_x = []
    # Start from the right and work backwards
    i = len(hs) - 1
    while i >= 0:
        h_i = hs[i]
        xs = grouped.get_group(h_i).x.tolist()

        # wrap in array so this works if h is pandas Series with custom index or numpy array
        next_idx_to_add = np.array(h == h_i).argmax() - 1
        while (len(xs) < w) and (next_idx_to_add >= 0):
            # Include points from the previous horizon. All of them if still
            # less than w, otherwise just enough to get to w.
            xs.append(x[next_idx_to_add])
            next_idx_to_add -= 1
        if len(xs) < w:
            # Ran out of points before getting enough.
            break
        res_h.append(hs[i])
        res_x.append(np.median(xs))
        i -= 1
    res_h.reverse()
    res_x.reverse()
    return pd.DataFrame({'horizon': res_h, name: res_x})

rolling_median_by_h(x=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), h=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), w=1, name='x')

Selected Statement: i = len(hs) - 1

Function Input:
x: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
h: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
w: 1
name: 'x'

Variable Values Before Statement:

Value After Statement Execution: 9

Variable States During Runtime (line, value):
x: 1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
h: 1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
w: 1
1
name: 1
'x'
df: 22
   x  h0  0  01  1  12  2  23  3  34  4  45  5  56  6  67  7  78  8  89  9  9
grouped: 23
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7ff75ca12880>
df2: 24
   h  00  0  11  1  12  2  13  3  14  4  15  5  16  6  17  7  18  8  19  9  1
hs: 25
0    01    12    23    34    45    56    67    78    89    9Name: h, dtype: int64
res_h: 27
45
[9]
45
[9, 8]
45
[9, 8, 7]
45
[9, 8, 7, 6]
45
[9, 8, 7, 6, 5]
45
[9, 8, 7, 6, 5, 4]
45
[9, 8, 7, 6, 5, 4, 3]
45
[9, 8, 7, 6, 5, 4, 3, 2]
45
[9, 8, 7, 6, 5, 4, 3, 2, 1]
45
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
48
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
res_x: 28
46
[9.0]
46
[9.0, 8.0]
46
[9.0, 8.0, 7.0]
46
[9.0, 8.0, 7.0, 6.0]
46
[9.0, 8.0, 7.0, 6.0, 5.0]
46
[9.0, 8.0, 7.0, 6.0, 5.0, 4.0]
46
[9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0]
46
[9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0]
46
[9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0]
46
[9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
49
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
i: 30
9
47
8
47
7
47
6
47
5
47
4
47
3
47
2
47
1
47
0
47
-1
h_i: 32
9
32
8
32
7
32
6
32
5
32
4
32
3
32
2
32
1
32
0
xs: 33
[9]
33
[8]
33
[7]
33
[6]
33
[5]
33
[4]
33
[3]
33
[2]
33
[1]
33
[0]
next_idx_to_add: 36
8
36
7
36
6
36
5
36
4
36
3
36
2
36
1
36
0
36
-1