Evaluation of the Approximations

This Module provides the detailes of the evaluation of the approximation from both of the regressions. For the Bayesian logistic regression we use the ClassificationAccuracy class and for the Bayesian linear regression we use the the Wasserstein2Distance class.

ClassificationAccuracy

Calculate the classification accuracy in Bayesian Logistic Regression

Parameters:
  • x_all (list) –

    the input data

  • y_all (list) –

    the output data

  • history_all (list) –

    contains the approximation from all the nodes

  • T (int) –

    the number of iterations

Source code in src/desgld/evaluation.py
 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
class ClassificationAccuracy:
    """Calculate the classification accuracy in Bayesian Logistic Regression

    Args:
        x_all (list): the input data
        y_all (list): the output data
        history_all (list): contains the approximation from all the nodes
        T (int): the number of iterations
    """

    def __init__(self, x_all, y_all, history_all, T):
        self.x_all = x_all
        self.y_all = y_all
        self.history_all = history_all
        self.T = T

    def compute_accuracy(self):
        """Function that computes the classification accuracy

        Args:
            params: the same parameters from the class

        Returns:
        result_acc (float): the classification accuracy
        result_std (float): the standard deviation of the classification
        accuracy
        """
        mis_class = np.empty((self.T + 1, len(self.history_all[0, 0, 0])))

        for t in range(self.T + 1):
            for n in range(len(self.history_all[t, 0, 0])):
                temp0 = 0
                for i in range(len(self.x_all)):
                    z = 1 / (
                        1
                        + np.exp(
                            -np.dot(
                                np.transpose(self.history_all[t, 1])[n],
                                self.x_all[i],
                            )
                        )
                    )
                    if z >= 0.5:
                        z = 1
                    else:
                        z = 0
                    if self.y_all[i] != z:
                        temp0 += 1
                mis_class[t, n] = 1 - temp0 / len(self.x_all)

        result_acc = np.mean(mis_class, axis=1)
        result_std = np.std(mis_class, axis=1)
        return result_acc, result_std

compute_accuracy()

Function that computes the classification accuracy

Parameters:
  • params

    the same parameters from the class

result_acc (float): the classification accuracy result_std (float): the standard deviation of the classification accuracy

Source code in src/desgld/evaluation.py
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
def compute_accuracy(self):
    """Function that computes the classification accuracy

    Args:
        params: the same parameters from the class

    Returns:
    result_acc (float): the classification accuracy
    result_std (float): the standard deviation of the classification
    accuracy
    """
    mis_class = np.empty((self.T + 1, len(self.history_all[0, 0, 0])))

    for t in range(self.T + 1):
        for n in range(len(self.history_all[t, 0, 0])):
            temp0 = 0
            for i in range(len(self.x_all)):
                z = 1 / (
                    1
                    + np.exp(
                        -np.dot(
                            np.transpose(self.history_all[t, 1])[n],
                            self.x_all[i],
                        )
                    )
                )
                if z >= 0.5:
                    z = 1
                else:
                    z = 0
                if self.y_all[i] != z:
                    temp0 += 1
            mis_class[t, n] = 1 - temp0 / len(self.x_all)

    result_acc = np.mean(mis_class, axis=1)
    result_std = np.std(mis_class, axis=1)
    return result_acc, result_std

Wasserstein2Distance

Class for: Wasserstein 2 distance in Bayesian Linear Regression

Parameters:
  • size_w (int) –

    the size of the network

  • T (int) –

    the number of iterations

  • avg_post (list) –

    the mean of the posterior distribution

  • cov_post (list) –

    the covariance of the posterior distribution

  • history_all (list) –

    contains the approximation from all the nodes

  • beta_mean_all (list) –

    contains the mean of the approximation

Source code in src/desgld/evaluation.py
 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
class Wasserstein2Distance:
    """Class for: Wasserstein 2 distance in Bayesian Linear Regression

    Args:
        size_w (int): the size of the network
        T (int): the number of iterations
        avg_post (list): the mean of the posterior distribution
        cov_post (list): the covariance of the posterior distribution
        history_all (list): contains the approximation from all the nodes
        beta_mean_all (list): contains the mean of the approximation
        from all the nodes
    """

    def __init__(
        self, size_w, T, avg_post, cov_post, history_all, beta_mean_all
    ):
        self.size_w = size_w
        self.T = T
        self.avg_post = avg_post
        self.cov_post = cov_post
        self.history_all = history_all
        self.beta_mean_all = beta_mean_all

    def W2_dist(self):
        """Class for: Wasserstein 2 distance in Bayesian Linear Regression

        Args:
            size_w (int): the size of the network
            T (int): the number of iterations
            avg_post (list): the mean of the posterior distribution
            cov_post (list): the covariance of the posterior distribution
            history_all (list): contains the approximation from all the nodes
            beta_mean_all (list): contains the mean of the approximation
            from all the nodes
        Returns:
        w2dis (list): contains the W2 distance of each agent and
        the mean of the approximation from all the ageents
        """
        w2dis = []
        for i in range(self.size_w):
            temp = []
            w2dis.append(temp)
        temp = []
        w2dis.append(temp)
        """
        W2 distance of each agent
        """
        for i in range(self.size_w):
            for t in range(self.T + 1):
                d = 0
                avg_temp = []
                avg_temp.append(np.mean(self.history_all[t][i][0]))
                avg_temp.append(np.mean(self.history_all[t][i][1]))
                avg_temp = np.array(avg_temp)
                cov_temp = np.cov(self.history_all[t][i])
                d = np.linalg.norm(self.avg_post - avg_temp) * np.linalg.norm(
                    self.avg_post - avg_temp
                )
                d = d + np.trace(
                    self.cov_post
                    + cov_temp
                    - 2
                    * sqrtm(
                        np.dot(
                            np.dot(sqrtm(cov_temp), self.cov_post),
                            sqrtm(cov_temp),
                        )
                    )
                )
                w2dis[i].append(np.array(math.sqrt(abs(d))))
        """
        W2 distance of the mean of agents
        """
        for t in range(self.T + 1):
            d = 0
            avg_temp = []
            avg_temp.append(np.mean(self.beta_mean_all[t][0]))
            avg_temp.append(np.mean(self.beta_mean_all[t][1]))
            avg_temp = np.array(avg_temp)
            cov_temp = np.cov(self.beta_mean_all[t])
            d = np.linalg.norm(self.avg_post - avg_temp) * np.linalg.norm(
                self.avg_post - avg_temp
            )
            d = d + np.trace(
                self.cov_post
                + cov_temp
                - 2
                * sqrtm(
                    np.dot(
                        np.dot(sqrtm(cov_temp), self.cov_post), sqrtm(cov_temp)
                    )
                )
            )
            w2dis[self.size_w].append(np.array(math.sqrt(abs(d))))

        for i in range(len(w2dis)):
            w2dis[i] = np.array(w2dis[i])

        return w2dis

W2_dist()

Class for: Wasserstein 2 distance in Bayesian Linear Regression

Parameters:
  • size_w (int) –

    the size of the network

  • T (int) –

    the number of iterations

  • avg_post (list) –

    the mean of the posterior distribution

  • cov_post (list) –

    the covariance of the posterior distribution

  • history_all (list) –

    contains the approximation from all the nodes

  • beta_mean_all (list) –

    contains the mean of the approximation

w2dis (list): contains the W2 distance of each agent and the mean of the approximation from all the ageents

Source code in src/desgld/evaluation.py
 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
def W2_dist(self):
    """Class for: Wasserstein 2 distance in Bayesian Linear Regression

    Args:
        size_w (int): the size of the network
        T (int): the number of iterations
        avg_post (list): the mean of the posterior distribution
        cov_post (list): the covariance of the posterior distribution
        history_all (list): contains the approximation from all the nodes
        beta_mean_all (list): contains the mean of the approximation
        from all the nodes
    Returns:
    w2dis (list): contains the W2 distance of each agent and
    the mean of the approximation from all the ageents
    """
    w2dis = []
    for i in range(self.size_w):
        temp = []
        w2dis.append(temp)
    temp = []
    w2dis.append(temp)
    """
    W2 distance of each agent
    """
    for i in range(self.size_w):
        for t in range(self.T + 1):
            d = 0
            avg_temp = []
            avg_temp.append(np.mean(self.history_all[t][i][0]))
            avg_temp.append(np.mean(self.history_all[t][i][1]))
            avg_temp = np.array(avg_temp)
            cov_temp = np.cov(self.history_all[t][i])
            d = np.linalg.norm(self.avg_post - avg_temp) * np.linalg.norm(
                self.avg_post - avg_temp
            )
            d = d + np.trace(
                self.cov_post
                + cov_temp
                - 2
                * sqrtm(
                    np.dot(
                        np.dot(sqrtm(cov_temp), self.cov_post),
                        sqrtm(cov_temp),
                    )
                )
            )
            w2dis[i].append(np.array(math.sqrt(abs(d))))
    """
    W2 distance of the mean of agents
    """
    for t in range(self.T + 1):
        d = 0
        avg_temp = []
        avg_temp.append(np.mean(self.beta_mean_all[t][0]))
        avg_temp.append(np.mean(self.beta_mean_all[t][1]))
        avg_temp = np.array(avg_temp)
        cov_temp = np.cov(self.beta_mean_all[t])
        d = np.linalg.norm(self.avg_post - avg_temp) * np.linalg.norm(
            self.avg_post - avg_temp
        )
        d = d + np.trace(
            self.cov_post
            + cov_temp
            - 2
            * sqrtm(
                np.dot(
                    np.dot(sqrtm(cov_temp), self.cov_post), sqrtm(cov_temp)
                )
            )
        )
        w2dis[self.size_w].append(np.array(math.sqrt(abs(d))))

    for i in range(len(w2dis)):
        w2dis[i] = np.array(w2dis[i])

    return w2dis