{"id":1822,"date":"2024-03-03T17:40:33","date_gmt":"2024-03-03T17:40:33","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=1822"},"modified":"2024-03-03T17:40:38","modified_gmt":"2024-03-03T17:40:38","slug":"advanced-data-analysis-with-pymc3-and-stan","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/","title":{"rendered":"Advanced data analysis with PyMC3 and Stan"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Welcome to our tutorial on advanced data analysis with PyMC3 and Stan! In this tutorial, we will explore the power of these two probabilistic programming languages for advanced data analysis.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, let&#8217;s start with a brief overview of PyMC3 and Stan. PyMC3 is a Python package for Bayesian statistical modeling and Probabilistic Machine Learning focusing on advanced Markov chain Monte Carlo and variational fitting algorithms. Stan is a platform for statistical modeling and high-performance statistical computation. It provides a flexible and powerful language for specifying complex statistical models, and it uses Hamiltonian Monte Carlo (HMC), a Markov chain Monte Carlo (MCMC) method, to sample from the posterior distribution of the model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, why should you care about advanced data analysis techniques? In today&#8217;s world, data is everywhere, and the ability to analyze and make sense of large and complex datasets is becoming increasingly important. Advanced data analysis techniques, such as those provided by PyMC3 and Stan, allow you to go beyond basic statistical methods and perform more sophisticated analyses. These techniques can help you uncover hidden patterns, make more accurate predictions, and make better decisions based on data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, why use PyMC3 and Stan for advanced data analysis? Both PyMC3 and Stan offer several benefits for advanced data analysis. First, they allow you to specify complex statistical models using a high-level language, making it easier to implement and understand these models. Second, they use advanced MCMC and variational inference algorithms to efficiently sample from the posterior distribution of the model, even for high-dimensional and complex models. Third, they provide a wide range of tools for model checking and criticism, allowing you to evaluate the fit of your models and diagnose any issues.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced Data Analysis with PyMC3<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Model Specification in PyMC3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A statistical model is a mathematical representation of the relationships between variables in a dataset. In PyMC3, we can specify a model using the <code>Model()<\/code> function. This function creates a PyMC3 model object, which we can use to define the model&#8217;s structure and specify the prior distributions for the model&#8217;s parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once we have created a PyMC3 model object, we can start defining the prior distributions for the model&#8217;s parameters. A prior distribution represents our prior knowledge or assumptions about the value of a parameter before seeing the data. In PyMC3, we can specify prior distributions using the <code>pm.Normal<\/code> or <code>pm.HalfNormal<\/code> functions for continuous variables, or the <code>pm.Dirichlet<\/code> function for categorical variables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After specifying the prior distributions, we need to define the model&#8217;s variables. In PyMC3, we can define two types of variables: deterministic and stochastic. Deterministic variables are functions of other variables in the model, and their values are determined by the values of those other variables. Stochastic variables, on the other hand, are variables that have a probability distribution, and their values are sampled from that distribution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to specify a simple linear regression model in PyMC3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> pm.Model() <span class=\"hljs-keyword\">as<\/span> model:\n    <span class=\"hljs-comment\"># Priors<\/span>\n    beta_0 = pm.Normal(<span class=\"hljs-string\">'beta_0'<\/span>, mu=<span class=\"hljs-number\">0<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n    beta_1 = pm.Normal(<span class=\"hljs-string\">'beta_1'<\/span>, mu=<span class=\"hljs-number\">0<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n    sigma = pm.HalfNormal(<span class=\"hljs-string\">'sigma'<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n\n    <span class=\"hljs-comment\"># Deterministic variable<\/span>\n    mu = pm.math.exp(beta_0 + beta_1 * X)\n\n    <span class=\"hljs-comment\"># Stochastic variable<\/span>\n    y_obs = pm.Normal(<span class=\"hljs-string\">'y_obs'<\/span>, mu=mu, sigma=sigma, observed=y)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we define a linear regression model with one predictor variable <code>X<\/code> and one response variable <code>y<\/code>. We specify normal prior distributions for the intercept <code>beta_0<\/code> and slope <code>beta_1<\/code> parameters, and a half-normal prior distribution for the residual standard deviation <code>sigma<\/code>. We then define a deterministic variable <code>mu<\/code> that represents the expected value of <code>y<\/code> given <code>X<\/code>, and a stochastic variable <code>y_obs<\/code> that represents the observed values of <code>y<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Model Fitting in PyMC3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once we have specified our model in PyMC3, the next step is to fit the model to our data. In PyMC3, we can fit the model using MCMC (Markov Chain Monte Carlo) methods. MCMC methods are a class of algorithms used to sample from the posterior distribution of a model&#8217;s parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To fit the model using MCMC methods, we can use the <code>sample()<\/code> function in PyMC3. The <code>sample()<\/code> function takes several arguments, including the number of samples to draw from the posterior distribution, the thinning interval (i.e., the number of samples to skip between each drawn sample), and the number of chains to run.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to use the <code>sample()<\/code> function to fit a PyMC3 model:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> model:\n    trace = pm.sample(<span class=\"hljs-number\">1000<\/span>, tune=<span class=\"hljs-number\">1000<\/span>, chains=<span class=\"hljs-number\">4<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are drawing 1000 samples from the posterior distribution, with a tuning period of 1000 iterations, and running 4 chains in parallel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When fitting a PyMC3 model, it&#8217;s important to monitor convergence and diagnose any convergence issues. Convergence refers to the process of ensuring that the MCMC chains have reached a stationary distribution that approximates the true posterior distribution of the model&#8217;s parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To monitor convergence, we can use several diagnostic tools, such as trace plots, autocorrelation plots, and the Gelman-Rubin statistic. Trace plots show the evolution of the MCMC chains over time, and can help us identify any trends or patterns in the chains. Autocorrelation plots show the correlation between samples drawn from the same chain, and can help us identify any residual autocorrelation in the chains. The Gelman-Rubin statistic compares the variance between chains to the variance within chains, and can help us identify any differences in the chains that may indicate non-convergence.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to generate trace plots and autocorrelation plots for a PyMC3 model:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">pm.plot_trace(trace, var_names=&#91;<span class=\"hljs-string\">'beta_0'<\/span>, <span class=\"hljs-string\">'beta_1'<\/span>, <span class=\"hljs-string\">'sigma'<\/span>])\npm.plot_autocorr(trace, var_names=&#91;<span class=\"hljs-string\">'beta_0'<\/span>, <span class=\"hljs-string\">'beta_1'<\/span>, <span class=\"hljs-string\">'sigma'<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are generating trace plots and autocorrelation plots for the <code>beta_0<\/code>, <code>beta_1<\/code>, and <code>sigma<\/code> parameters in the model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we identify any convergence issues, we may need to adjust the MCMC settings or the model specification. For example, we may need to increase the number of tuning iterations, increase the number of samples, or adjust the prior distributions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to MCMC methods, PyMC3 also supports other inference methods, such as Variational Inference (VI) and No-U-Turn Sampling (NUTS). NUTS is a variant of HMC that automatically adjusts the step size and the number of leapfrog steps, making it more efficient than traditional HMC. VI is a deterministic method that approximates the posterior distribution using a simpler distribution, such as a Gaussian distribution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To use NUTS in PyMC3, we can simply set the <code>step<\/code> argument in the <code>sample()<\/code> function to <code>pm.NUTS()<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> model:\n    trace = pm.sample(<span class=\"hljs-number\">1000<\/span>, tune=<span class=\"hljs-number\">1000<\/span>, chains=<span class=\"hljs-number\">4<\/span>, step=pm.NUTS())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Similarly, to use VI in PyMC3, we can use the <code>fit()<\/code> function with the <code>ADVI()<\/code> method. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> model:\n    approx = pm.fit(n=<span class=\"hljs-number\">10000<\/span>, method=<span class=\"hljs-string\">'advi'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are using ADVI to approximate the posterior distribution using a Gaussian distribution with 10000 samples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Model Checking and Criticism in PyMC3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once we have fit our PyMC3 model to the data, it&#8217;s important to check and criticize the fit of the model. Model checking and criticism involves evaluating the adequacy of the model and identifying any potential issues or limitations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One way to check the fit of a PyMC3 model is to use posterior predictive checks. A posterior predictive check compares the observed data to simulated data generated from the posterior distribution of the model&#8217;s parameters. By comparing the observed data to the simulated data, we can evaluate the goodness-of-fit of the model and identify any discrepancies between the two.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To perform a posterior predictive check in PyMC3, we can use the <code>predict()<\/code> function to generate simulated data from the posterior distribution, and then compare the simulated data to the observed data using a visualization tool such as a histogram or a Q-Q plot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to perform a posterior predictive check in PyMC3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> model:\n    <span class=\"hljs-comment\"># Generate simulated data from the posterior distribution<\/span>\n    ppc = pm.sample_posterior_predictive(trace)\n\n<span class=\"hljs-comment\"># Plot the observed and simulated data<\/span>\npm.plot_posterior_predictive(trace, samples=<span class=\"hljs-number\">1000<\/span>, var_names=&#91;<span class=\"hljs-string\">'y'<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are generating simulated data from the posterior distribution of the <code>y<\/code> variable, and then plotting the observed data and simulated data using the <code>plot_posterior_predictive()<\/code> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another way to check the fit of a PyMC3 model is to check for residual autocorrelation. Residual autocorrelation refers to the correlation between the residuals (i.e., the difference between the observed and predicted values) at different time points. If the residuals are autocorrelated, it may indicate that the model is misspecified or that there is residual structure in the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To check for residual autocorrelation in PyMC3, we can use the <code>autocorr()<\/code> function to calculate the autocorrelation of the residuals at different lags.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to check for residual autocorrelation in PyMC3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> model:\n    <span class=\"hljs-comment\"># Calculate the residuals<\/span>\n    resid = y - pm.math.exp(trace&#91;<span class=\"hljs-string\">'beta_0'<\/span>] + trace&#91;<span class=\"hljs-string\">'beta_1'<\/span>] * X)\n\n    <span class=\"hljs-comment\"># Calculate the autocorrelation of the residuals<\/span>\n    acf = pm.autocorr(resid, lags=<span class=\"hljs-number\">10<\/span>)\n\n    <span class=\"hljs-comment\"># Plot the autocorrelation<\/span>\n    plt.plot(acf)\n    plt.xlabel(<span class=\"hljs-string\">'Lag'<\/span>)\n    plt.ylabel(<span class=\"hljs-string\">'Autocorrelation'<\/span>)\n    plt.show()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are calculating the residuals by subtracting the predicted values from the observed values, and then calculating the autocorrelation of the residuals at lags 1 to 10 using the <code>autocorr()<\/code> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, to compare the fit of different PyMC3 models, we can use leave-one-out cross-validation (LOO-CV) or widely applicable information criterion (WAIC). LOO-CV and WAIC are Bayesian model selection criteria that estimate the out-of-sample predictive accuracy of a model. By comparing the LOO-CV or WAIC values of different models, we can identify the model that best fits the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To calculate the LOO-CV or WAIC values in PyMC3, we can use the <code>az.loo()<\/code> or <code>az.waic()<\/code> functions from the <code>arviz<\/code> library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to calculate the LOO-CV value in PyMC3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> arviz <span class=\"hljs-keyword\">as<\/span> az\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\n<span class=\"hljs-keyword\">with<\/span> model:\n    trace = pm.sample(<span class=\"hljs-number\">1000<\/span>, tune=<span class=\"hljs-number\">1000<\/span>, chains=<span class=\"hljs-number\">4<\/span>)\n\n<span class=\"hljs-comment\"># Calculate the LOO-CV value<\/span>\nloo = az.loo(trace)\n\n<span class=\"hljs-comment\"># Print the LOO-CV value<\/span>\nprint(loo.loo)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are fitting the model using MCMC methods, and then calculating the LOO-CV value using the <code>az.loo()<\/code> function from the <code>arviz<\/code> library.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced PyMC3 Techniques<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we will explore some advanced techniques for using PyMC3, including Hamiltonian Monte Carlo (HMC), the No U-Turn Sampler (NUTS), and Variational Inference.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Implementing Hamiltonian Monte Carlo (HMC)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">HMC is a MCMC algorithm that uses the Hamiltonian dynamics to explore the posterior distribution of a model. HMC uses a combination of gradient information and a proposal distribution to generate samples from the posterior distribution. This allows HMC to explore the posterior distribution more efficiently than traditional MCMC methods, such as the Metropolis-Hastings algorithm.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To implement HMC in PyMC3, we can use the <code>sample()<\/code> function with the <code>hmc<\/code> argument set to <code>True<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> model:\n    trace = pm.sample(<span class=\"hljs-number\">1000<\/span>, tune=<span class=\"hljs-number\">1000<\/span>, chains=<span class=\"hljs-number\">4<\/span>, step=pm.HamiltonianMC())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are drawing 1000 samples from the posterior distribution using HMC, with a tuning period of 1000 iterations, and running 4 chains in parallel.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Using the No U-Turn Sampler (NUTS)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">NUTS is a variant of HMC that automatically adjusts the step size and the number of leapfrog steps based on the geometry of the posterior distribution. This allows NUTS to explore the posterior distribution more efficiently than traditional HMC.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To use NUTS in PyMC3, we can simply set the <code>step<\/code> argument in the <code>sample()<\/code> function to <code>pm.NUTS()<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> model:\n    trace = pm.sample(<span class=\"hljs-number\">1000<\/span>, tune=<span class=\"hljs-number\">1000<\/span>, chains=<span class=\"hljs-number\">4<\/span>, step=pm.NUTS())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are drawing 1000 samples from the posterior distribution using NUTS, with a tuning period of 1000 iterations, and running 4 chains in parallel.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Implementing Variational Inference<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Variational Inference (VI) is a deterministic method that approximates the posterior distribution using a simpler distribution, such as a Gaussian distribution. VI is typically faster than MCMC methods, but may not capture the full complexity of the posterior distribution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To implement VI in PyMC3, we can use the <code>fit()<\/code> function with the <code>method<\/code> argument set to <code>'advi'<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> model:\n    approx = pm.fit(n=<span class=\"hljs-number\">10000<\/span>, method=<span class=\"hljs-string\">'advi'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are approximating the posterior distribution using ADVI with 10000 samples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced Data Analysis with Stan<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Model Specification in Stan<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we will explore how to specify a statistical model using the Stan language. Stan is a probabilistic programming language that allows us to define complex statistical models and perform advanced data analysis.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Defining the Model using the Stan Language<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The Stan language is a high-level language for specifying statistical models. It is similar to other programming languages, such as Python or R, but has some unique features for defining statistical models.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To define a model in Stan, we need to specify the likelihood function, the prior distributions for the model&#8217;s parameters, and any transformed parameters. The likelihood function represents the probability of the data given the model&#8217;s parameters, while the prior distributions represent our prior knowledge or assumptions about the values of the parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to define a simple linear regression model in Stan:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">data {\n  int&lt;lower=<span class=\"hljs-number\">0<\/span>&gt; N; \/\/ number of data points\n  vector&#91;N] x; \/\/ predictor variable\n  vector&#91;N] y; \/\/ response variable\n}\n\nparameters {\n  real beta_0; \/\/ intercept\n  real beta_1; \/\/ slope\n  real&lt;lower=<span class=\"hljs-number\">0<\/span>&gt; sigma; \/\/ residual standard deviation\n}\n\nmodel {\n  y ~ normal(beta_0 + beta_1 * x, sigma);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we define a linear regression model with one predictor variable <code>x<\/code> and one response variable <code>y<\/code>. We specify the likelihood function using the <code>y ~ normal()<\/code> syntax, where <code>y<\/code> is assumed to follow a normal distribution with mean <code>beta_0 + beta_1 * x<\/code> and residual standard deviation <code>sigma<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Specifying Priors for Model Parameters<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In Stan, we can specify prior distributions for the model&#8217;s parameters using the <code>parameters<\/code> block. A prior distribution represents our prior knowledge or assumptions about the value of a parameter before seeing the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the linear regression example, we can specify prior distributions for the intercept <code>beta_0<\/code>, slope <code>beta_1<\/code>, and residual standard deviation <code>sigma<\/code> using the <code>real<\/code> and <code>real&lt;lower=0&gt;<\/code> syntax. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">parameters {\n  real beta_0 ~ normal(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>); \/\/ intercept\n  real beta_1 ~ normal(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>); \/\/ slope\n  real&lt;lower=<span class=\"hljs-number\">0<\/span>&gt; sigma ~ exponential(<span class=\"hljs-number\">1<\/span>); \/\/ residual standard deviation\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are specifying normal prior distributions for the intercept and slope, and an exponential prior distribution for the residual standard deviation.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Defining Transformed Parameters<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In Stan, we can define transformed parameters using the <code>transformed parameters<\/code> block. Transformed parameters are functions of the model&#8217;s parameters, and can be used to simplify the model or improve convergence.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the linear regression example, we can define a transformed parameter for the expected value of <code>y<\/code> using the <code>real<\/code> syntax. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">transformed parameters {\n  real mu;\n  mu = beta_0 + beta_1 * x;\n}\n\nmodel {\n  y ~ normal(mu, sigma);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are defining a transformed parameter <code>mu<\/code> that represents the expected value of <code>y<\/code> given <code>x<\/code>, <code>beta_0<\/code>, and <code>beta_1<\/code>. We can then use <code>mu<\/code> in the likelihood function instead of <code>beta_0 + beta_1 * x<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Model Fitting in Stan<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once we have specified our statistical model in Stan, the next step is to fit the model to the data. In Stan, we can fit the model using MCMC methods, such as HMC or the No-U-Turn Sampler (NUTS).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Using the&nbsp;<code>stan()<\/code>&nbsp;Function to Fit the Model<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To fit a Stan model to the data, we can use the&nbsp;<code>stan()<\/code>&nbsp;function from the&nbsp;<code>rstan<\/code>&nbsp;package in R or the&nbsp;<code>sample()<\/code>&nbsp;function from the&nbsp;<code>cmdstanpy<\/code>&nbsp;package in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to fit a Stan model using the&nbsp;<code>stan()<\/code>&nbsp;function in R:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">library(rstan)\n\n<span class=\"hljs-comment\"># Load the data<\/span>\ndata &lt;- list(N = n, x = x, y = y)\n\n<span class=\"hljs-comment\"># Compile the Stan model<\/span>\nmodel_code &lt;- <span class=\"hljs-string\">\"\ndata {\n  int&lt;lower=0&gt; N;\n  vector&#91;N] x;\n  vector&#91;N] y;\n}\n\nparameters {\n  real beta_0;\n  real beta_1;\n  real&lt;lower=0&gt; sigma;\n}\n\nmodel {\n  y ~ normal(beta_0 + beta_1 * x, sigma);\n}\"<\/span>\n\nmodel &lt;- stan_model(model_code = model_code)\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\nfit &lt;- stan(model_data = data, model_model = model)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\">Specifying the Inference Method<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In Stan, we can specify the inference method using the <code>method<\/code> argument in the <code>stan()<\/code> or <code>sample()<\/code> function. The available inference methods in Stan include MCMC, HMC, and the No-U-Turn Sampler (NUTS).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to fit a Stan model using HMC in R:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">library(rstan)\n\n<span class=\"hljs-comment\"># Load the data<\/span>\ndata &lt;- list(N = n, x = x, y = y)\n\n<span class=\"hljs-comment\"># Compile the Stan model<\/span>\nmodel_code &lt;- <span class=\"hljs-string\">\"\ndata {\n  int&lt;lower=0&gt; N;\n  vector&#91;N] x;\n  vector&#91;N] y;\n}\n\nparameters {\n  real beta_0;\n  real beta_1;\n  real&lt;lower=0&gt; sigma;\n}\n\nmodel {\n  y ~ normal(beta_0 + beta_1 * x, sigma);\n}\n\"<\/span>\nmodel &lt;- stan_model(model_code = model_code)\n\n<span class=\"hljs-comment\"># Fit the model using HMC<\/span>\nfit &lt;- stan(model_data = data, model_model = model, control = list(adapt_delta = <span class=\"hljs-number\">0.95<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are fitting the model using HMC with a target acceptance probability of 0.95.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Monitoring Convergence and Diagnosing Convergence Issues<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When fitting a Stan model, it&#8217;s important to monitor convergence and diagnose any convergence issues. Convergence refers to the process of ensuring that the MCMC chains have reached a stationary distribution that approximates the true posterior distribution of the model&#8217;s parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To monitor convergence in Stan, we can use the <code>n_eff<\/code> and <code>Rhat<\/code> diagnostics. The <code>n_eff<\/code> diagnostic measures the effective sample size of the MCMC chains, while the <code>Rhat<\/code> diagnostic measures the potential scale reduction factor. If the <code>n_eff<\/code> diagnostic is large and the <code>Rhat<\/code> diagnostic is close to 1, it indicates that the MCMC chains have converged.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to monitor convergence in R:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Extract the posterior samples<\/span>\nsamples &lt;- extract(fit)\n\n<span class=\"hljs-comment\"># Check the `n_eff` and `Rhat` diagnostics<\/span>\nprint(summary(samples))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are extracting the posterior samples from the <code>fit<\/code> object, and then checking the <code>n_eff<\/code> and <code>Rhat<\/code> diagnostics using the <code>summary()<\/code> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we identify any convergence issues, we may need to adjust the MCMC settings or the model specification. For example, we may need to increase the number of iterations, adjust the step size or the number of leapfrog steps, or adjust the prior distributions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Model Checking and Criticism in Stan<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once we have fit our Stan model to the data, it&#8217;s important to check and criticize the fit of the model. Model checking and criticism involves evaluating the adequacy of the model and identifying any potential issues or limitations.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Using Posterior Predictive Checks<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">A posterior predictive check compares the observed data to simulated data generated from the posterior distribution of the model&#8217;s parameters. By comparing the observed data to the simulated data, we can evaluate the goodness-of-fit of the model and identify any discrepancies between the two.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To perform a posterior predictive check in Stan, we can use the <code>posterior_predict()<\/code> function to generate simulated data from the posterior distribution, and then compare the simulated data to the observed data using a visualization tool such as a histogram or a Q-Q plot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to perform a posterior predictive check in R:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">library(rstan)\n\n<span class=\"hljs-comment\"># Load the data<\/span>\ndata &lt;- list(N = n, x = x, y = y)\n\n<span class=\"hljs-comment\"># Compile the Stan model<\/span>\nmodel_code &lt;- <span class=\"hljs-string\">\"\ndata {\n  int&lt;lower=0&gt; N;\n  vector&#91;N] x;\n  vector&#91;N] y;\n}\n\nparameters {\n  real beta_0;\n  real beta_1;\n  real&lt;lower=0&gt; sigma;\n}\n\nmodel {\n  y ~ normal(beta_0 + beta_1 * x, sigma);\n}\n\ngenerated quantities {\n  vector&#91;N] y_rep;\n  y_rep = normal_rng(beta_0 + beta_1 * x, sigma);\n}\"<\/span>\nmodel &lt;- stan_model(model_code = model_code)\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\nfit &lt;- stan(model_data = data, model_model = model)\n\n<span class=\"hljs-comment\"># Generate simulated data from the posterior distribution<\/span>\ny_rep &lt;- posterior_predict(fit)\n\n<span class=\"hljs-comment\"># Plot the observed and simulated data<\/span>\npar(mfrow = c(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>))\nhist(y, main = <span class=\"hljs-string\">\"Observed Data\"<\/span>)\nhist(y_rep&#91;<span class=\"hljs-number\">1<\/span>, ], main = <span class=\"hljs-string\">\"Simulated Data\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are generating simulated data from the posterior distribution of the model&#8217;s parameters using the <code>posterior_predict()<\/code> function, and then plotting the observed data and simulated data using the <code>hist()<\/code> function.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Checking for Residual Autocorrelation<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Residual autocorrelation refers to the correlation between the residuals (i.e., the difference between the observed and predicted values) at different time points. If the residuals are autocorrelated, it may indicate that the model is misspecified or that there is residual structure in the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To check for residual autocorrelation in Stan, we can use the <code>residuals()<\/code> function to calculate the residuals, and then use a visualization tool such as an autocorrelation plot to check for autocorrelation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to check for residual autocorrelation in R:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">library(rstan)\n\n<span class=\"hljs-comment\"># Load the data<\/span>\ndata &lt;- list(N = n, x = x, y = y)\n\n<span class=\"hljs-comment\"># Compile the Stan model<\/span>\nmodel_code &lt;- <span class=\"hljs-string\">\"\ndata {\n  int&lt;lower=0&gt; N;\n  vector&#91;N] x;\n  vector&#91;N] y;\n}\n\nparameters {\n  real beta_0;\n  real beta_1;\n  real&lt;lower=0&gt; sigma;\n}\n\nmodel {\n  y ~ normal(beta_0 + beta_1 * x, sigma);\n}\n\"<\/span>\nmodel &lt;- stan_model(model_code = model_code)\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\nfit &lt;- stan(model_data = data, model_model = model)\n\n<span class=\"hljs-comment\"># Calculate the residuals<\/span>\nresid &lt;- residuals(fit)\n\n<span class=\"hljs-comment\"># Plot the residuals<\/span>\npar(mfrow = c(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>))\nacf(resid, main = <span class=\"hljs-string\">\"Residuals\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are calculating the residuals using the <code>residuals()<\/code> function, and then plotting the residuals using the <code>acf()<\/code> function.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Comparing Model Fit using LOO-CV and WAIC<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To compare the fit of different Stan models, we can use leave-one-out cross-validation (LOO-CV) or widely applicable information criterion (WAIC). LOO-CV and WAIC are Bayesian model selection criteria that estimate the out-of-sample predictive accuracy of a model. By comparing the LOO-CV or WAIC values of different models, we can identify the model that best fits the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To calculate the LOO-CV or WAIC values in Stan, we can use the <code>loo()<\/code> or <code>waic()<\/code> functions from the <code>rstan<\/code> package in R or the <code>cmdstanpy<\/code> package in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to calculate the LOO-CV value in R:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">library(rstan)\n\n<span class=\"hljs-comment\"># Load the data<\/span>\ndata &lt;- list(N = n, x = x, y = y)\n\n<span class=\"hljs-comment\"># Compile the Stan model<\/span>\nmodel_code &lt;- <span class=\"hljs-string\">\"\ndata {\n  int&lt;lower=0&gt; N;\n  vector&#91;N] x;\n  vector&#91;N] y;\n}\n\nparameters {\n  real beta_0;\n  real beta_1;\n  real&lt;lower=0&gt; sigma;\n}\n\nmodel {\n  y ~ normal(beta_0 + beta_1 * x, sigma);\n}\n\"<\/span>\nmodel &lt;- stan_model(model_code = model_code)\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\nfit &lt;- stan(model_data = data, model_model = model)\n\n<span class=\"hljs-comment\"># Calculate the LOO-CV value<\/span>\nloo &lt;- loo(fit)\nprint(loo)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are fitting the model using the <code>stan()<\/code> function, and then calculating the LOO-CV value using the <code>loo()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Stan Techniques<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we will explore some advanced techniques for using Stan, including Adaptive Hamiltonian Monte Carlo (HMC), the Stan Math Library, and Variational Inference.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Implementing Adaptive Hamiltonian Monte Carlo (HMC)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Hamiltonian Monte Carlo (HMC) is a MCMC algorithm that uses the Hamiltonian dynamics to explore the posterior distribution of a model. HMC uses a combination of gradient information and a proposal distribution to generate samples from the posterior distribution. This allows HMC to explore the posterior distribution more efficiently than traditional MCMC methods, such as the Metropolis-Hastings algorithm.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Stan, we can implement adaptive HMC using the <code>adapt_engaged<\/code> argument in the <code>stan()<\/code> or <code>sample()<\/code> function. The <code>adapt_engaged<\/code> argument controls the length of the adaptation phase, which is the initial phase of the MCMC algorithm where the step size and the number of leapfrog steps are adjusted.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to implement adaptive HMC in R:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">library(rstan)\n\n<span class=\"hljs-comment\"># Load the data<\/span>\ndata &lt;- list(N = n, x = x, y = y)\n\n<span class=\"hljs-comment\"># Compile the Stan model<\/span>\nmodel_code &lt;- <span class=\"hljs-string\">\"\ndata {\n  int&lt;lower=0&gt; N;\n  vector&#91;N] x;\n  vector&#91;N] y;\n}\n\nparameters {\n  real beta_0;\n  real beta_1;\n  real&lt;lower=0&gt; sigma;\n}\n\nmodel {\n  y ~ normal(beta_0 + beta_1 * x, sigma);\n}\n\"<\/span>\nmodel &lt;- stan_model(model_code = model_code)\n\n<span class=\"hljs-comment\"># Fit the model using adaptive HMC<\/span>\nfit &lt;- stan(model_data = data, model_model = model, control = list(adapt_engaged = TRUE))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are fitting the model using adaptive HMC with an engagement period of 1000 iterations.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Using the Stan Math Library<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The Stan Math Library is a library of mathematical functions that can be used in Stan models. The Stan Math Library includes functions for linear algebra, random number generation, and probability distributions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To use the Stan Math Library in a Stan model, we can include the <code>using namespace stan::math;<\/code> statement at the beginning of the model code. This will allow us to use the functions from the Stan Math Library in the model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to use the Stan Math Library in a Stan model:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">using namespace stan::math;\n\ndata {\n  int&lt;lower=<span class=\"hljs-number\">0<\/span>&gt; N;\n  vector&#91;N] x;\n  vector&#91;N] y;\n}\n\nparameters {\n  real beta_0;\n  real beta_1;\n  real&lt;lower=<span class=\"hljs-number\">0<\/span>&gt; sigma;\n}\n\nmodel {\n  y ~ normal(beta_0 + beta_1 * x, sigma);\n\n  \/\/ Use the log1m() function <span class=\"hljs-keyword\">from<\/span> the Stan Math Library\n  real log_likelihood = log(pdf_normal(y, beta_0 + beta_1 * x, sigma));\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are using the <code>log1m()<\/code> function from the Stan Math Library to calculate the log-likelihood of the data.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Implementing Variational Inference<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Variational Inference (VI) is a deterministic method that approximates the posterior distribution using a simpler distribution, such as a Gaussian distribution. VI is typically faster than MCMC methods, but may not capture the full complexity of the posterior distribution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To implement VI in Stan, we can use the <code>variational()<\/code> function from the <code>rstan<\/code> package in R or the <code>cmdstanpy<\/code> package in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to implement VI in R:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">library(rstan)\n\n<span class=\"hljs-comment\"># Load the data<\/span>\ndata &lt;- list(N = n, x = x, y = y)\n\n<span class=\"hljs-comment\"># Compile the Stan model<\/span>\nmodel_code &lt;- <span class=\"hljs-string\">\"\ndata {\n  int&lt;lower=0&gt; N;\n  vector&#91;N] x;\n  vector&#91;N] y;\n}\n\nparameters {\n  real beta_0;\n  real beta_1;\n  real&lt;lower=0&gt; sigma;\n}\n\nmodel {\n  y ~ normal(beta_0 + beta_1 * x, sigma);\n}\n\"<\/span>\nmodel &lt;- stan_model(model_code = model_code)\n\n<span class=\"hljs-comment\"># Define the variational distribution<\/span>\ndistribution &lt;- variational_distribution(\n  prior = normal(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>),\n  transform = function(x) x\n)\n\n<span class=\"hljs-comment\"># Fit the model using VI<\/span>\nfit &lt;- stan_vi(model_data = data, model_model = model, variational_distribution = distribution)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are fitting the model using VI with a normal prior distribution and the identity transform.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Bayesian Hierarchical Modeling using PyMC3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we will explore a practical example of using PyMC3 for Bayesian hierarchical modeling. Hierarchical modeling is a statistical technique for modeling grouped data, where the parameters of the model are allowed to vary across groups. This allows us to estimate group-level and population-level effects, and to make inferences about the relationships between groups.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Modeling Grouped Data<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we have a dataset of test scores for students in multiple schools. We want to model the test scores as a function of the school and student-level variables. We can use a hierarchical model to estimate the school-level and student-level effects, and to make inferences about the relationships between schools.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to model grouped data in PyMC3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> pymc3 <span class=\"hljs-keyword\">as<\/span> pm\n<span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\n<span class=\"hljs-comment\"># Load the data<\/span>\ndata &lt;- ...\n\n<span class=\"hljs-comment\"># Define the model<\/span>\n<span class=\"hljs-keyword\">with<\/span> pm.Model() <span class=\"hljs-keyword\">as<\/span> model:\n  <span class=\"hljs-comment\"># Priors for the population-level effects<\/span>\n  mu_beta_0 = pm.Normal(<span class=\"hljs-string\">'mu_beta_0'<\/span>, mu=<span class=\"hljs-number\">0<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n  mu_beta_1 = pm.Normal(<span class=\"hljs-string\">'mu_beta_1'<\/span>, mu=<span class=\"hljs-number\">0<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n  tau_sigma = pm.HalfNormal(<span class=\"hljs-string\">'tau_sigma'<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n\n  <span class=\"hljs-comment\"># Priors for the group-level effects<\/span>\n  beta_0 = pm.Normal(<span class=\"hljs-string\">'beta_0'<\/span>, mu=mu_beta_0, sigma=tau_sigma, shape=n_schools)\n  beta_1 = pm.Normal(<span class=\"hljs-string\">'beta_1'<\/span>, mu=mu_beta_1, sigma=tau_sigma, shape=n_schools)\n  sigma = pm.HalfNormal(<span class=\"hljs-string\">'sigma'<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n\n  <span class=\"hljs-comment\"># Model the data<\/span>\n  mu = pm.math.exp(beta_0&#91;school_id] + beta_1&#91;school_id] * student_variable)\n  y = pm.Normal(<span class=\"hljs-string\">'y'<\/span>, mu=mu, sigma=sigma, observed=test_score)\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\ntrace &lt;- pm.sample(<span class=\"hljs-number\">1000<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are defining a hierarchical model with population-level and group-level effects. The population-level effects are the <code>mu_beta_0<\/code> and <code>mu_beta_1<\/code> parameters, which represent the mean intercept and slope for the student-level variable. The group-level effects are the <code>beta_0<\/code> and <code>beta_1<\/code> parameters, which represent the school-level deviations from the population-level effects.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Specifying Group-Level and Population-Level Effects<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To specify the group-level and population-level effects in the model, we can use the <code>shape<\/code> argument in the <code>pm.Normal()<\/code> function. The <code>shape<\/code> argument specifies the number of group-level effects, and the <code>mu<\/code> argument specifies the population-level effects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to specify the group-level and population-level effects in PyMC3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> pm.Model() <span class=\"hljs-keyword\">as<\/span> model:\n  <span class=\"hljs-comment\"># Population-level effects<\/span>\n  mu_beta_0 = pm.Normal(<span class=\"hljs-string\">'mu_beta_0'<\/span>, mu=<span class=\"hljs-number\">0<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n  mu_beta_1 = pm.Normal(<span class=\"hljs-string\">'mu_beta_1'<\/span>, mu=<span class=\"hljs-number\">0<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n  tau_sigma = pm.HalfNormal(<span class=\"hljs-string\">'tau_sigma'<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n\n  <span class=\"hljs-comment\"># Group-level effects<\/span>\n  beta_0 = pm.Normal(<span class=\"hljs-string\">'beta_0'<\/span>, mu=mu_beta_0, sigma=tau_sigma, shape=n_schools)\n  beta_1 = pm.Normal(<span class=\"hljs-string\">'beta_1'<\/span>, mu=mu_beta_1, sigma=tau_sigma, shape=n_schools)\n\n  <span class=\"hljs-comment\"># Model the data<\/span>\n  mu = pm.math.exp(beta_0&#91;school_id] + beta_1&#91;school_id] * student_variable)\n  y = pm.Normal(<span class=\"hljs-string\">'y'<\/span>, mu=mu, sigma=sigma, observed=test_score)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are specifying the population-level effects as the <code>mu_beta_0<\/code> and <code>mu_beta_1<\/code> parameters, and the group-level effects as the <code>beta_0<\/code> and <code>beta_1<\/code> parameters.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Comparing Model Fit using LOO-CV and WAIC<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To compare the fit of different hierarchical models, we can use leave-one-out cross-validation (LOO-CV) or widely applicable information criterion (WAIC). LOO-CV and WAIC are Bayesian model selection criteria that estimate the out-of-sample predictive accuracy of a model. By comparing the LOO-CV or WAIC values of different models, we can identify the model that best fits the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To calculate the LOO-CV or WAIC values in PyMC3, we can use the <code>loo()<\/code> or <code>waic()<\/code> functions from the <code>pymc3.stats<\/code> module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to calculate the LOO-CV value in PyMC3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> pymc3.stats <span class=\"hljs-keyword\">import<\/span> loo\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\ntrace &lt;- pm.sample(<span class=\"hljs-number\">1000<\/span>)\n\n<span class=\"hljs-comment\"># Calculate the LOO-CV value<\/span>\nloo_values &lt;- loo(trace, pointwise_log_prob)\nprint(loo_values)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are fitting the model using the <code>pm.sample()<\/code> function, and then calculating the LOO-CV value using the <code>loo()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Non-linear Mixed Effects Modeling using Stan<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we will explore a practical example of using Stan for non-linear mixed effects modeling. Non-linear mixed effects modeling is a statistical technique for modeling non-linear relationships between variables, where the parameters of the model are allowed to vary across groups. This allows us to estimate group-level and population-level effects, and to make inferences about the relationships between groups.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Modeling Non-Linear Relationships<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we have a dataset of growth curves for children in multiple schools. We want to model the growth curves as a non-linear function of age, where the parameters of the model are allowed to vary across schools. We can use a non-linear mixed effects model to estimate the school-level and population-level effects, and to make inferences about the relationships between schools.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to model non-linear relationships in Stan:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">functions {\n  real log_growth_curve(real age, real beta_0, real beta_1, real beta_2, real sigma) {\n    <span class=\"hljs-keyword\">return<\/span> log(pdf_normal(age, mu=beta_0 + beta_1 * exp(-beta_2 * age), sigma));\n  }\n}\n\ndata {\n  int&lt;lower=<span class=\"hljs-number\">0<\/span>&gt; N;\n  real age&#91;N];\n  real growth_curve&#91;N];\n  int&lt;lower=<span class=\"hljs-number\">0<\/span>&gt; n_schools;\n  int&lt;lower=<span class=\"hljs-number\">1<\/span>&gt; school_id&#91;N];\n}\n\nparameters {\n  real beta_0;\n  real beta_1;\n  real beta_2;\n  real&lt;lower=<span class=\"hljs-number\">0<\/span>&gt; sigma;\n  real beta_0_school&#91;n_schools];\n  real beta_1_school&#91;n_schools];\n  real beta_2_school&#91;n_schools];\n}\n\nmodel {\n  \/\/ Population-level effects\n  beta_0 ~ normal(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>);\n  beta_1 ~ normal(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>);\n  beta_2 ~ normal(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>);\n  sigma ~ exponential(<span class=\"hljs-number\">1<\/span>);\n\n  \/\/ Group-level effects\n  beta_0_school ~ normal(beta_0, sigma);\n  beta_1_school ~ normal(beta_1, sigma);\n  beta_2_school ~ normal(beta_2, sigma);\n\n  \/\/ Model the data\n  <span class=\"hljs-keyword\">for<\/span> (i <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">1<\/span>:N) {\n    target += log_growth_curve(age&#91;i], beta_0_school&#91;school_id&#91;i]], beta_1_school&#91;school_id&#91;i]], beta_2_school&#91;school_id&#91;i]], sigma);\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are defining a non-linear mixed effects model with population-level and group-level effects. The population-level effects are the <code>beta_0<\/code>, <code>beta_1<\/code>, and <code>beta_2<\/code> parameters, which represent the mean growth curve for the population. The group-level effects are the <code>beta_0_school<\/code>, <code>beta_1_school<\/code>, and <code>beta_2_school<\/code> parameters, which represent the school-level deviations from the population-level effects.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Specifying Random Effects<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To specify the random effects in the model, we can use the <code>shape<\/code> argument in the <code>pm.Normal()<\/code> function. The <code>shape<\/code> argument specifies the number of group-level effects, and the <code>mu<\/code> argument specifies the population-level effects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to specify the random effects in Stan:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">functions {\n  real log_growth_curve(real age, real beta_0, real beta_1, real beta_2, real sigma) {\n    <span class=\"hljs-keyword\">return<\/span> log(pdf_normal(age, mu=beta_0 + beta_1 * exp(-beta_2 * age), sigma));\n  }\n}\n\ndata {\n  int&lt;lower=<span class=\"hljs-number\">0<\/span>&gt; N;\n  real age&#91;N];\n  real growth_curve&#91;N];\n  int&lt;lower=<span class=\"hljs-number\">1<\/span>&gt; n_schools;\n  int&lt;lower=<span class=\"hljs-number\">1<\/span>&gt; school_id&#91;N];\n}\n\nparameters {\n  real beta_0;\n  real beta_1;\n  real beta_2;\n  real&lt;lower=<span class=\"hljs-number\">0<\/span>&gt; sigma;\n  real beta_0_school&#91;n_schools];\n  real beta_1_school&#91;n_schools];\n  real beta_2_school&#91;n_schools];\n}\n\nmodel {\n  \/\/ Population-level effects\n  beta_0 ~ normal(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>);\n  beta_1 ~ normal(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>);\n  beta_2 ~ normal(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>);\n  sigma ~ exponential(<span class=\"hljs-number\">1<\/span>);\n\n  \/\/ Group-level effects\n  beta_0_school ~ normal(beta_0, sigma, shape=n_schools);\n  beta_1_school ~ normal(beta_1, sigma, shape=n_schools);\n  beta_2_school ~ normal(beta_2, sigma, shape=n_schools);\n\n  \/\/ Model the data\n  <span class=\"hljs-keyword\">for<\/span> (i <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">1<\/span>:N) {\n    target += log_growth_curve(age&#91;i], beta_0_school&#91;school_id&#91;i]], beta_1_school&#91;school_id&#91;i]], beta_2_school&#91;school_id&#91;i]], sigma);\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are specifying the random effects as the <code>beta_0_school<\/code>, <code>beta_1_school<\/code>, and <code>beta_2_school<\/code> parameters, which represent the school-level deviations from the population-level effects.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Comparing Model Fit using LOO-CV and WAIC<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To compare the fit of different non-linear mixed effects models, we can use leave-one-out cross-validation (LOO-CV) or widely applicable information criterion (WAIC). LOO-CV and WAIC are Bayesian model selection criteria that estimate the out-of-sample predictive accuracy of a model. By comparing the LOO-CV or WAIC values of different models, we can identify the model that best fits the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To calculate the LOO-CV or WAIC values in Stan, we can use the <code>loo()<\/code> or <code>waic()<\/code> functions from the <code>rstan<\/code> package in R or the <code>cmdstanpy<\/code> package in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to calculate the LOO-CV value in R:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">library(rstan)\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\nfit &lt;- stan(model_code = model_code, data = data)\n\n<span class=\"hljs-comment\"># Calculate the LOO-CV value<\/span>\nloo_values &lt;- loo(fit)\nprint(loo_values)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are fitting the model using the <code>stan()<\/code> function, and then calculating the LOO-CV value using the <code>loo()<\/code> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By using these techniques, we can perform non-linear mixed effects modeling using Stan. This will allow us to make inferences about group-level and population-level effects, and to compare the fit of different non-linear mixed effects models.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Time Series Analysis using PyMC3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we will explore a practical example of using PyMC3 for time series analysis. Time series analysis is a statistical technique for modeling time-dependent data, where the parameters of the model are allowed to vary over time. This allows us to estimate time-varying effects, and to make predictions about future values of the time series.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Modeling Time Series Data<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we have a dataset of monthly sales for a retail store. We want to model the sales as a function of time, where the parameters of the model are allowed to vary over time. We can use a time series model to estimate time-varying effects, and to make predictions about future values of the sales.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to model time series data in PyMC3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> pymc3 <span class=\"hljs-keyword\">as<\/span> pm\n<span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\n<span class=\"hljs-comment\"># Load the data<\/span>\ndata &lt;- ...\n\n<span class=\"hljs-comment\"># Define the model<\/span>\n<span class=\"hljs-keyword\">with<\/span> pm.Model() <span class=\"hljs-keyword\">as<\/span> model:\n  <span class=\"hljs-comment\"># Priors for the population-level effects<\/span>\n  mu_beta_0 = pm.Normal(<span class=\"hljs-string\">'mu_beta_0'<\/span>, mu=<span class=\"hljs-number\">0<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n  mu_beta_1 = pm.Normal(<span class=\"hljs-string\">'mu_beta_1'<\/span>, mu=<span class=\"hljs-number\">0<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n  tau_sigma = pm.HalfNormal(<span class=\"hljs-string\">'tau_sigma'<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n\n  <span class=\"hljs-comment\"># Priors for the time-varying effects<\/span>\n  beta_0 = pm.GaussianRandomWalk(<span class=\"hljs-string\">'beta_0'<\/span>, tau=tau_sigma, shape=n_months)\n  beta_1 = pm.GaussianRandomWalk(<span class=\"hljs-string\">'beta_1'<\/span>, tau=tau_sigma, shape=n_months)\n  sigma = pm.HalfNormal(<span class=\"hljs-string\">'sigma'<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n\n  <span class=\"hljs-comment\"># Model the data<\/span>\n  mu = pm.math.exp(beta_0&#91;month_id] + beta_1&#91;month_id] * time_variable)\n  y = pm.Normal(<span class=\"hljs-string\">'y'<\/span>, mu=mu, sigma=sigma, observed=sales)\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\ntrace &lt;- pm.sample(<span class=\"hljs-number\">1000<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are defining a time series model with population-level and time-varying effects. The population-level effects are the <code>mu_beta_0<\/code> and <code>mu_beta_1<\/code> parameters, which represent the mean intercept and slope for the time series. The time-varying effects are the <code>beta_0<\/code> and <code>beta_1<\/code> parameters, which represent the time-dependent deviations from the population-level effects.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Implementing State-Space Models<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To implement state-space models in PyMC3, we can use the <code>pm.StateSpace<\/code> class. The <code>pm.StateSpace<\/code> class allows us to define a state-space model, which is a type of time series model where the parameters of the model are allowed to vary over time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to implement a state-space model in PyMC3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">with pm.Model() <span class=\"hljs-keyword\">as<\/span> model:\n  <span class=\"hljs-comment\"># Population-level effects<\/span>\n  mu_beta_0 = pm.Normal(<span class=\"hljs-string\">'mu_beta_0'<\/span>, mu=<span class=\"hljs-number\">0<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n  mu_beta_1 = pm.Normal(<span class=\"hljs-string\">'mu_beta_1'<\/span>, mu=<span class=\"hljs-number\">0<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n  tau_sigma = pm.HalfNormal(<span class=\"hljs-string\">'tau_sigma'<\/span>, sigma=<span class=\"hljs-number\">10<\/span>)\n\n  <span class=\"hljs-comment\"># State-space model<\/span>\n  state_space = pm.StateSpace(<span class=\"hljs-string\">'state_space'<\/span>, target=y,\n                              mu=pm.math.exp(mu_beta_0 + mu_beta_1 * time_variable),\n                              <span class=\"hljs-keyword\">var<\/span>=sigma**<span class=\"hljs-number\">2<\/span>,\n                              sigma=sigma,\n                              shape=n_months)\n\n  <span class=\"hljs-comment\"># Model the data<\/span>\n  state_space.observe(time_variable, sales)\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\ntrace &lt;- pm.sample(<span class=\"hljs-number\">1000<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are defining a state-space model with population-level effects and a state-space model for the time series. The state-space model allows us to estimate time-varying effects, and to make predictions about future values of the time series.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Comparing Model Fit using LOO-CV and WAIC<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To compare the fit of different time series models, we can use leave-one-out cross-validation (LOO-CV) or widely applicable information criterion (WAIC). LOO-CV and WAIC are Bayesian model selection criteria that estimate the out-of-sample predictive accuracy of a model. By comparing the LOO-CV or WAIC values of different models, we can identify the model that best fits the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To calculate the LOO-CV or WAIC values in PyMC3, we can use the <code>loo()<\/code> or <code>waic()<\/code> functions from the <code>pymc3.stats<\/code> module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to calculate the LOO-CV value in PyMC3:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">from pymc3.stats import loo\n\n<span class=\"hljs-comment\"># Fit the model<\/span>\ntrace &lt;- pm.sample(<span class=\"hljs-number\">1000<\/span>)\n\n<span class=\"hljs-comment\"># Calculate the LOO-CV value<\/span>\nloo_values &lt;- loo(trace, pointwise_log_prob)\n<span class=\"hljs-keyword\">print<\/span>(loo_values)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we are fitting the model using the <code>pm.sample()<\/code> function, and then calculating the LOO-CV value using the <code>loo()<\/code> function.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">This tutorial provided a comprehensive overview of advanced data analysis with PyMC3 and Stan, including Bayesian inference, model specification and fitting, model checking and criticism, advanced techniques, and practical examples. Key takeaways include the importance of model checking and criticism, the benefits of advanced techniques such as Hamiltonian Monte Carlo and Variational Inference, and the wide range of data analysis tasks that can be accomplished using PyMC3 and Stan. Future directions for advanced data analysis with PyMC3 and Stan include implementing advanced statistical models, developing custom probability distributions and inference methods, and applying these tools to real-world problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Welcome to our tutorial on advanced data analysis with PyMC3 and Stan! In this tutorial, we will explore the power of these two probabilistic programming languages for advanced data analysis. First, let&#8217;s start with a brief overview of PyMC3 and Stan. PyMC3 is a Python package for Bayesian statistical modeling and Probabilistic Machine Learning [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-1822","post","type-post","status-publish","format-standard","category-programming-languages","category-python","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Advanced data analysis with PyMC3 and Stan<\/title>\n<meta name=\"description\" content=\"In this tutorial, we will explore the power of PyMC3 and Stan, two probabilistic programming languages for advanced data analysis\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Advanced data analysis with PyMC3 and Stan\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we will explore the power of PyMC3 and Stan, two probabilistic programming languages for advanced data analysis\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-03T17:40:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-03T17:40:38+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"20 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-data-analysis-with-pymc3-and-stan\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-data-analysis-with-pymc3-and-stan\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"Advanced data analysis with PyMC3 and Stan\",\"datePublished\":\"2024-03-03T17:40:33+00:00\",\"dateModified\":\"2024-03-03T17:40:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-data-analysis-with-pymc3-and-stan\\\/\"},\"wordCount\":4512,\"articleSection\":[\"Programming Languages\",\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-data-analysis-with-pymc3-and-stan\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-data-analysis-with-pymc3-and-stan\\\/\",\"name\":\"Advanced data analysis with PyMC3 and Stan\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2024-03-03T17:40:33+00:00\",\"dateModified\":\"2024-03-03T17:40:38+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"In this tutorial, we will explore the power of PyMC3 and Stan, two probabilistic programming languages for advanced data analysis\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-data-analysis-with-pymc3-and-stan\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-data-analysis-with-pymc3-and-stan\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-data-analysis-with-pymc3-and-stan\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Advanced data analysis with PyMC3 and Stan\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Advanced data analysis with PyMC3 and Stan","description":"In this tutorial, we will explore the power of PyMC3 and Stan, two probabilistic programming languages for advanced data analysis","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/","og_locale":"en_US","og_type":"article","og_title":"Advanced data analysis with PyMC3 and Stan","og_description":"In this tutorial, we will explore the power of PyMC3 and Stan, two probabilistic programming languages for advanced data analysis","og_url":"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/","article_published_time":"2024-03-03T17:40:33+00:00","article_modified_time":"2024-03-03T17:40:38+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"20 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"Advanced data analysis with PyMC3 and Stan","datePublished":"2024-03-03T17:40:33+00:00","dateModified":"2024-03-03T17:40:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/"},"wordCount":4512,"articleSection":["Programming Languages","Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/","url":"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/","name":"Advanced data analysis with PyMC3 and Stan","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2024-03-03T17:40:33+00:00","dateModified":"2024-03-03T17:40:38+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"In this tutorial, we will explore the power of PyMC3 and Stan, two probabilistic programming languages for advanced data analysis","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/advanced-data-analysis-with-pymc3-and-stan\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"Advanced data analysis with PyMC3 and Stan"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=1822"}],"version-history":[{"count":6,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1822\/revisions"}],"predecessor-version":[{"id":1828,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1822\/revisions\/1828"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=1822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=1822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=1822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}