{"id":9220,"date":"2020-08-05T07:53:33","date_gmt":"2020-08-05T07:53:33","guid":{"rendered":"https:\/\/www.experfy.com\/blog\/?p=9220"},"modified":"2023-11-22T12:08:16","modified_gmt":"2023-11-22T12:08:16","slug":"intro-to-probabilistic-programming","status":"publish","type":"post","link":"https:\/\/www.experfy.com\/blog\/bigdata-cloud\/intro-to-probabilistic-programming\/","title":{"rendered":"Intro to probabilistic programming"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"9220\" class=\"elementor elementor-9220\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"has_eae_slider elementor-section elementor-top-section elementor-element elementor-element-4dc0375c elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"4dc0375c\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"has_eae_slider elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-89dacad\" data-id=\"89dacad\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-33a924fc elementor-widget elementor-widget-text-editor\" data-id=\"33a924fc\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\n<h1 class=\"wp-block-heading\" id=\"3108\">What is Probabilistic Programming?<\/h1>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f21bc65 elementor-widget elementor-widget-text-editor\" data-id=\"f21bc65\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>The idea behind Probabilistic programming to bring the inference algorithms and theory from statistics combined with formal semantics, compilers, and other tools from programming languages to build efficient inference evaluators for models and applications from Machine Learning. In other words, probabilistic programming is\u00a0<em>a tool for statistical modeling<\/em>. The idea is to borrow lessons from the world of programming languages and apply them to the problems of designing and using statistical models.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\">\n<p><em>Probabilistic programming is about doing statistics using the tools of computer science.<\/em><\/p>\n<\/blockquote>\n<!-- \/wp:quote -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-473aaff elementor-widget elementor-widget-image\" data-id=\"473aaff\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/601\/0*0OuE4hivq_5WcbkF.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ee2d36c elementor-widget elementor-widget-text-editor\" data-id=\"ee2d36c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>In the above figure you can see a typical computer science programming pipeline: Write a program, specify the values of its arguments then evaluate the program to produce an output. The right-hand side illustrates the approach taken to modeling in statistics: Start with the output, the observations or data Y, then specify an abstract generative model p(X,Y), often denoted mathematically, and finally use algebra and inference techniques to characterize the posterior distribution, p(X | Y ), of the unknown quantities in the model given the observed quantities. Whereas in Probabilistic programming: a programming language for model definitions and statistical inference algorithms for computing the conditional distribution of the program inputs that could have given rise to the observed program output.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\">\n<p><em>Note: Probabilistic programming is not about writing software that behaves probabilistically.<\/em><\/p>\n<\/blockquote>\n<!-- \/wp:quote -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5ba8639 elementor-widget elementor-widget-text-editor\" data-id=\"5ba8639\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>To implement such Ensemble Architecture Tensorflow introduces Tensorflow-probability.<\/p>\n<!-- \/wp:paragraph -->\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d4ea2ec elementor-widget elementor-widget-heading\" data-id=\"d4ea2ec\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><h1 id=\"553d\">Tensorflow-Probability<\/h1><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8f2b680 elementor-widget elementor-widget-text-editor\" data-id=\"8f2b680\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>TensorFlow Probability is a library for probabilistic reasoning and statistical analysis in TensorFlow. As part of the TensorFlow ecosystem, TensorFlow Probability provides integration of probabilistic methods with deep networks, gradient-based inference via automatic differentiation, and scalability to large datasets and models via hardware acceleration (e.g., GPUs) and distributed computation.<br \/>Probabilistic Machine Learning tools in TensorFlow-probability are structured in different levels. In this blog, we\u2019ll discuss\u00a0<em>Statistical building blocks<\/em>\u00a0and\u00a0<em>Model Building\u00a0<\/em>using TensorFlow-probability.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Let\u2019s start with importing necessary modules:<\/p>\n<!-- \/wp:paragraph -->\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b36105d elementor-widget elementor-widget-image\" data-id=\"b36105d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2020\/08\/Untitled-1-2.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-745f687 elementor-widget elementor-widget-heading\" data-id=\"745f687\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><!-- wp:heading {\"level\":1} -->\n<h1 id=\"8826\">Statistical Functions in Tensorflow-probability<\/h1>\n<!-- \/wp:heading --><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-43eba4a elementor-widget elementor-widget-heading\" data-id=\"43eba4a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><!-- wp:heading {\"level\":1} -->\n<h1 id=\"07cc\">Distributions-<\/h1>\n<!-- \/wp:heading --><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-17722b9 elementor-widget elementor-widget-text-editor\" data-id=\"17722b9\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>A\u00a0<code><a href=\"https:\/\/www.tensorflow.org\/probability\/api_docs\/python\/tfp\/distributions\/Distribution\" target=\"_blank\" rel=\"noreferrer noopener\">tfp.distributions.Distribution<\/a><\/code>\u00a0is a class with two core methods:\u00a0<code>sample<\/code>\u00a0and\u00a0<code>log_prob<\/code>. This class contains many distributions which can be seen by writing:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">print_subclasses_from_module(tfp.distributions, tfp.distributions.Distribution)<\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-dd6b5ff elementor-widget elementor-widget-text-editor\" data-id=\"dd6b5ff\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>Let\u2019s see how to sample a Normal Distribution which is a good starter in stat101 using tf-probability:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"># A standard normal<br \/>normal = tfd.Normal(loc=0., scale=1.) # mean=0, std=3samples = normal.sample(1000)<br \/>sns.distplot(samples)<br \/>plt.title(\"Samples from a standard Normal\")<br \/>plt.show()'''<br \/>log of the probability density\/mass function evaluated at the given sample value.<br \/>'''<br \/>print(\"log(PDF):\",normal.log_prob(0.))<\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-abf5cfc elementor-widget elementor-widget-text-editor\" data-id=\"abf5cfc\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>Now in order to calculate other statistical parameters like cumulative distribution function and multiple distributions we can still utilize tf-probabilities native classes.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"># Define a single scalar Normal distribution.<br \/>dist = tfd.Normal(loc=0., scale=3.)  # mean=0, std=3# Evaluate the cdf at 1, returning a scalar.<br \/>dist.cdf(1.)# Define a batch of two scalar valued Normals.<br \/># The first has mean 1 and standard deviation 11, the second 2 and 22.<br \/>dist = tfd.Normal(loc=[1, 2.], scale=[11, 22.])# Evaluate the pdf of the first distribution on 0, and the second on 1.5,<br \/># returning a length two tensor.<br \/>dist.prob([0, 1.5])# Get 3 samples, returning a 3 x 2 tensor.<br \/>dist.sample([3])<\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f7a9032 elementor-widget elementor-widget-text-editor\" data-id=\"f7a9032\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>Using the above code you can calculate CDFs and Multiple Normal Distributions on-the-fly.<br \/>While using statistical-tools in your project you might also require declaring Multivariate Distributions, tf-probability has got you covered here as well!<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">mvn = tfd.MultivariateNormalDiag(loc=[0., 0.], scale_diag = [1., 1.])<br \/>print(\"Batch shape:\", mvn.batch_shape)<br \/>print(\"Event shape:\", mvn.event_shape)samples = mvn.sample(1000)<br \/>print(\"Samples shape:\", samples.shape)g = sns.jointplot(samples[:, 0], samples[:, 1], kind='scatter')<br \/>plt.show()<\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-48edac3 elementor-widget elementor-widget-text-editor\" data-id=\"48edac3\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>There are tons of distributions like these inside the tfp module, LogNormal, Logistic, LogitNormal Mixture, Multinomial, MultivariateNormalDiag, to name a few. Every distribution comes with a plethora of statistical inferences and functions.<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1bb15a4 elementor-widget elementor-widget-heading\" data-id=\"1bb15a4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><h1 id=\"4971\">Bijectors-<\/h1><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8266c7c elementor-widget elementor-widget-text-editor\" data-id=\"8266c7c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>Bijectors represent invertible, smooth functions. They can be used to transform distributions, preserving the ability to take samples and compute log_probs. They can be accessed from the\u00a0<code><a href=\"https:\/\/www.tensorflow.org\/probability\/api_docs\/python\/tfp\/bijectors\" target=\"_blank\" rel=\"noreferrer noopener\">tfp.bijectors<\/a><\/code>\u00a0module.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Each bijector implements at least 3 methods:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list -->\n<ul>\n<li><code>forward<\/code><\/li>\n<li><code>inverse<\/code>, and<\/li>\n<li>(at least) one of\u00a0<code>forward_log_det_jacobian<\/code>\u00a0and\u00a0<code>inverse_log_det_jacobian<\/code>.<\/li>\n<\/ul>\n<!-- \/wp:list -->\n\n<!-- wp:paragraph -->\n<p>With these ingredients, we can transform a distribution and still get samples and log probs from the result!<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">print_subclasses_from_module(tfp.bijectors, tfp.bijectors.Bijector)<\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4ee461b elementor-widget elementor-widget-text-editor\" data-id=\"4ee461b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>Using the code snippet above we can list all the bijector functions available in tf-probability.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Now let\u2019s see how to declare a NormalCDF using bijectors and calculate the log of the determinant of the Jacobian in forward propagation.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">normal_cdf = tfp.bijectors.NormalCDF()<br \/>xs = np.linspace(-4., 4., 200)<br \/>plt.plot(xs, normal_cdf.forward(xs))<br \/>plt.show()plt.plot(xs, normal_cdf.forward_log_det_jacobian(xs, event_ndims=0))<br \/>plt.show()<\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8c32448 elementor-widget elementor-widget-text-editor\" data-id=\"8c32448\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>Bijectors are mainly used for transforming Distributions. Here\u2019s a quick example where transformation is applied:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"># creates a Y=g(X)=exp(X) transform<br \/>exp_bijector = tfp.bijectors.Exp()# declare a Normal Distribution and Transform it<br \/>log_normal = exp_bijector(tfd.Normal(0., .5))<br \/><br \/>samples = log_normal.sample(1000)<br \/>xs = np.linspace(1e-10, np.max(samples), 200)sns.distplot(samples, norm_hist=True, kde=False)<br \/>plt.plot(xs, log_normal.prob(xs), c='k', alpha=.75)<br \/>plt.show()<\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5662212 elementor-widget elementor-widget-text-editor\" data-id=\"5662212\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>After running the above snippet you\u2019ll see a transformed distribution(bins in blue) and probability density estimation plot (curve in black)<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>We successfully implemented the building blocks of statistical inference in Tensorflow-probability, now let\u2019s see how we can use the same framework with\u00a0<em>tabular time-series<\/em>\u00a0data.<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-cbff082 elementor-widget elementor-widget-heading\" data-id=\"cbff082\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><h1 id=\"7f21\">Timeseries with Tensorflow-Probability<\/h1><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d95a9e8 elementor-widget elementor-widget-text-editor\" data-id=\"d95a9e8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\n<!-- wp:paragraph -->\n<p>TensorFlow-Probability (TFP) features built-in support for fitting and forecasting using structural time series models. This support includes the Bayesian inference of the model. Because they\u2019re built-in TensorFlow, these methods naturally take advantage of vectorized hardware (GPUs and TPUs), can efficiently process many time series in parallel, and can be integrated with deep neural networks.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Let\u2019s solve a time-series problem using TensorFlow-probability:<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3090464 elementor-widget elementor-widget-heading\" data-id=\"3090464\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><h1 id=\"f6d9\">Importing necessary modules:<\/h1><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-77e44d3 elementor-widget elementor-widget-text-editor\" data-id=\"77e44d3\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>Now let\u2019s define a time-series problem of\u00a0<em>Co2 concentration<\/em>\u00a0over the years (sampled by month):<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4949ae1 elementor-widget elementor-widget-image\" data-id=\"4949ae1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2020\/08\/Untitled-2.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-80addcd elementor-widget elementor-widget-image\" data-id=\"80addcd\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1460\/0*suNPjRkKQEaYPD9M.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c6b02ad elementor-widget elementor-widget-heading\" data-id=\"c6b02ad\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><h1 id=\"f9f1\">Model and Fitting<\/h1><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-32de51d elementor-widget elementor-widget-text-editor\" data-id=\"32de51d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>We\u2019ll model this series with a local linear trend, plus a month-of-year seasonal effect, and fit the model using\u00a0<em>variational inference.<\/em>\u00a0This involves running an optimizer to minimize a variational loss function, the negative evidence lower bound (ELBO). This fits a set of approximate posterior distributions for the parameters (in practice we assume these to be independent Normals transformed to the support space of each parameter).<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>The\u00a0<code>tfp.sts<\/code>\u00a0forecasting methods require posterior samples as inputs, so we&#8217;ll finish by drawing a set of samples from the variational posterior.<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c88c67d elementor-widget elementor-widget-image\" data-id=\"c88c67d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2020\/08\/Untitled-3-1.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ba72cb0 elementor-widget elementor-widget-text-editor\" data-id=\"ba72cb0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>After running the above script you\u2019ll see a plot like this:<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-851f334 elementor-widget elementor-widget-image\" data-id=\"851f334\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/803\/0*QTk5M6o5o-PoWncd.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4384274 elementor-widget elementor-widget-heading\" data-id=\"4384274\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><h1 id=\"6464\">Forecasting<\/h1><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c49ad05 elementor-widget elementor-widget-text-editor\" data-id=\"c49ad05\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>Now let\u2019s use the fitted model to construct a forecast. We just call\u00a0<code>tfp.sts.forecast<\/code>, which returns a TensorFlow Distribution instance representing the predictive distribution over future timesteps.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:html -->\n<figure><iframe src=\"https:\/\/towardsdatascience.com\/media\/d26f8ea61cb716642cb6bd0b073b2be7\" width=\"0\" height=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/figure>\n<!-- \/wp:html -->\n\n<!-- wp:paragraph -->\n<p>In particular, the\u00a0<code>mean<\/code>\u00a0and\u00a0<code>stddev<\/code>\u00a0of the forecast distribution give us a prediction with marginal uncertainty at each timestep, and we can also draw samples of possible futures.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:html -->\n<figure><iframe src=\"https:\/\/towardsdatascience.com\/media\/871bb1615602f0c2983335bf686f66ee\" width=\"0\" height=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/figure>\n<!-- \/wp:html -->\n<!-- wp:paragraph -->\n<p>Lets now visualize the forecast of the Co2 Concentration.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:html -->\n<figure><iframe src=\"https:\/\/towardsdatascience.com\/media\/7c609dd6a93b77e6f473817a7aa20268\" width=\"0\" height=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/figure>\n<!-- \/wp:html -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-af79964 elementor-widget elementor-widget-image\" data-id=\"af79964\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/803\/0*QTk5M6o5o-PoWncd.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d5ddeed elementor-widget elementor-widget-text-editor\" data-id=\"d5ddeed\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>We can observe here that the forecast made by our model turned out pretty good. It was able to capture seasonal changes and the overall trend of the time-series and able to predict the Co2 concentrations as it happened.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Let\u2019s now see the Trend and Seasonality of the Co2 concentration:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:html -->\n<figure><iframe src=\"https:\/\/towardsdatascience.com\/media\/5f9f403818f8379e712d2968ba295c01\" width=\"0\" height=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/figure>\n<!-- \/wp:html -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-54dd920 elementor-widget elementor-widget-image\" data-id=\"54dd920\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1698\/0*_QcxuVnBnACKt62O.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8784826 elementor-widget elementor-widget-text-editor\" data-id=\"8784826\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>As we already discussed how statistical inferences and tools can be easily implemented through TFP. In the above plot, we can identify trends and seasonality from the data just as efficiently as other core stats modules.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:separator --><hr class=\"wp-block-separator\" \/><!-- \/wp:separator -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a3cc4d1 elementor-widget elementor-widget-heading\" data-id=\"a3cc4d1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><h1 id=\"305f\">You\u2019ve reached the end!<\/h1><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-32d0102 elementor-widget elementor-widget-text-editor\" data-id=\"32d0102\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>Congratulations! we just learned about the use and importance of Probabilistic-programming and discussed statistical tools in TensorFlow-probability. Not to mention we just solved a structured time-series problem using models from Tensorflow-probability!<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Probabilistic programming is a tool for statistical modeling. The idea is to borrow lessons from the world of programming languages and apply them to the problems of designing and using statistical models.<\/p>\n","protected":false},"author":877,"featured_media":9221,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[187],"tags":[519,521,520],"ppma_author":[3754],"class_list":["post-9220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bigdata-cloud","tag-probabilistic-programming","tag-programming-languages","tag-statistical-modeling"],"authors":[{"term_id":3754,"user_id":877,"is_guest":0,"slug":"fabiana-clemente","display_name":"Fabiana Clemente","avatar_url":"https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2020\/08\/Fabiana-Clemente-150x150.png","user_url":"https:\/\/ydata.ai","last_name":"Clemente","first_name":"Fabiana","job_title":"","description":"Fabiana Clemente a data scientist, is Co-Founder &amp; CDO at YData that provides privacy and synthetic data tools."}],"_links":{"self":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/9220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/users\/877"}],"replies":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/comments?post=9220"}],"version-history":[{"count":6,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/9220\/revisions"}],"predecessor-version":[{"id":34269,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/9220\/revisions\/34269"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media\/9221"}],"wp:attachment":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media?parent=9220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/categories?post=9220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/tags?post=9220"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=9220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}