{"id":28583616,"url":"https://github.com/majianthu/pycopent","last_synced_at":"2025-06-11T05:09:37.891Z","repository":{"id":39251348,"uuid":"251264883","full_name":"majianthu/pycopent","owner":"majianthu","description":"Estimating Copula Entropy (Mutual Information), Transfer Entropy (Conditional Mutual Information), and the statistics for multivariate normality test and two-sample test, and change point detection in Python","archived":false,"fork":false,"pushed_at":"2024-10-02T22:35:47.000Z","size":1485,"stargazers_count":163,"open_issues_count":0,"forks_count":33,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-17T23:47:35.815Z","etag":null,"topics":["causal-discovery","causality","change-detection","change-point-detection","changepoint","conditional-independence-test","conditional-mutual-information","copula","copula-entropy","correlation","entropy","granger-causality","hypothesis-testing","information-theory","mutual-information","normality-test","python","transfer-entropy","two-sample-test","variable-selection"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/copent/","language":"Python","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/majianthu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-30T09:54:32.000Z","updated_at":"2025-04-20T13:50:58.000Z","dependencies_parsed_at":"2024-03-21T04:32:29.991Z","dependency_job_id":"56e7d1a0-0acc-44ee-9059-769c6f747281","html_url":"https://github.com/majianthu/pycopent","commit_stats":{"total_commits":75,"total_committers":3,"mean_commits":25.0,"dds":0.4933333333333333,"last_synced_commit":"bf986909627c94d0c6fdc7a811bd636885b05674"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/majianthu%2Fpycopent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/majianthu%2Fpycopent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/majianthu%2Fpycopent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/majianthu%2Fpycopent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/majianthu","download_url":"https://codeload.github.com/majianthu/pycopent/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/majianthu%2Fpycopent/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259204818,"owners_count":22821162,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["causal-discovery","causality","change-detection","change-point-detection","changepoint","conditional-independence-test","conditional-mutual-information","copula","copula-entropy","correlation","entropy","granger-causality","hypothesis-testing","information-theory","mutual-information","normality-test","python","transfer-entropy","two-sample-test","variable-selection"],"created_at":"2025-06-11T05:09:32.311Z","updated_at":"2025-06-11T05:09:37.862Z","avatar_url":"https://github.com/majianthu.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PyPI version](https://badge.fury.io/py/copent.svg)](https://pypi.org/project/copent)\n# copent\nEstimating Copula Entropy and Transfer Entropy\n\n#### Introduction\nThe nonparametric methods for estimating copula entropy, transfer entropy, and the statistics for multivariate normality test and two-sample test are implemented. The change point detection method based on this two-sample test is also implemented.\n\nThe method for estimating copula entropy composes of two simple steps: estimating empirical copula by rank statistic and estimating copula entropy with the KSG method. Copula Entropy is a mathematical concept for multivariate statistical independence measuring and testing, and proved to be equivalent to mutual information. Different from Pearson Correlation Coefficient, Copula Entropy is defined for non-linear, high-order and multivariate cases, which makes it universally applicable. Estimating copula entropy can be applied to many cases, including but not limited to variable selection and causal discovery (by estimating transfer entropy). Please refer to Ma and Sun (2011) \u003c[doi:10.1016/S1007-0214(11)70008-6](http://www.doi.org/10.1016/S1007-0214(11)70008-6)\u003e for more information.\n\nThe nonparametric method for estimating transfer entropy composes of two steps: estimating three copula entropy and calculating transfer entropy from the estimated copula entropies. A function for conditional independence testing is also provided. Please refer to Ma (2019) \u003c[arXiv:1910.04375](https://arxiv.org/abs/1910.04375)\u003e for more information.\n\nThe copula entropy based statistics for multivariate normality test and two-sample test are implemented. The change point detection method based on this two-sample test is also implemented. Please refer to Ma (2022) \u003c[arXiv:2206.05956](https://arxiv.org/abs/2206.05956)\u003e, Ma (2023) \u003c[arXiv:2307.07247](https://arxiv.org/abs/2307.07247)\u003e, and Ma (2024) \u003c[arXiv:2403.07892](https://arxiv.org/abs/2403.07892)\u003e for more details. \n\n#### Functions\n* copent -- estimating copula entropy;\n* construct_empirical_copula -- the first step of the copent function, which estimates empirical copula for data by rank statistics;\n* entknn -- the second step of the copent function, which estimates copula entropy from empirical copula with kNN method;\n* ci -- conditional independence testing based on copula entropy \n* transent -- estimating transfer entropy via copula entropy\n* mvnt -- estimating the copula entropy-based statistic for multivariate normality test\n* tst -- estimating the copula entropy-based statistic for two-sample test\n* cpd -- single change point detection\n* mcpd -- multiple change point detection\n\n#### Parameters\n* x: N * d data, N samples, d dimensions\n* k: kth nearest neighbour, parameter for kNN entropy estimation. default = 3\n* dtype: distance type, can be 'euclidean' or 'chebychev' (for Maximum Distance)\n* lag: time lag. default = 1\n* s0,s1: two samples with same dimension\n* n: repeat time of estimation\n* thd\t: threshold for the statistic of two-sample test\n* maxp\t: maximal number of change points\n* minseglen : minimal length of binary segmentation\n\n#### Installation\nThe package can be installed from PyPI directly:\n```\npip install copent\n```\nThe package can be installed from Github:\n```\npip install git+https://github.com/majianthu/pycopent.git\n```\n#### Usage Examples\n##### estimating copula entropy \n```python\nfrom numpy.random import multivariate_normal as mnorm\nimport copent\nrho = 0.6\nmean1 = [0,0]\ncov1 = [ [1,rho],[rho,1] ]\nx = mnorm(mean1,cov1,200) # bivariate gaussian \nce1 = copent.copent(x) # estimated copula entropy\n```\n\n##### estimating transfer entropy \n```python\nfrom copent import transent\nfrom pandas import read_csv\nimport numpy as np\nurl = \"https://archive.ics.uci.edu/ml/machine-learning-databases/00381/PRSA_data_2010.1.1-2014.12.31.csv\"\nprsa2010 = read_csv(url)\n# index: 5(PM2.5),6(Dew Point),7(Temperature),8(Pressure),10(Cumulative Wind Speed)\ndata = prsa2010.iloc[2200:2700,[5,8]].values\nte = np.zeros(24)\nfor lag in range(1,25):\n\tte[lag-1] = transent(data[:,0],data[:,1],lag)\n\tstr = \"TE from pressure to PM2.5 at %d hours lag : %f\" %(lag,te[lag-1])\n\tprint(str)\n```\n\n##### multivariate normality test\n```python\nfrom numpy.random import multivariate_normal as mnorm\nfrom copent import mvnt\nmean1 = [0,0]\ncov1 = [[1,0.65],[0.65,1]]\ndata = mnorm(mean1, cov1, 500)\nstat1 = mvnt(data)\n```\n\n##### two-sample test\n```python\nfrom copent import tst\nfrom numpy import zeros\nfrom numpy.random import multivariate_normal as mnorm\nm0 = [0,0]\nrho1 = 0.5\nv0 = [[1,rho1],[rho1,1]]\ns0 = mnorm(m0, v0, 400) # bivariate gaussian \nstat1 = zeros(9)\nfor i in range(0,9):\n\tm1 = [i,i]\n\ts1 = mnorm(m1,v0,500)\n\tstat1[i] = tst(s0,s1)\n\tprint(stat1[i])\n```\n\n##### change point detection\n```python\nfrom copent import mcpd\nimport numpy as np\nfrom numpy.random import multivariate_normal as mnorm\n\nn1 = 50\nx1 = np.random.normal(0,1,n1)\nx2 = np.random.normal(5,1,n1)\nx3 = np.random.normal(10,1,n1)\nx4 = np.random.normal(1,1,n1)\nx = np.concatenate((x1,x2,x3,x4))\npos,maxstat = mcpd(x,thd =0.2)\nprint(pos)\n\nx1 = mnorm([0,0],[[1,0],[0,1]],n1)\nx2 = mnorm([10,10],[[1,0],[0,1]],n1)\nx3 = mnorm([5,5],[[1,0],[0,1]],n1)\nx4 = mnorm([1,1],[[1,0],[0,1]],n1)\nx = np.concatenate((x1,x2,x3,x4))\npos,maxstat = mcpd(x,thd =0.2)\nprint(pos)\n```\n\n#### References\n1. Jian Ma and Zengqi Sun. Mutual information is copula entropy. Tsinghua Science \u0026 Technology, 2011, 16(1): 51-54. See also arXiv preprint arXiv:0808.0845, 2008.\n2. Jian Ma. Estimating Transfer Entropy via Copula Entropy. arXiv preprint arXiv:1910.04375, 2019.\n3. Jian Ma. Multivariate Normality Test with Copula Entropy. arXiv preprint arXiv:2206.05956, 2022.\n4. Jian Ma. Two-Sample Test with Copula Entropy. arXiv preprint arXiv:2307.07247, 2023.\n5. Jian Ma. Change Point Detection with Copula Entropy based Two-Sample Test. arXiv preprint arXiv:2403.07892, 2024.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmajianthu%2Fpycopent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmajianthu%2Fpycopent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmajianthu%2Fpycopent/lists"}