From f8015b695ad41752699131d55d64a16763a71c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucie=20Proch=C3=A1zkov=C3=A1?= <58247294+LucyAnne98@users.noreply.github.com> Date: Sat, 26 Mar 2022 14:50:03 +0100 Subject: [PATCH 1/5] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1665639..cc2d84a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ Repozitář pro přípravu na zkoušku z NI-VSM na FIT ČVUT. -Za funkční příklady a další díky [@tenhobi](https://github.com/tenhobi) \ No newline at end of file +Forked from [@pokorj54](https://github.com/pokorj54) + +Za funkční příklady a další díky [@tenhobi](https://github.com/tenhobi) From c31d111cb7913d0d9cfeaef039ff2f6870afd9e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ko=C5=99istka?= Date: Sun, 27 Mar 2022 17:55:07 +0200 Subject: [PATCH 2/5] Add requirements --- requirements.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a2887c8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +jupyter +scipy +numpy \ No newline at end of file From b4dd3901f09e9a74761172f875305285ad34fa1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ko=C5=99istka?= Date: Sun, 27 Mar 2022 17:56:08 +0200 Subject: [PATCH 3/5] Add python .gitignore from https://github.com/github/gitignore/blob/main/Python.gitignore --- .gitignore | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/.gitignore b/.gitignore index 62c472e..e22f9df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,145 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook .ipynb_checkpoints +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ From da54a2fd1c63e128cfff9232bf3ebefd4ad84217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ko=C5=99istka?= Date: Wed, 30 Mar 2022 18:10:35 +0200 Subject: [PATCH 4/5] Add joint probability distribution calculations --- JointProbabilityDistribution.py | 107 ++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 JointProbabilityDistribution.py diff --git a/JointProbabilityDistribution.py b/JointProbabilityDistribution.py new file mode 100644 index 0000000..cf3513d --- /dev/null +++ b/JointProbabilityDistribution.py @@ -0,0 +1,107 @@ +import numpy as np +from scipy.stats.contingency import margins + + +def entropy(array): + """ + Calculates the entropy of a discrete random variable + :param array: probabilities of the discrete random variable + :return: Entropy of the discrete random variable described by array + """ + return -1 * np.sum(array * np.log2(array)) + + +def relative_entropy(p, q): + """ + Calculates the Kullback-Leibler distance between two discrete random variables p, q D(p||q) + :param p: discrete random variable + :param q: discrete random variable + :return: Kullback-Leibler distance D(p||q) + """ + logarithms = np.log2(p / q, out=np.zeros_like(p), where=(p != 0)) + return np.sum(np.multiply(p, logarithms)) + + +class JointProbabilityDistribution: + def __init__(self, matrix): + self.matrix = matrix + + @property + def all(self): + marginal_distributions = self.marginal_distributions + conditional_entropy = self.conditional_entropy + return { + 'marginal distribution x': marginal_distributions[0], + 'marginal distribution y': marginal_distributions[1], + 'joined entropy': self.joined_entropy, + 'marginal entropy x': entropy(marginal_distributions[0]), + 'marginal entropy y': entropy(marginal_distributions[1]), + 'conditional entropy x|y': conditional_entropy[0], + 'conditional entropy y|x': conditional_entropy[1], + 'relative entropy x||y': self.relative_entropy('x'), + 'relative entropy y||x': self.relative_entropy('y'), + 'joined distribution': self.matrix + } + + @property + def marginal_distributions(self): + x, y = margins(self.matrix) + return x.T.flatten(), y.flatten() + + @property + def joined_entropy(self): + logarithms = np.log2(self.matrix, out=np.zeros_like(self.matrix), where=(self.matrix != 0)) + products = np.multiply(self.matrix, logarithms) + return -1 * np.sum(products) + + @property + def conditional_entropy(self): + return self.__conditional_entropy(axis=0), self.__conditional_entropy(axis=1) + + def relative_entropy(self, first='x'): + marginal_distributions = self.marginal_distributions + + if first == 'x': + p, q = marginal_distributions + elif first == 'y': + q, p = marginal_distributions + else: + raise ValueError('Permitted values: {x,y}') + return relative_entropy(p, q) + + @property + def mutual_information(self): + x, y = self.marginal_distributions + p = self.matrix + q = np.multiply(x, y) + return relative_entropy(p, q) + + def __conditional_entropy(self, axis=0): + axis_a, axis_b = (0, 1) if axis == 0 else (1, 0) + + p_b = self.marginal_distributions[axis_a] + + result = 0 + + for i in range(self.matrix.shape[axis_a]): + for j in range(self.matrix.shape[axis_b]): + if self.matrix[i, j] == 0: + continue + p_xy = self.matrix[i, j] + p_a_given_b = p_xy / p_b[j] + result += p_xy * np.log2(p_a_given_b) + + return -1 * result + + +if __name__ == '__main__': + matrix = np.matrix([ + [1 / 8, 1 / 16, 1 / 32, 1 / 32], + [1 / 16, 1 / 8, 1 / 32, 1 / 32], + [1 / 16, 1 / 16, 1 / 16, 1 / 16], + [1 / 4, 0, 0, 0], + ]) + jointProbabilityDistribution = JointProbabilityDistribution(matrix) + + for key, value in jointProbabilityDistribution.all.items(): + print(f'{key}: {value}') From 9e29b9f4715ae601e4260c0c179b7674ce89d95c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ko=C5=99istka?= Date: Wed, 30 Mar 2022 18:10:35 +0200 Subject: [PATCH 5/5] Add joint probability distribution calculations --- ...robabilityDistribution.py => joint_probability_distribution.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename JointProbabilityDistribution.py => joint_probability_distribution.py (100%) diff --git a/JointProbabilityDistribution.py b/joint_probability_distribution.py similarity index 100% rename from JointProbabilityDistribution.py rename to joint_probability_distribution.py