3

In R, the following is used to calculate the integral between points 0 and 0.5 on beta distribution with the parameters 10 and 20:

 integrate(function(p) dbeta(p,10,20),0,0.5)

The result is:

0.9692858 absolute error < 6.6e-08

How can this be done in Python?

A.E
  • 997
  • 1
  • 16
  • 33
  • 1
    `scipy.stats.beta(10,20).cdf(0.5)` – Paul Panzer Aug 05 '19 at 17:42
  • @PaulPanzer thanks for the answer. It seems though cdf takes only one argument (upper limit of the integral). It throws an error if i want to integrate over 0.2 to 0.5 like this scipy.stats.beta(10,20).cdf(0.2, 0.5). Also, please answer the question so I can confirm it. – A.E Aug 05 '19 at 18:18

2 Answers2

7

You can use the .cdf attribute of scipy.stats.beta. For a proper interval use the difference, e.g.

betacdf = scipy.stats.beta(10,20).cdf
betacdf(0.5)-betacdf(0.2)
# 0.9200223098258666
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
0
from scipy.integrate import quad
def f(x): return beta.pdf(x, 10, 20)
res, err = quad(f, 0, 0.5)
print (res)
print (err)
  • 2
    This answer was flagged as [Low Quality](https://stackoverflow.com/help/review-low-quality), and could benefit from an explanation. Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/32204606) – Trenton McKinney Jul 08 '22 at 17:47