|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Mutual Information Truths\n", |
| 8 | + "\n", |
| 9 | + "## Normal mutual information" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 2, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import numpy as np" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 1, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [ |
| 26 | + { |
| 27 | + "ename": "NameError", |
| 28 | + "evalue": "name 'X' is not defined", |
| 29 | + "output_type": "error", |
| 30 | + "traceback": [ |
| 31 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 32 | + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", |
| 33 | + "\u001b[0;32m<ipython-input-1-8f0676416654>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmeans_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcovariances_\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0mestimate_x_params\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mnormal_entropy_f\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvar\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 34 | + "\u001b[0;31mNameError\u001b[0m: name 'X' is not defined" |
| 35 | + ] |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "from scipy.stats import norm\n", |
| 40 | + "from scipy.stats import multivariate_normal\n", |
| 41 | + "import scipy.integrate as integrate\n", |
| 42 | + "import math\n", |
| 43 | + "from sklearn import mixture\n", |
| 44 | + "\n", |
| 45 | + "def estimate_p(y):\n", |
| 46 | + " return y.count(1)/len(y)\n", |
| 47 | + "\n", |
| 48 | + "def estimate_x_params(X):\n", |
| 49 | + " model = mixture.GaussianMixture(n_components = 2, covariance_type = \"full\")\n", |
| 50 | + " model.fit(X)\n", |
| 51 | + " return model.means_, model.covariances_\n", |
| 52 | + " \n", |
| 53 | + "estimate_x_params(X)\n", |
| 54 | + "\n", |
| 55 | + "def normal_entropy_f(t, mean, var):\n", |
| 56 | + " return -norm.pdf(t, mean, var)*np.log(norm.pdf(t, mean, var))\n", |
| 57 | + "\n", |
| 58 | + "def two_mixture_normals_entropy_f(t, mean_1, mean_2, var_1, var_2):\n", |
| 59 | + " return -.5*norm.pdf(t, mean_1, var_1)*np.log(.5*norm.pdf(t, mean_1, var_1) + .5*norm.pdf(t, mean_2, var_2)) - .5*norm.pdf(t, mean_1, var_1)*np.log(.5*norm.pdf(t, mean_1, var_1) + .5*norm.pdf(t, mean_2, var_2))\n", |
| 60 | + "\n", |
| 61 | + "def normal_entropy(var):\n", |
| 62 | + " return .5*np.log(2*math.pi*math.e*var)\n", |
| 63 | + "\n", |
| 64 | + "#NOTE: this doesn't work for mean = 0\n", |
| 65 | + "def plugin_estimate_cat_1D(X, y):\n", |
| 66 | + " y_param = estimate_p(y)\n", |
| 67 | + " x_params = estimate_x_params(X)\n", |
| 68 | + " h_y = -y_param*np.log(y_param) - (1 - y_param)*np.log(1 - y_param)\n", |
| 69 | + " #h_x_cond_y = integrate.quad(normal_entropy_f, -20, 20, args = (x_params[0][0], x_params[1][0].item()))[0]*.5 + \\\n", |
| 70 | + " #integrate.quad(normal_entropy_f, -20, 20, args = (x_params[0][1], x_params[1][1].item()))[0]*.5\n", |
| 71 | + " h_x_cond_y = normal_entropy(x_params[1][0])*.5 + normal_entropy(x_params[1][1])*.5\n", |
| 72 | + " h_x = integrate.quad(two_mixture_normals_entropy_f, -20, 20, args = (x_params[0][0], x_params[0][1], x_params[1][0].item(), x_params[1][1].item()))[0]\n", |
| 73 | + " cond_entropy = h_y - h_x + h_x_cond_y\n", |
| 74 | + " if cond_entropy < 0:\n", |
| 75 | + " return np.array((0))\n", |
| 76 | + " return cond_entropy\n", |
| 77 | + "\n", |
| 78 | + "def truth_1d(mean):\n", |
| 79 | + " h_y = -.5*np.log(.5) - .5*np.log(.5)\n", |
| 80 | + " h_x_cond_y = normal_entropy(1)*.5 + normal_entropy(1)*.5\n", |
| 81 | + " h_x = integrate.quad(two_mixture_normals_entropy_f, -20, 20, args = (mean, -mean, 1, 1))[0]\n", |
| 82 | + " cond_entropy = h_y - h_x + h_x_cond_y\n", |
| 83 | + " if cond_entropy < 0:\n", |
| 84 | + " return np.array((0))\n", |
| 85 | + " return cond_entropy\n", |
| 86 | + "\n", |
| 87 | + "\n", |
| 88 | + "def mutual_information_truth():\n", |
| 89 | + " entropy = -.5*np.log(.5) - .5*np.log(.5)\n", |
| 90 | + " means = [i*-.2 for i in range(0, 26)]\n", |
| 91 | + " means.reverse()\n", |
| 92 | + " means.extend([i*.2 for i in range(1, 26)])\n", |
| 93 | + " truths = []\n", |
| 94 | + " for elem in means:\n", |
| 95 | + " truths.append(entropy - truth_1d(elem))\n", |
| 96 | + " #return means, cef_all, plugin_all, truth\n", |
| 97 | + " return truths" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "## Different variances" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": null, |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [], |
| 112 | + "source": [ |
| 113 | + "from scipy.stats import norm\n", |
| 114 | + "from scipy.stats import multivariate_normal\n", |
| 115 | + "import scipy.integrate as integrate\n", |
| 116 | + "import math\n", |
| 117 | + "from sklearn import mixture\n", |
| 118 | + "\n", |
| 119 | + "def estimate_p(y):\n", |
| 120 | + " return y.count(1)/len(y)\n", |
| 121 | + "\n", |
| 122 | + "def estimate_x_params(X):\n", |
| 123 | + " model = mixture.GaussianMixture(n_components = 2, covariance_type = \"full\")\n", |
| 124 | + " model.fit(X)\n", |
| 125 | + " return model.means_, model.covariances_\n", |
| 126 | + " \n", |
| 127 | + "def normal_entropy_f(t, mean, var):\n", |
| 128 | + " return -norm.pdf(t, mean, var)*np.log(norm.pdf(t, mean, var))\n", |
| 129 | + "\n", |
| 130 | + "def two_mixture_normals_entropy_f(t, mean_1, mean_2, var_1, var_2):\n", |
| 131 | + " return -.5*norm.pdf(t, mean_1, var_1)*np.log(.5*norm.pdf(t, mean_1, var_1) + .5*norm.pdf(t, mean_2, var_2)) - .5*norm.pdf(t, mean_1, var_1)*np.log(.5*norm.pdf(t, mean_1, var_1) + .5*norm.pdf(t, mean_2, var_2))\n", |
| 132 | + "\n", |
| 133 | + "def normal_entropy(var):\n", |
| 134 | + " return .5*np.log(2*math.pi*math.e*var)\n", |
| 135 | + "\n", |
| 136 | + "#NOTE: this doesn't work for mean = 0\n", |
| 137 | + "def plugin_estimate_cat_1D(X, y):\n", |
| 138 | + " y_param = estimate_p(y)\n", |
| 139 | + " x_params = estimate_x_params(X)\n", |
| 140 | + " h_y = -y_param*np.log(y_param) - (1 - y_param)*np.log(1 - y_param)\n", |
| 141 | + " #h_x_cond_y = integrate.quad(normal_entropy_f, -20, 20, args = (x_params[0][0], x_params[1][0].item()))[0]*.5 + \\\n", |
| 142 | + " #integrate.quad(normal_entropy_f, -20, 20, args = (x_params[0][1], x_params[1][1].item()))[0]*.5\n", |
| 143 | + " h_x_cond_y = normal_entropy(x_params[1][0])*.5 + normal_entropy(x_params[1][1])*.5\n", |
| 144 | + " h_x = integrate.quad(two_mixture_normals_entropy_f, -20, 20, args = (x_params[0][0], x_params[0][1], x_params[1][0].item(), x_params[1][1].item()))[0]\n", |
| 145 | + " cond_entropy = h_y - h_x + h_x_cond_y\n", |
| 146 | + " if cond_entropy < 0:\n", |
| 147 | + " return np.array((0))\n", |
| 148 | + " return cond_entropy\n", |
| 149 | + "\n", |
| 150 | + "def truth_1d(mean):\n", |
| 151 | + " h_y = -.5*np.log(.5) - .5*np.log(.5)\n", |
| 152 | + " h_x_cond_y = normal_entropy(1)*.5 + normal_entropy(3)*.5\n", |
| 153 | + " h_x = integrate.quad(two_mixture_normals_entropy_f, -20, 20, args = (mean, -mean, 1, math.sqrt(3)))[0]\n", |
| 154 | + " cond_entropy = h_y - h_x + h_x_cond_y\n", |
| 155 | + " if cond_entropy < 0:\n", |
| 156 | + " return np.array((0))\n", |
| 157 | + " return cond_entropy\n", |
| 158 | + "\n", |
| 159 | + "\n", |
| 160 | + "def mutual_information_truth():\n", |
| 161 | + " entropy = -.5*np.log(.5) - .5*np.log(.5)\n", |
| 162 | + " means=[i*.2 for i in range(0, 26)]\n", |
| 163 | + " truths = []\n", |
| 164 | + " for elem in means:\n", |
| 165 | + " mi = entropy - truth_1d(elem)\n", |
| 166 | + " if mi < 0:\n", |
| 167 | + " truths.append(0)\n", |
| 168 | + " else:\n", |
| 169 | + " truths.append((entropy - truth_1d(elem))/entropy)\n", |
| 170 | + " #return means, cef_all, plugin_all, truth\n", |
| 171 | + " return truths" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": 28, |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [], |
| 179 | + "source": [ |
| 180 | + "truths_diff_covar = mutual_information_truth()" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "code", |
| 185 | + "execution_count": 29, |
| 186 | + "metadata": {}, |
| 187 | + "outputs": [ |
| 188 | + { |
| 189 | + "name": "stdout", |
| 190 | + "output_type": "stream", |
| 191 | + "text": [ |
| 192 | + "[0, 0, 0, 0, 0, 0.00139387239137524, 0.0923354486255463, 0.17983487386233699, 0.26014481159701247, 0.3311346125243729, 0.39191714540163064, 0.4424935478389682, 0.4834711645708754, 0.5158388832981347, 0.5407855870838623, 0.5595579047838196, 0.5733562865659112, 0.5832668885594593, 0.5902244414373682, 0.5949996896546803, 0.5982044676085582, 0.6003078952265687, 0.6016582208033919, 0.602506180664179, 0.6030271057430278, 0.6033401961717422]\n" |
| 193 | + ] |
| 194 | + } |
| 195 | + ], |
| 196 | + "source": [ |
| 197 | + "print(truths_diff_covar)" |
| 198 | + ] |
| 199 | + }, |
| 200 | + { |
| 201 | + "cell_type": "markdown", |
| 202 | + "metadata": {}, |
| 203 | + "source": [ |
| 204 | + "## Different ps" |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": 19, |
| 210 | + "metadata": {}, |
| 211 | + "outputs": [], |
| 212 | + "source": [ |
| 213 | + "from scipy.stats import norm\n", |
| 214 | + "from scipy.stats import multivariate_normal\n", |
| 215 | + "import scipy.integrate as integrate\n", |
| 216 | + "import math\n", |
| 217 | + "from sklearn import mixture\n", |
| 218 | + "\n", |
| 219 | + "def estimate_p(y):\n", |
| 220 | + " return y.count(1)/len(y)\n", |
| 221 | + "\n", |
| 222 | + "def estimate_x_params(X):\n", |
| 223 | + " model = mixture.GaussianMixture(n_components = 2, covariance_type = \"full\")\n", |
| 224 | + " model.fit(X)\n", |
| 225 | + " return model.means_, model.covariances_\n", |
| 226 | + " \n", |
| 227 | + "\n", |
| 228 | + "def normal_entropy_f(t, mean, var):\n", |
| 229 | + " return -norm.pdf(t, mean, var)*np.log(norm.pdf(t, mean, var))\n", |
| 230 | + "\n", |
| 231 | + "def two_mixture_normals_entropy_f(t, mean_1, mean_2, var_1, var_2, prob):\n", |
| 232 | + " return -prob*norm.pdf(t, mean_1, var_1)*np.log(prob*norm.pdf(t, mean_1, var_1) + (prob)*norm.pdf(t, mean_2, var_2)) - (1-prob)*norm.pdf(t, mean_1, var_1)*np.log((1-prob)*norm.pdf(t, mean_1, var_1) + (1-prob)*norm.pdf(t, mean_2, var_2))\n", |
| 233 | + "\n", |
| 234 | + "def normal_entropy(var):\n", |
| 235 | + " return .5*np.log(2*math.pi*math.e*var)\n", |
| 236 | + "\n", |
| 237 | + "#NOTE: this doesn't work for mean = 0\n", |
| 238 | + "def plugin_estimate_cat_1D(X, y):\n", |
| 239 | + " y_param = estimate_p(y)\n", |
| 240 | + " x_params = estimate_x_params(X)\n", |
| 241 | + " h_y = -y_param*np.log(y_param) - (1 - y_param)*np.log(1 - y_param)\n", |
| 242 | + " #h_x_cond_y = integrate.quad(normal_entropy_f, -20, 20, args = (x_params[0][0], x_params[1][0].item()))[0]*.5 + \\\n", |
| 243 | + " #integrate.quad(normal_entropy_f, -20, 20, args = (x_params[0][1], x_params[1][1].item()))[0]*.5\n", |
| 244 | + " h_x_cond_y = normal_entropy(x_params[1][0])*.5 + normal_entropy(x_params[1][1])*.5\n", |
| 245 | + " h_x = integrate.quad(two_mixture_normals_entropy_f, -20, 20, args = (x_params[0][0], x_params[0][1], x_params[1][0].item(), x_params[1][1].item()))[0]\n", |
| 246 | + " cond_entropy = h_y - h_x + h_x_cond_y\n", |
| 247 | + " if cond_entropy < 0:\n", |
| 248 | + " return np.array((0))\n", |
| 249 | + " return cond_entropy\n", |
| 250 | + "\n", |
| 251 | + "def truth_1d(prob):\n", |
| 252 | + " h_y = -prob*np.log(prob) - (1-prob)*np.log(1-prob)\n", |
| 253 | + " h_x_cond_y = normal_entropy(1)*prob + normal_entropy(1)*(1-prob)\n", |
| 254 | + " h_x = integrate.quad(two_mixture_normals_entropy_f, -20, 20, args = (1, -1, 1, 1, prob))[0]\n", |
| 255 | + " cond_entropy = h_y - h_x + h_x_cond_y\n", |
| 256 | + " if cond_entropy < 0:\n", |
| 257 | + " return np.array((0))\n", |
| 258 | + " return cond_entropy\n", |
| 259 | + "\n", |
| 260 | + "\n", |
| 261 | + "def mutual_information_truth():\n", |
| 262 | + " probs = [i*.02 for i in range(1, 26)]\n", |
| 263 | + " truths = []\n", |
| 264 | + " for elem in probs:\n", |
| 265 | + " entropy = -elem*np.log(elem) - (1-elem)*np.log(1 - elem)\n", |
| 266 | + " truths.append(entropy - truth_1d(elem))\n", |
| 267 | + " #return means, cef_all, plugin_all, truth\n", |
| 268 | + " return truths" |
| 269 | + ] |
| 270 | + }, |
| 271 | + { |
| 272 | + "cell_type": "code", |
| 273 | + "execution_count": 20, |
| 274 | + "metadata": {}, |
| 275 | + "outputs": [], |
| 276 | + "source": [ |
| 277 | + "truths=mutual_information_truth()" |
| 278 | + ] |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "code", |
| 282 | + "execution_count": 22, |
| 283 | + "metadata": {}, |
| 284 | + "outputs": [ |
| 285 | + { |
| 286 | + "data": { |
| 287 | + "text/plain": [ |
| 288 | + "[-0.2582772469333816,\n", |
| 289 | + " -0.18837221247894043,\n", |
| 290 | + " -0.12934883771250916,\n", |
| 291 | + " -0.07754698844452623,\n", |
| 292 | + " -0.031233386821665654,\n", |
| 293 | + " 0.010608631059595974,\n", |
| 294 | + " 0.048647124850824675,\n", |
| 295 | + " 0.08335351918822936,\n", |
| 296 | + " 0.11507724880731518,\n", |
| 297 | + " 0.14408606332507468,\n", |
| 298 | + " 0.17059160121826666,\n", |
| 299 | + " 0.19476356787385918,\n", |
| 300 | + " 0.21674055691830674,\n", |
| 301 | + " 0.2366369572343613,\n", |
| 302 | + " 0.25454794184177987,\n", |
| 303 | + " 0.27055309735931277,\n", |
| 304 | + " 0.28471911766804203,\n", |
| 305 | + " 0.29710183458058814,\n", |
| 306 | + " 0.30774776635099443,\n", |
| 307 | + " 0.3166953067961429,\n", |
| 308 | + " 0.32397563997903966,\n", |
| 309 | + " 0.3296134400392592,\n", |
| 310 | + " 0.3336273982452863,\n", |
| 311 | + " 0.3360306068768478,\n", |
| 312 | + " 0.3368308203468321]" |
| 313 | + ] |
| 314 | + }, |
| 315 | + "execution_count": 22, |
| 316 | + "metadata": {}, |
| 317 | + "output_type": "execute_result" |
| 318 | + } |
| 319 | + ], |
| 320 | + "source": [ |
| 321 | + "[elem for elem in truths]" |
| 322 | + ] |
| 323 | + }, |
| 324 | + { |
| 325 | + "cell_type": "code", |
| 326 | + "execution_count": null, |
| 327 | + "metadata": {}, |
| 328 | + "outputs": [], |
| 329 | + "source": [] |
| 330 | + } |
| 331 | + ], |
| 332 | + "metadata": { |
| 333 | + "kernelspec": { |
| 334 | + "display_name": "Python 3", |
| 335 | + "language": "python", |
| 336 | + "name": "python3" |
| 337 | + }, |
| 338 | + "language_info": { |
| 339 | + "codemirror_mode": { |
| 340 | + "name": "ipython", |
| 341 | + "version": 3 |
| 342 | + }, |
| 343 | + "file_extension": ".py", |
| 344 | + "mimetype": "text/x-python", |
| 345 | + "name": "python", |
| 346 | + "nbconvert_exporter": "python", |
| 347 | + "pygments_lexer": "ipython3", |
| 348 | + "version": "3.7.0" |
| 349 | + } |
| 350 | + }, |
| 351 | + "nbformat": 4, |
| 352 | + "nbformat_minor": 2 |
| 353 | +} |
0 commit comments