Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Bayesian Analysis with Python
Bayesian Analysis with Python

Bayesian Analysis with Python: A practical guide to probabilistic modeling , Third Edition

eBook
€26.98 €29.99
Paperback
€37.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Bayesian Analysis with Python

Chapter 1
Thinking Probabilistically

Probability theory is nothing but common sense reduced to calculation. – Pierre Simon Laplace

In this chapter, we will learn about the core concepts of Bayesian statistics and some of the instruments in the Bayesian toolbox. We will use some Python code, but this chapter will be mostly theoretical; most of the concepts we will see here will be revisited many times throughout this book. This chapter, being heavy on the theoretical side, is perhaps a little anxiogenic for the coder in you, but I think it will ease the path to effectively applying Bayesian statistics to your problems.

In this chapter, we will cover the following topics:

  • Statistical modeling

  • Probabilities and uncertainty

  • Bayes’ theorem and statistical inference

  • Single-parameter inference and the classic coin-flip problem

  • Choosing priors and why people often don’t like them but should

  • Communicating a Bayesian analysis

1.1 Statistics, models, and this book’s approach

Statistics is about collecting, organizing, analyzing, and interpreting data, and hence statistical knowledge is essential for data analysis. Two main statistical methods are used in data analysis:

  • Exploratory Data Analysis (EDA): This is about numerical summaries, such as the mean, mode, standard deviation, and interquartile ranges. EDA is also about visually inspecting the data, using tools you may be already familiar with, such as histograms and scatter plots.

  • Inferential statistics: This is about making statements beyond the current data. We may want to understand some particular phenomenon, maybe we want to make predictions for future (yet unobserved) data points, or we need to choose among several competing explanations for the same set of observations. In summary, inferential statistics allow us to draw meaningful insights from a limited set of data and make informed decisions based on the results of our analysis.

A Match Made in Heaven

The focus of this book is on how to perform Bayesian inferential statistics, but we will also use ideas from EDA to summarize, interpret, check, and communicate the results of Bayesian inference.

Most introductory statistical courses, at least for non-statisticians, are taught as a collection of recipes that go like this: go to the statistical pantry, pick one tin can and open it, add data to taste, and stir until you obtain a consistent p-value, preferably under 0.05. The main goal of these courses is to teach you how to pick the proper can. I never liked this approach, mainly because the most common result is a bunch of confused people unable to grasp, even at the conceptual level, the unity of the different learned methods. We will take a different approach: we will learn some recipes, but they will be homemade rather than canned food; we will learn how to mix fresh ingredients that will suit different statistical occasions and, more importantly, that will let you apply concepts far beyond the examples in this book.

Taking this approach is possible for two reasons:

  • Ontological: Statistics is a form of modeling unified under the mathematical framework of probability theory. Using a probabilistic approach provides a unified view of what may seem like very disparate methods; statistical methods and machine learning methods look much more similar under the probabilistic lens.

  • Technical: Modern software, such as PyMC, allows practitioners, just like you and me, to define and solve models in a relatively easy way. Many of these models were unsolvable just a few years ago or required a high level of mathematical and technical sophistication.

1.2 Working with data

Data is an essential ingredient in statistics and data science. Data comes from several sources, such as experiments, computer simulations, surveys, and field observations. If we are the ones in charge of generating or gathering the data, it is always a good idea to first think carefully about the questions we want to answer and which methods we will use, and only then proceed to get the data. There is a whole branch of statistics dealing with data collection, known as experimental design. In the era of the data deluge, we can sometimes forget that gathering data is not always cheap. For example, while it is true that the Large Hadron Collider (LHC) produces hundreds of terabytes a day, its construction took years of manual and intellectual labor.

As a general rule, we can think of the process of generating the data as stochastic, because there is ontological, technical, and/or epistemic uncertainty, that is, the system is intrinsically stochastic, there are technical issues adding noise or restricting us from measuring with arbitrary precision, and/or there are conceptual limitations veiling details from us. For all these reasons, we always need to interpret data in the context of models, including mental and formal ones. Data does not speak but through models.

In this book, we will assume that we already have collected the data. Our data will also be clean and tidy, something that’s rarely true in the real world. We will make these assumptions to focus on the subject of this book. I just want to emphasize, especially for newcomers to data analysis, that even when not covered in this book, there are important skills that you should learn and practice to successfully work with data.

A very useful skill when analyzing data is knowing how to write code in a programming language, such as Python. Manipulating data is usually necessary given that we live in a messy world with even messier data, and coding helps to get things done. Even if you are lucky and your data is very clean and tidy, coding will still be very useful since modern Bayesian statistics is done mostly through programming languages such as Python or R. If you want to learn how to use Python for cleaning and manipulating data, you can find a good introduction in Python for Data Analysis by McKinney [2022].

1.3 Bayesian modeling

Models are simplified descriptions of a given system or process that, for some reason, we are interested in. Those descriptions are deliberately designed to capture only the most relevant aspects of the system and not to explain every minor detail. This is one reason a more complex model is not always a better one. There are many different kinds of models; in this book, we will restrict ourselves to Bayesian models. We can summarize the Bayesian modeling process using three steps:

  1. Given some data and some assumptions on how this data could have been generated, we design a model by combining building blocks known as probability distributions. Most of the time these models are crude approximations, but most of the time that’s all we need.

  2. We use Bayes’ theorem to add data to our models and derive the logical consequences of combining the data and our assumptions. We say we are conditioning the model on our data.

  3. We evaluate the model, and its predictions, under different criteria, including the data, our expertise on the subject, and sometimes by comparing it to other models.

In general, we will find ourselves performing these three steps in an iterative non-linear fashion. We will retrace our steps at any given point: maybe we made a silly coding mistake, or we found a way to change the model and improve it, or we realized that we need to add more data or collect a different kind of data.

Bayesian models are also known as probabilistic models because they are built using probabilities. Why probabilities? Because probabilities are a very useful tool to model uncertainty; we even have good arguments to state they are the correct mathematical concept. So let’s take a walk through the garden of forking paths [Borges1944].

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Conduct Bayesian data analysis with step-by-step guidance
  • Gain insight into a modern, practical, and computational approach to Bayesian statistical modeling
  • Enhance your learning with best practices through sample problems and practice exercises
  • Purchase of the print or Kindle book includes a free PDF eBook.

Description

The third edition of Bayesian Analysis with Python serves as an introduction to the main concepts of applied Bayesian modeling using PyMC, a state-of-the-art probabilistic programming library, and other libraries that support and facilitate modeling like ArviZ, for exploratory analysis of Bayesian models; Bambi, for flexible and easy hierarchical linear modeling; PreliZ, for prior elicitation; PyMC-BART, for flexible non-parametric regression; and Kulprit, for variable selection. In this updated edition, a brief and conceptual introduction to probability theory enhances your learning journey by introducing new topics like Bayesian additive regression trees (BART), featuring updated examples. Refined explanations, informed by feedback and experience from previous editions, underscore the book's emphasis on Bayesian statistics. You will explore various models, including hierarchical models, generalized linear models for regression and classification, mixture models, Gaussian processes, and BART, using synthetic and real datasets. By the end of this book, you will possess a functional understanding of probabilistic modeling, enabling you to design and implement Bayesian models for your data science challenges. You'll be well-prepared to delve into more advanced material or specialized statistical modeling if the need arises.

Who is this book for?

If you are a student, data scientist, researcher, or developer looking to get started with Bayesian data analysis and probabilistic programming, this book is for you. The book is introductory, so no previous statistical knowledge is required, although some experience in using Python and scientific libraries like NumPy is expected.

What you will learn

  • Build probabilistic models using PyMC and Bambi
  • Analyze and interpret probabilistic models with ArviZ
  • Acquire the skills to sanity-check models and modify them if necessary
  • Build better models with prior and posterior predictive checks
  • Learn the advantages and caveats of hierarchical models
  • Compare models and choose between alternative ones
  • Interpret results and apply your knowledge to real-world problems
  • Explore common models from a unified probabilistic perspective
  • Apply the Bayesian framework's flexibility for probabilistic thinking

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 31, 2024
Length: 394 pages
Edition : 3rd
Language : English
ISBN-13 : 9781805127161
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jan 31, 2024
Length: 394 pages
Edition : 3rd
Language : English
ISBN-13 : 9781805127161
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 119.97
Transformers for Natural Language Processing and Computer Vision
€41.99
Mastering NLP from Foundations to LLMs
€39.99
Bayesian Analysis with Python
€37.99
Total 119.97 Stars icon

Table of Contents

14 Chapters
Chapter 1 Thinking Probabilistically Chevron down icon Chevron up icon
Chapter 2 Programming Probabilistically Chevron down icon Chevron up icon
Chapter 3 Hierarchical Models Chevron down icon Chevron up icon
Chapter 4 Modeling with Lines Chevron down icon Chevron up icon
Chapter 5 Comparing Models Chevron down icon Chevron up icon
Chapter 6 Modeling with Bambi Chevron down icon Chevron up icon
Chapter 7 Mixture Models Chevron down icon Chevron up icon
Chapter 8 Gaussian Processes Chevron down icon Chevron up icon
Chapter 9 Bayesian Additive Regression Trees Chevron down icon Chevron up icon
Chapter 10 Inference Engines Chevron down icon Chevron up icon
Chapter 11 Where to Go Next Chevron down icon Chevron up icon
Bibliography Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7
(23 Ratings)
5 star 78.3%
4 star 17.4%
3 star 0%
2 star 0%
1 star 4.3%
Filter icon Filter
Top Reviews

Filter reviews by




Jon Barley Nov 08, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
An excellent introduction into practical Bayesian analysis with many illuminating examples.
Feefo Verified review Feefo
RP Aug 13, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you had to buy just one book on Bayesian analysis, this is the one to get. It takes a lot of skill to write a concise, readable book on such a complicated topic.
Amazon Verified review Amazon
ben Jun 18, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Osvaldo Martin’s “Bayesian Analysis with Python” is an exceptional resource for anyone looking to delve into the world of Bayesian inference using Python. The book is tailored for readers who possess a basic understanding of Python but may not have extensive knowledge of statistics or Bayesian methods. This accessibility makes it an ideal starting point for beginners while still offering depth for more experienced readers.One of the book’s standout features is its practical approach. Each chapter concludes with exercises that reinforce the concepts covered, and there is even a dedicated Discord space provided by the publisher for further discussion and learning. The introductory chapters lay a strong foundation in both theoretical and computational aspects of Bayesian inference, with “Thinking Probabilistically” and “Programming Probabilistically” offering a seamless blend of theory and hands-on coding with PyMC, one of the leading probabilistic programming languages.For those new to the subject, reading the first two chapters in tandem can be particularly beneficial, combining conceptual understanding with computational implementation. Subsequent chapters delve into specific modeling approaches, such as hierarchical models, generalized linear models, mixture models, Gaussian processes, and Bayesian adaptive regression trees (BART). Each of these chapters provides valuable insights and practical knowledge that can be directly applied to real-world problems.The book also covers essential topics in practice, including model comparison and evaluation, which are crucial for any data scientist. The chapter on Bambi is especially noteworthy, demonstrating how formula syntax can be used to efficiently build PyMC models, accompanied by clear visual representations of the models using Graphviz.Additionally, the chapter on inference engines serves as a comprehensive reference for understanding the mechanics behind Bayesian samplers and inference methods, making it a valuable resource for both teaching and practical application.Overall, “Bayesian Analysis with Python” is an excellent book for anyone interested in Bayesian inference. It successfully bridges the gap between core concepts and practical implementation, and it does so using the robust Bayesian “tech stack” of PyMC, ArviZ, and Bambi. The book also provides an excellent list of further resources, including books, code repositories, paid courses, and open-source community hangouts, making it a well-rounded and highly recommended read for aspiring Bayesian analysts.
Amazon Verified review Amazon
Banachan Mar 06, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a great practical guide to probabilistic modeling using Python, especially for those interested in or working with Bayesian data analysis. It covers a broad range of topics, from basic concepts to more advanced modeling techniques, so I can see it being an invaluable resource to both practitioners and students.The book provides a thorough exploration, specifically on addressing Bayesian analysis techniques. It's quite an enjoyable and comprehensive guide for both beginners and advanced practitioners. Good emphasis on fundamentals, then transitions to more complex concepts and application. I thought it provided a good balance between theory and practical skills.Summary of pros are that it encourages hands-on learning, with wide coverage on the subject. Doesn't hurt that it's an updated edition with up-to-date approaches. Some of the cons are that it might be complex in some sections for pure beginners. Code examples are mostly in Python (so I guess this could be a con or pro depending on how look at it).I still like that it has a lot of examples, code snippets, and real-world scenarios included, with good explanations. Covers model construction, prior selection, model comparison, to predictive analysis. I surmise that this allows for a variety of learning needs for most folks. Author style is both authoritative and accessible. I would recommend this book for academia, professional development, or just for personal interest in data science.
Amazon Verified review Amazon
Nicole M Radziwill May 07, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
While Python is my go-to language for things like NLP, I usually use R for everything else. After spending a solid long weekend with Martin's new book "Bayesian Analysis with Python" I can confirm that this book will be just what ONE audience needs, but may disappoint others. As a gentle introduction to Bayesian approaches for people who are well versed in intro statistics and have a solid foundation in Python, it's perfect. But if you're missing that mathematical statistics background (or if you're rusty on Python) this book may present a struggle.As a result, this is five stars for the target audience and four for the other audiences.The writing is clear and easy to follow, but sometimes encourages you to "review the code for understanding" where the text could have explained each of the lines of code in sequence. The book also assumes that the reader has a fundamental understanding of distributions and mathematical notation, which may not be the case for all programmers or data analysts. As a professor this would have been a great book to use from an introductory Bayesian methods course for juniors or seniors in STEM with at least one or two semesters of Python. For this group, the book is particularly strong, because it takes a computation-first approach but fills in the gaps with just enough theory.Highlights include:- There is a simple discussion on ROPE and loss functions that is valuable- There is a good discussion about how to do linear regression the Bayesian way (hint: all parameters treated as priors)- Some interesting mixture models using the Palmer Penguins dataset- The best part was the MCMC with Metropolis-Hastings to calculate the value of piDO buy this book if you have a solid foundation in Python (and a Python environment already set up) and want to spend a few weeks (or a couple months) expanding your understanding into building and running simple Bayesian models. If you have the time to spend, this will deepen your understanding.DO NOT buy this book if you are a programmer who needs to start building Bayesian models at work within the next couple days! It's not going to help you work that next ticket in the queue.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.