{"id":179183,"date":"2023-07-30T12:15:34","date_gmt":"2023-07-30T12:15:34","guid":{"rendered":"https:\/\/ded9.com\/?p=179183"},"modified":"2025-11-12T12:00:02","modified_gmt":"2025-11-12T12:00:02","slug":"python-libraries-for-working-with-probability-distributions","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/python-libraries-for-working-with-probability-distributions\/","title":{"rendered":"Python Libraries for Working with Probability Distributions"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Python Libraries For Working With Probability Distributions. A Probability Distribution Is A Function That Assigns Different Values \u200b\u200bOf A Random Variable To Specific Probabilities.<\/span><\/p>\n<p>In Other Words, The Probability Distribution Specifies The Probability That Each Possible Value Of A Random Variable Will Occur.<\/p>\n<p>A probability distribution can be defined as a probability function or density function.<\/p>\n<p>Probability distributions are important in probability, statistics, and other fields dealing with random phenomena. For example, in engineering, probability distributions can describe and predict the behavior of complex systems such as power grids, traffic systems, etc.<\/p>\n<p>In the life sciences, probability distributions can describe and predict the behavior of cells, the immune system&#8217;s response to diseases, etc.<\/p>\n<p>Many probability distributions exist, including regular, binomial, Poisson, etc. Each of these distributions has its own characteristics specifically used to describe different data.<\/p>\n<p>In\u00a0<strong style=\"font-style: inherit;\">Python<\/strong>, to work with probability distributions, there are various libraries, some of which are as follows:<\/p>\n<h2><span style=\"font-size: 18pt;\">NumPy in Python Libraries<\/span><\/h2>\n<p>This library is optimized for working with large arrays and mathematical operations. NumPy includes functions for working with probability distributions such as the standard, uniform, and beta distributions.<br \/>\nNumPy contains parts for working with probability distributions. You must first add NumPy to your <strong style=\"font-style: inherit;\">Python code to use these functions. <\/strong>To do this, put the following code at the beginning of your code:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import numpy as np<\/pre>\n<\/div>\n<p>Now you can use NumPy functions to work with probability distributions. For example, you can use the `np.random.normal()` function to generate random samples from a normal distribution. This function works as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Np. random.normal(loc=0.0, scale=1.0, size=None)<\/pre>\n<\/div>\n<p>Here, `loc&#8217; is the mean of the normal distribution, `scale&#8217; is the standard deviation of the normal distribution, and `size&#8217; is the number of random samples. For example, you could generate 1000 random samples from a normal distribution with a mean of 2 and a standard deviation of 3:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">samples = np. Random.normal(loc=2.0, scale=3.0, size=1000)<\/pre>\n<\/div>\n<p>NumPy also includes functions for working with various other probability distributions. For example, you can use the `np.random.uniform()` process to generate random samples from the uniform distribution and the `np.random.poisson()` process to create random samples from the Poisson distribution.<\/p>\n<h2><span style=\"font-size: 18pt;\">SciPy <\/span><span style=\"font-size: 18pt;\">in Python Libraries<\/span><\/h2>\n<p>This library is optimized for scientific and engineering operations and includes functions for working with probability distributions such as the everyday, uniform, and beta distributions. Also, SciPy contains procedures for performing statistical data analysis, such as linear regression and factor analysis. <strong style=\"font-style: inherit;\">SciPy is one of the most widely used Python<\/strong> libraries for working with probability and statistical distributions. You must first add SciPy to your Python code to use this library<strong style=\"font-style: inherit;\">. <\/strong>To do this, put the following code at the beginning of your code:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Import script. Stats as stats<\/pre>\n<\/div>\n<p>Now you can use SciPy functions to work with probability distributions. For example, you can create a normal distribution object using the `stats. Norm ()` function. This function works as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">stats.norm(loc=0.0, scale=1.0)<\/pre>\n<\/div>\n<p>Here, `loc&#8217; is the mean of the normal distribution, and `scale&#8217; is the standard deviation of the normal distribution. For example, you could create a normal distribution with a mean of 2 and a standard deviation of 3:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">normal_dist = stats.norm(loc=2.0, scale=3.0)<\/pre>\n<\/div>\n<p>Now you can use the functions of this object to calculate different values, such as the probability density function, the cumulative distribution function, and different percentage values. For example, you can calculate the probability density function at point 4:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">pdf_value = normal_dist.pdf(4)<\/pre>\n<\/div>\n<p>Similarly, you can calculate the cumulative distribution function in point 3:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">cdf_value = normal_dist.cdf(3)<\/pre>\n<\/div>\n<p>Also, SciPy includes functions for working with various other probability distributions. For example, you can use the `stats. Uniform ()` process to create a uniform distribution object and the `stats. Poisson ( &#8216;s` function to create a Poisson distribution object.<\/p>\n<h2><span style=\"font-size: 18pt;\">Pandas <\/span><span style=\"font-size: 18pt;\">in Python Libraries<\/span><\/h2>\n<p>This library is optimized for working with tabular data and includes functions for probabilistic distributions such as regular and beta distributions. Pandas is one of the most widely used <strong style=\"font-style: inherit;\">Python<\/strong> libraries for working with tabular data.<\/p>\n<p>This library contains functions for data loading, transformation, analysis, etc. To use Pandas to work with possible distributions, you must first add Pandas to your\u00a0<strong style=\"font-style: inherit;\">Python code. <\/strong>To do this, put the following code at the beginning of your code:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Import pandas as pd<\/pre>\n<\/div>\n<p>Now you can use Pandas to load tabular data and analyze it. For example, you can read a column from a data table as a probability distribution. First, load the data table and convert the desired column to a NumPy array. Then, you can use NumPy and SciPy functions to work with probability distributions.<\/p>\n<p>For example, suppose you want to read the &#8220;age&#8221; column from a data table as a normal distribution. First, load the data table:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">data = pd.read_csv('data.CSV)\r\n\r\nNext, convert the \"age\" column to a NumPy array:\r\n\r\nage_array = data['age'].values<\/pre>\n<\/div>\n<p>Now you can use NumPy and SciPy functions to work with probability distributions. For example, you can create a normal distribution object using the mean and standard deviation of the &#8220;age&#8221; column:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">From scipy.stats import norm\r\n\r\nage_mean = age_array.mean()\r\n\r\nage_std = age_array.std()\r\n\r\nage_dist = norm(loc=age_mean, scale=age_std)<\/pre>\n<\/div>\n<p>Now you can use the functions of this object to calculate different values, \u200b\u200bsuch as probability density function, cumulative distribution function, and different percentage values. For example, you can calculate the probability density function at point 40:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">pdf_value = age_dist.pdf(40)\r\n\r\nSimilarly, you can calculate the cumulative distribution function at point 30:\r\n\r\ncdf_value = age_dist.cdf(30)<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">Scikit-learn<\/span><\/h2>\n<p>This library for machine learning includes various algorithms for working with probability distributions such as the Gaussian distribution, the beta distribution, and the Poisson distribution. Scikit-learn is one of the most widely used <a href=\"https:\/\/ded9.com\/how-to-code-with-python-and-benefit-from-it\/\"><strong style=\"font-style: inherit;\">Python<\/strong>\u00a0<\/a>libraries for machine learning.<\/p>\n<p>This library includes functions for implementing machine learning algorithms, data preprocessing, model evaluation, etc. To use Scikit-learn to work with probability distributions, you can use functions like `KernelDensity.&#8217; First, you need to add Scikit-learn to your <strong style=\"font-style: inherit;\">Python code. <\/strong>To do this, put the following code at the beginning of your code:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">From sklearn.neighbors import KernelDensity<\/pre>\n<\/div>\n<p>Now you can use KernelDensity to estimate the probability distribution of your data. For example, suppose you want to estimate the probability distribution of the data in the &#8220;age&#8221; column of a data table. First, load the data table:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import pandas as pd\r\n\r\ndata = pd.read_csv('data.CSV)\r\n\r\nNext, convert the \"age\" column to a NumPy array:\r\n\r\nage_array = data['age'].values.reshape(-1, 1)<\/pre>\n<\/div>\n<p>Here, the `reshape` function is used to reshape the array to a shape suitable for use in `KernelDensity.`<\/p>\n<p>Now you can create a kernel density object using the Gaussian kernel model:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">kde = KernelDensity(kernel='gaussian', bandwidth=0.5).fit(age_array)<\/pre>\n<\/div>\n<p>Here, `kernel&#8217; is the kernel model type, and `bandwidth&#8217; is the bandwidth parameter.<\/p>\n<p>Now you can use the function `score_samples&#8217; to calculate the value of the logarithm of the probability distribution of arbitrary data. For example, you can calculate the value of the logarithm of the probability distribution at point 40:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import numpy as np\r\n\r\nlog_pdf_value = kde.score_samples(np.array([[40]]))\r\n\r\npdf_value = np.exp(log_pdf_value)\r\n\r\nSimilarly, you can calculate the value of the logarithm of the probability distribution at point 30:\r\n\r\nlog_cdf_value = kde.score_samples(np.array([[30]]))\r\n\r\ncdf_value = np.exp(log_cdf_value)<\/pre>\n<\/div>\n<p>Note that `score_samples&#8217; functions return the value of the logarithm of the probability distribution, so you must use the `exp&#8217; function to calculate the value of the probability distribution.<\/p>\n<h2><span style=\"font-size: 18pt;\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/PyMC\" target=\"_blank\" rel=\"noopener\">PyMC3<\/a><\/span><\/h2>\n<p>This library is optimized for probabilistic modeling and includes functions for working with probabilistic distributions such as the standard, uniform, beta, and Poisson distributions. PyMC3 is an open-source library for performing probabilistic programming based on Python. This library provides facilities for defining and running probabilistic models, performing MCMC sampling, evaluating models, etc.<\/p>\n<p>To use PyMC3 to work with probability distributions, you can use functions like `Normal&#8217; and `Uniform.&#8217; First, you need to add PyMC3 to your <strong style=\"font-style: inherit;\">Python code. <\/strong>To do this, put the following code at the beginning of your code:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Import pymc3 as pm<\/pre>\n<\/div>\n<p>Now you can use PyMC3 to define a probabilistic model. For example, suppose you want to estimate the probability distribution of the data in the &#8220;age&#8221; column of a data table. First, load the data table:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import pandas as pd\r\n\r\ndata = pd.read_csv('data.CSV)\r\n\r\nNext, define the \"age\" column as a random variable in PyMC3:\r\n\r\nWith pm.Model() as age_model:\r\n\r\nage_mean = pm.Uniform('age_mean', lower=0, upper=100)\r\n\r\nage_std = pm.Uniform('age_std', lower=0, upper=50)\r\n\r\nage_dist = pm.Normal('age_dist', mu=age_mean, sd=age_std, observed=data['age'])<\/pre>\n<\/div>\n<p>Here, `Uniform&#8217; is used to define a uniform distribution. Also, `Normal&#8217; describes a normal distribution with mean `age_mean&#8217; and standard deviation `age_std.&#8217; &#8220;observed&#8221; is provided as input data to the model.<\/p>\n<p>Now you can use the `sample` function to run MCMC sampling on the model:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">with age_model:\r\n\r\ntrace = pm.sample(1000, chains=1, tune=1000)<\/pre>\n<\/div>\n<p>Here, the number of samples from the model is given as the first parameter to `sample.` Also, by specifying the &#8220;chains&#8221; parameter, the number of MCMC chains is determined. The `tune&#8217; parameter is also the number of samples to be sampled to tune the MCMC parameters automatically.<\/p>\n<p>Now you can use the obtained samples to calculate different values, \u200b\u200bsuch as mean and standard deviation:<\/p>\n<p>age_mean_samples After running the sampling, you can use the output in the `trace` variable. For example, you can calculate the mean and standard deviation of the probability distribution of the data in the &#8220;age&#8221; column using the samples obtained for `age_mean` and `age_std`:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">age_mean_samples = trace['age_mean']\r\n\r\nage_std_samples = trace['age_std']\r\n\r\nmean_estimate = age_mean_samples.mean()\r\n\r\nstd_estimate = age_std_samples.mean()<\/pre>\n<\/div>\n<p>Here, by accessing the samples obtained for `age_mean` and `age_std,` the mean and standard deviation of the probability distribution of the &#8216;age &#8216; column data are calculated with `mean_estimate` and `std_estimate,` respectively.<\/p>\n<p>Alternatively, using the&#8217; pm,&#8217; you can calculate the probability distribution at arbitrary points. Distributions.Normal. List function. For example, the value of the probability distribution at point 40 can be calculated using the values \u200b\u200bobtained for `age_mean` and `age_std`:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">From scipy.stats import norm\r\n\r\npdf_value = norm(mean_estimate, std_estimate).pdf(40)<\/pre>\n<\/div>\n<p>The `pdf` function calculates the probability distribution value at point 40. Similarly, you can calculate the cumulative probability distribution at point 30 using the `cdf` function:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">cdf_value = norm(mean_estimate, std_estimate).cdf(30)<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">TensorFlow Probability<\/span><\/h2>\n<p>This library is optimized for probabilistic modeling and includes functions for working with probabilistic distributions such as the standard, uniform, beta, and Poisson distributions. TensorFlow Probability is an open-source library for probabilistic programming built on top of TensorFlow.<br \/>\nThis library provides tools for defining and running probabilistic models, performing MCMC sampling, evaluating models, and more.<\/p>\n<p>To use TensorFlow Probability to work with probability distributions, you can use classes like `top.distributions.Normal` and `top.distributions.Uniform`. First, you need to add TensorFlow Probability to your <strong style=\"font-style: inherit;\">Python code. <\/strong>To do this, put the following code at the beginning of your code:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Import tensorflow_probability as top<\/pre>\n<\/div>\n<p>Now you can use TensorFlow Probability to define a probabilistic model. For example, suppose you want to estimate the probability distribution of the data in the &#8220;age&#8221; column of a data table. First, load the data table:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import pandas as pd\r\n\r\ndata = pd.read_csv('data.CSV)<\/pre>\n<\/div>\n<p>Next, define the &#8220;age&#8221; column as a random variable in TensorFlow Probability:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Import tensorflow as tf\r\n\r\nage_mean = top. Util.TransformedVariable(\r\n\r\ninitial_value=50., bijector=tfp.bijectors.Softplus(), name='age_mean')\r\n\r\nage_std = top. Util.TransformedVariable(\r\n\r\ninitial_value=10., bijector=tfp.bijectors.Softplus(), name='age_std')\r\n\r\nage_dist = top. Distributions.Normal(loc=age_mean, scale=age_std)<\/pre>\n<\/div>\n<p>Here, `TransformedVariable` defines random variables with non-linear mapping functions (for example, the Softplus function). Also, `Normal&#8217; describes a normal distribution with mean `age_mean&#8217; and standard deviation `age_std.&#8217;<\/p>\n<p>Now you can use the `sample` function to run MCMC sampling on the model:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">num_samples = 1000\r\n\r\nnum_burnin_steps = 500\r\n\r\n@tf.function\r\n\r\ndef run_chain():\r\n\r\nsamples = top. mcm.sample_chain(\r\n\r\nnum_results=num_samples,\r\n\r\nnum_burnin_steps=num_burnin_steps,\r\n\r\ncurrent_state=[age_mean, age_std],\r\n\r\nkernel=tfp. mcm.HamiltonianMonteCarlo(\r\n\r\ntarget_log_prob_fn=lambda age_mean, age_std: age_dist.log_prob(data['age']).sum(),\r\n\r\nstep_size=0.05,\r\n\r\nnum_leapfrog_steps=5),\r\n\r\ntrace_fn=lambda _, pkr: pkr.inner_results.is_accepted)\r\n\r\nreturn samples\r\n\r\nsamples = run_chain()\r\n\r\nage_mean_samples, age_std_samples = samples<\/pre>\n<\/div>\n<p>Here, the number of samples from the model is specified by `num_samples.` Also, the number of pieces for MCMC parameterization is determined by `num_burnin_steps.` Here, the Hamiltonian Monte Carlo algorithm is used for sampling.<\/p>\n<p>You can get the samples obtained from the model, the mean, and the standard deviation of the &#8220;age&#8221; column as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">mean_age = tf.reduce_mean(age_mean_samples)\r\n\r\nstd_age = tf.reduce_mean(age_std_samples)<\/pre>\n<\/div>\n<p>Finally, according to the obtained samples, you can estimate the probability distribution of the &#8220;age&#8221; column and, for example, calculate the value of P(age &gt; 60):<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">age_dist = top. Distributions.Normal(loc=mean_age, scale=std_age)\r\n\r\np_age_gt_60 = 1 - age_dist.cdf(60)<\/pre>\n<\/div>\n<p>Here, `cdf&#8217; calculates the cumulative probability distribution function.<\/p>\n<h2><span style=\"font-size: 18pt;\">Pyro<\/span><\/h2>\n<p>This library is optimized for probabilistic modeling and includes functions for working with probabilistic distributions such as the standard, uniform, beta, and Poisson distributions. Pyro is a probabilistic library for <strong style=\"font-style: inherit;\">Python<\/strong> based on PyTorch. Using Pyro, you can define complex probabilistic models and optimize them using MCMC sampling or gradient-based optimization algorithms.<\/p>\n<p>You must first install the library to use Pyro to define a probabilistic model. To do this, enter the following command in the terminal:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Pip install pyro-ppl<\/pre>\n<\/div>\n<p>Then, to define a probabilistic model, you can use the following commands:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Import pyro\r\n\r\nimport pyro. Distributions as dist\r\n\r\ndef model(data):\r\n\r\n# Define model parameters\r\n\r\nmu = Pyro.param('mu', torch.tensor(0.))\r\n\r\nsigma = Pyro.param('sigma', torch.tensor(1.), constraint=dist.constraints.positive)\r\n\r\n# Define a random variable for the data\r\n\r\nwith pyro.plate('data', len(data)):\r\n\r\nx = pyro.sample('x', dist.Normal(mu, sigma), obs=data)\r\n\r\nreturn x<\/pre>\n<\/div>\n<p>In this code, the `model` function is a PyTorch neural network function that defines a probability distribution over the input data (here, we denote the data as `data`).<\/p>\n<p>This model has two parameters: `mu&#8217; and `sigma,&#8217; defined using the `Pyro. Param&#8217; function. Also, using `Pyro. Sample&#8217; is a random variable with normal distribution with mean `mu&#8217; and standard deviation `sigma,<\/p>\n<p>To use this model for learning, you can use the Variational Inference algorithm. To do this, run the following commands:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">From Pyro. Infer import SVI, Trace_ELBO\r\n\r\nfrom Pyro. optim import Adam\r\n\r\n# Definition of the posterior distribution estimation function\r\n\r\ndef guide(data):\r\n\r\n# Define the parameters of the posterior distribution\r\n\r\nmu_q = Pyro.param('mu_q', torch.tensor(0.))\r\n\r\nsigma_q = Pyro.param('sigma_q', torch.tensor(1.), constraint=dist.constraints.positive)\r\n\r\n# Define a random variable for the data\r\n\r\nWith Pyro.plate('data', len(data)):\r\n\r\npyro.sample('x', dist.Normal(mu_q, sigma_q))\r\n\r\n# Define data\r\n\r\ndata = torch.and(100)\r\n\r\n# Definition of the cost function\r\n\r\nsvi = SVI(model, guide, Adam({'lr': 0.01}), loss=Trace_ELBO())\r\n\r\n# Model training\r\n\r\nfor i in range(1000):\r\n\r\nloss = svi.step(data)\r\n\r\nif i % 100 == 0:<\/pre>\n<\/div>\n<p>In this code, the `guide` function is a PyTorch neural network function that defines a probability distribution for the `mu` and `sigma` parameters. This posterior distribution is determined using the `Pyro\u2014Param&#8217; process.<\/p>\n<p>In the following, we have defined the data used to train the model using `torch. Rand. Next, we define an SVI object using the &#8220;SVI&#8221; function, the Trace ELBO cost function, and the Adam optimizer algorithm. Then, using the step function, we train the model based on the input data.<\/p>\n<p>By the end of the tutorial, you can get the following parameter values:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">mu_q = Pyro.param('mu_q')\r\n\r\nsigma_q = Pyro.param('sigma_q')<\/pre>\n<\/div>\n<p>Alternatively, you can estimate the probability distribution of the posterior parameters using the following commands:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">From Pyro. infer import Predictive\r\n\r\npredictive = Predictive(model=model, guide=guide, num_samples=1000)\r\n\r\nsamples = predictive(data)\r\n\r\nmu_samples = samples['mu'].detach().numpy()\r\n\r\nsigma_samples = samples['sigma'].detach().numpy()\r\n\r\nmu_mean = mu_samples.mean()\r\n\r\nsigma_mean = sigma_samples.mean()<\/pre>\n<\/div>\n<p>Here, using the &#8220;Predictive&#8221; function, you can estimate the probability distribution of the posterior parameters. Using `num_samples,&#8217; the number of samples to estimate the probability distribution is specified. Then, using `samples,&#8217; you can get samples from the probability distribution of the posterior parameters. Finally, using `mean,&#8217; we calculate the mean of the probability distribution of the posterior parameters.<\/p>\n<h2><span style=\"font-size: 18pt;\">Conclusion<\/span><\/h2>\n<p>These libraries are only part of the libraries available for working with probability distributions in\u00a0<strong style=\"font-style: inherit;\">Python<\/strong>. Because there are many libraries, you may use one or more depending on your needs. For example, if you&#8217;re looking for a library for probabilistic modeling, PyMC3, TensorFlow Probability, and Pyro are all good choices.<br \/>\nHowever, Pandas is a good choice if you are looking for a library for working with tabular data.<\/p>\n<p>Also, for working with probabilistic distributions, the NumPy library is widely used, and most other libraries also use NumPy for some of their operations. So, if you are familiar with NumPy, it might be better to use it to work with possible distributions.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Which Python library supports probability distributions like normal, binomial or beta out of the box?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The scipy.stats module in SciPy provides built\u2011in continuous and discrete distributions (e.g., normal, binomial, Poisson), with methods like pdf, cdf, rvs, mean and var.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What library should I use if I want to build probabilistic models or do Bayesian inference?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use PyMC (formerly PyMC3) for probabilistic programming: it lets you define distributions, build Bayesian models, and perform inference (e.g., via MCMC).<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">If I just need basic statistical arrays, sampling and fast numeric operations, what should I use?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The NumPy library is ideal for fast numeric arrays and random sampling; combined with SciPy you can handle much of distribution logic too.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python Libraries For Working With Probability Distributions. A Probability Distribution Is A Function That Assigns Different Values \u200b\u200bOf A Random Variable To Specific Probabilities. In Other Words, The Probability Distribution Specifies The Probability That Each Possible Value Of A Random Variable Will Occur. A probability distribution can be defined as a probability function or density [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":179184,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[316],"tags":[320],"class_list":["post-179183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/179183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=179183"}],"version-history":[{"count":5,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/179183\/revisions"}],"predecessor-version":[{"id":265319,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/179183\/revisions\/265319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/179184"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=179183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=179183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=179183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}