File size: 9,034 Bytes
910db9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""Module with generic functions for regression analysis.

These generic functions are reusable parts of the regression computation.

All functions operate on the data / model dictionaries, which are unpacked during the function call
 - this allows more readable function specifications and easier function calls.

All functions return or yield some form of data dictionary, which is then appended to the base
data dictionary.

This allows to extend and modify the computation process as long as all the functions follow
the given argument and return value conventions.

"""

from math import sqrt
import perun.postprocess.regression_analysis.tools as tools


def generic_compute_regression(data_gen, func_list, **model):
    """The core of the computation process.

    Computes the regression model according to the provided sequence of generator ('data_gen')
    and function list (func_list).

    :param iterable data_gen: generator object which provide computation values (sum of x etc.)
    :param list func_list: list of functions which are applied in order to the data from generator
    :param dict model: the regression model dictionary from regression_model (e.g. the 'linear'
        section)
    :raises GenericRegressionExceptionBase: the derived exceptions as used in the generator or
        function list
    :raises TypeError: if the required function arguments are not in the unpacked dictionary input
    :returns iterable: generator object which produces regression model computation steps in a data
        dictionary
    """
    # Get intermediate results from data generator
    for data in data_gen:
        # Update the data with model dictionary
        data.update(model)
        # Apply every function on the model data to compute all needed values
        for func in func_list:
            result = func(**data)
            data.update(result)
        yield data


def generic_regression_data(x_pts, y_pts, f_x, f_y, steps, **_):
    """The generic data generator.

    Produces the sums of x, y, square x, square y and x * y values. Also provides the x min/max
    values and the number of points.

    'f_x' and 'f_y' refer to the x and y values modification for the sums (e.g. log10 for x values
    => sum of log10(x) values).

    The 'steps' allows to split the points sequence into parts (for iterative computation),
    where each part continues the computation (the part contains results from the previous).

    Yielded data dictionary contains 'x_sum', 'y_sum', 'xy_sum', 'x_sq_sum', 'y_sq_sum', 'pts_num',
    'num_sqrt', 'x_start' and 'x_end' keys.

    :param list x_pts: the list of x data points
    :param list y_pts: the list of y data points
    :param function f_x: function object for modification of x values (e.g. log10, **2, etc.) as
        specified by the model formula
    :param function f_y: function object for modification of y values (e.g. log10, **2, etc.) as
        specified by the model formula
    :param int steps: splits the data generation into specified steps
    :raises GenericRegressionExceptionBase: the derived exceptions
    :raises TypeError: if the required function arguments are not in the unpacked dictionary input
    :returns iterable: generator object which produces intermediate results for each computation
        step in a data dictionary
    """
    # We also need the min and max values
    x_min = x_pts[0]
    x_max = x_pts[0]

    # Compute the sums of x, y, x^2, y^2 and x*y
    x_sum, y_sum, x_square_sum, y_square_sum, xy_sum = 0.0, 0.0, 0.0, 0.0, 0.0
    # Split the computation into specified steps
    for part_start, part_end in tools.split_sequence(len(x_pts), steps):
        skipped = 0
        for x_pt, y_pt in zip(x_pts[part_start:part_end], y_pts[part_start:part_end]):

            # Account for possible domain errors with f_x and f_y functions, simply skip the point
            try:
                x_tmp = f_x(x_pt)
                y_tmp = f_y(y_pt)
            except ValueError:
                skipped += 1
                continue

            # Compute the intermediate results
            x_sum += x_tmp
            y_sum += y_tmp
            x_square_sum += x_tmp ** 2
            y_square_sum += y_tmp ** 2
            xy_sum += x_tmp * y_tmp

            # Check the min and max
            x_min, x_max = min(x_min, x_pt), max(x_max, x_pt)

        # Computation step is complete, save the data
        pts_num = part_end - skipped
        data = dict(
            x_sum=x_sum, y_sum=y_sum, xy_sum=xy_sum, x_sq_sum=x_square_sum,
            y_sq_sum=y_square_sum, pts_num=pts_num, num_sqrt=sqrt(pts_num),
            x_start=x_min, x_end=x_max
        )
        yield data


def generic_regression_coefficients(
        f_a, f_b, x_sum, y_sum, xy_sum, x_sq_sum, pts_num, num_sqrt, **_):
    """The generic function for coefficients computation.

    The function uses the general coefficient computation formula, which produces two coefficients
    based on the intermediate results from the data generator.

    The 'f_a' and 'f_b' refer to the coefficients modification function (similar to the 'f_x' and
    'f_y', e.g. 10**x for b0 coefficient => 10**(b0) coefficient value)), which is applied after
    the coefficients are computed.

    Returns the data dictionary with intermediate values 's_xx', 's_xy' and 'coeffs' key containing
    the coefficients list in ascending order.

    The coefficients are computed using formula below:
        b0 = (SUM(y) - b1 * SUM(x)) / n

            for x, y in range <0, n - 1>

        b1 = S_xy / S_xx where

            S_xy = SUM(x * y) - (SUM(x) * SUM(y)) / n
            -> SUM(x * y) - (SUM(x) / sqrt(n)) * (SUM(y) / sqrt(n))
            S_xx = SUM(x^2) - SUM(x)^2 / n
            -> SUM(x^2) - (SUM(x) / sqrt(n))^2

            for x, y in range <0, n - 1>
            the formulas are transformed (->) to avoid computation with extremely big values,
            which can occur in models that use power of x / y values (e.g. power ...)

    The coefficients are further modified using the 'f_a' and 'f_b':
        b0 = f_a(b0)
        b1 = f_b(b1)

        e.g. b1 = log10(b1)

    :param function f_a: function object for modification of b0 coefficient (e.g. 10**x) as
        specified by the model formula
    :param function f_b: function object for modification of b1 coefficient (e.g. 10**x) as
        specified by the model formula
    :param float x_sum: sum of x points values
    :param float y_sum: sum of y points values
    :param float xy_sum: sum of x*y values
    :param float x_sq_sum: sum of x^2 values
    :param int pts_num: number of summed points
    :param float num_sqrt: square root of pts_num
    :raises TypeError: if the required function arguments are not in the unpacked dictionary input
    :returns dict: data dictionary with coefficients and intermediate results
    """
    # Compute the coefficients
    s_xy = xy_sum - tools.safe_division(x_sum, num_sqrt) * tools.safe_division(y_sum, num_sqrt)
    s_xx = x_sq_sum - (tools.safe_division(x_sum, num_sqrt) ** 2)

    b_1 = tools.safe_division(s_xy, s_xx)
    b_0 = tools.safe_division(y_sum - b_1 * x_sum, pts_num)

    # Apply the modification functions on the coefficients and save them
    data = dict(coeffs=[f_a(b_0), f_b(b_1)], s_xy=s_xy, s_xx=s_xx)
    return data


def generic_regression_error(s_xy, s_xx, y_sum, y_sq_sum, num_sqrt, **_):
    """The generic function for error (r^2) computation.

    Returns data dictionary with 'r_square' value representing the model error.

    This function computes the error using the general formula:
        r^2 = rss / tss where

            rss = (S_xy^2) / S_xx
            -> (S_xy / sqrt(S_xx))^2
            tss = SUM(y^2) - SUM(y)^2 / n
            -> SUM(y^2) - ((SUM(y) / sqrt(n)) ** 2)

            for x, y in range <0, n - 1> and S_xy, S_xx from coefficients computation

            the formulas are transformed (->) to avoid computation with extremely big values,
            which can occur in models that use power of x / y values (e.g. quad, power ...)

        RSS equals to the Regression sum of squares, alternatively Explained sum of squares.
        TSS corresponds to the Total sum of squares.

    :param float s_xy: intermediate value from coefficients computation
    :param float s_xx: intermediate value from coefficients computation
    :param float y_sum: sum of y values
    :param float y_sq_sum: sum of y^2 values
    :param float num_sqrt: square root of number of points summed
    :raises TypeError: if the required function arguments are not in the unpacked dictionary input
    :returns dict: data dictionary with error value, tss and rss results
    """
    # Compute the TSS
    tss = y_sq_sum - (tools.safe_division(y_sum, num_sqrt) ** 2)

    # Compute the RSS
    rss = tools.safe_division(s_xy, sqrt(s_xx)) ** 2

    # Compute the r^2
    r_square = tools.safe_division(rss, tss)

    # Save the data
    data = dict(rss=rss, tss=tss, r_square=r_square)

    return data