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

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

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

    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 mean of x.
    """
    # Aggregate over h
    df = pd.DataFrame({'x': x, 'h': h})
    df2 = (
        df.groupby('h').agg(['sum', 'count']).reset_index().sort_values('h')
    )
    xs = df2['x']['sum'].values
    ns = df2['x']['count'].values
    hs = df2.h.values

    trailing_i = len(df2) - 1
    x_sum = 0
    n_sum = 0
    # We don't know output size but it is bounded by len(df2)
    res_x = np.empty(len(df2))

    # Start from the right and work backwards
    for i in range(len(df2) - 1, -1, -1):
        x_sum += xs[i]
        n_sum += ns[i]
        while n_sum >= w:
            # Include points from the previous horizon. All of them if still
            # less than w, otherwise weight the mean by the difference
            excess_n = n_sum - w
            excess_x = excess_n * xs[i] / ns[i]
            res_x[trailing_i] = (x_sum - excess_x)/ w
            x_sum -= xs[trailing_i]
            n_sum -= ns[trailing_i]
            trailing_i -= 1

    res_h = hs[(trailing_i + 1):]
    res_x = res_x[(trailing_i + 1):]

    return pd.DataFrame({'horizon': res_h, name: res_x})

rolling_mean_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: trailing_i = len(df2) - 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: 19
   x  h0  0  01  1  12  2  23  3  34  4  45  5  56  6  67  7  78  8  89  9  9
df2: 20
   h   x           sum count0  0   0     11  1   1     12  2   2     13  3   3     14  4   4     15  5   5     16  6   6     17  7   7     18  8   8     19  9   9     1
xs: 23
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
ns: 24
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
hs: 25
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
trailing_i: 27
9
45
8
45
7
45
6
45
5
45
4
45
3
45
2
45
1
45
0
45
-1
x_sum: 28
0
35
9
43
0
35
8
43
0
35
7
43
0
35
6
43
0
35
5
43
0
35
4
43
0
35
3
43
0
35
2
43
0
35
1
43
0
n_sum: 29
0
36
1
44
0
36
1
44
0
36
1
44
0
36
1
44
0
36
1
44
0
36
1
44
0
36
1
44
0
36
1
44
0
36
1
44
0
36
1
44
0
res_x: 31
array([0.0e+000, 4.9e-324, 9.9e-324, 1.5e-323, 2.0e-323, 2.5e-323,       3.0e-323, 3.5e-323, 4.0e-323, 4.4e-323])
42
array([0.0e+000, 4.9e-324, 9.9e-324, 1.5e-323, 2.0e-323, 2.5e-323,       3.0e-323, 3.5e-323, 4.0e-323, 9.0e+000])
42
array([0.0e+000, 4.9e-324, 9.9e-324, 1.5e-323, 2.0e-323, 2.5e-323,       3.0e-323, 3.5e-323, 8.0e+000, 9.0e+000])
42
array([0.0e+000, 4.9e-324, 9.9e-324, 1.5e-323, 2.0e-323, 2.5e-323,       3.0e-323, 7.0e+000, 8.0e+000, 9.0e+000])
42
array([0.0e+000, 4.9e-324, 9.9e-324, 1.5e-323, 2.0e-323, 2.5e-323,       6.0e+000, 7.0e+000, 8.0e+000, 9.0e+000])
42
array([0.0e+000, 4.9e-324, 9.9e-324, 1.5e-323, 2.0e-323, 5.0e+000,       6.0e+000, 7.0e+000, 8.0e+000, 9.0e+000])
42
array([0.0e+000, 4.9e-324, 9.9e-324, 1.5e-323, 4.0e+000, 5.0e+000,       6.0e+000, 7.0e+000, 8.0e+000, 9.0e+000])
42
array([0.e+000, 5.e-324, 1.e-323, 3.e+000, 4.e+000, 5.e+000, 6.e+000,       7.e+000, 8.e+000, 9.e+000])
42
array([0.e+000, 5.e-324, 2.e+000, 3.e+000, 4.e+000, 5.e+000, 6.e+000,       7.e+000, 8.e+000, 9.e+000])
42
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
i: 34
9
34
8
34
7
34
6
34
5
34
4
34
3
34
2
34
1
34
0
excess_n: 40
0
excess_x: 41
0.0
res_h: 47
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])