Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 127793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:31:59+00:00 2026-05-11T05:31:59+00:00

In attempting to use scipy’s quad method to integrate a gaussian (lets say there’s

  • 0

In attempting to use scipy’s quad method to integrate a gaussian (lets say there’s a gaussian method named gauss), I was having problems passing needed parameters to gauss and leaving quad to do the integration over the correct variable. Does anyone have a good example of how to use quad w/ a multidimensional function?

But this led me to a more grand question about the best way to integrate a gaussian in general. I didn’t find a gaussian integrate in scipy (to my surprise). My plan was to write a simple gaussian function and pass it to quad (or maybe now a fixed width integrator). What would you do?

Edit: Fixed-width meaning something like trapz that uses a fixed dx to calculate areas under a curve.

What I’ve come to so far is a method make___gauss that returns a lambda function that can then go into quad. This way I can make a normal function with the average and variance I need before integrating.

def make_gauss(N, sigma, mu):     return (lambda x: N/(sigma * (2*numpy.pi)**.5) *             numpy.e ** (-(x-mu)**2/(2 * sigma**2)))  quad(make_gauss(N=10, sigma=2, mu=0), -inf, inf) 

When I tried passing a general gaussian function (that needs to be called with x, N, mu, and sigma) and filling in some of the values using quad like

quad(gen_gauss, -inf, inf, (10,2,0)) 

the parameters 10, 2, and 0 did NOT necessarily match N=10, sigma=2, mu=0, which prompted the more extended definition.

The erf(z) in scipy.special would require me to define exactly what t is initially, but it nice to know it is there.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:31:59+00:00Added an answer on May 11, 2026 at 5:31 am

    Okay, you appear to be pretty confused about several things. Let’s start at the beginning: you mentioned a ‘multidimensional function’, but then go on to discuss the usual one-variable Gaussian curve. This is not a multidimensional function: when you integrate it, you only integrate one variable (x). The distinction is important to make, because there is a monster called a ‘multivariate Gaussian distribution’ which is a true multidimensional function and, if integrated, requires integrating over two or more variables (which uses the expensive Monte Carlo technique I mentioned before). But you seem to just be talking about the regular one-variable Gaussian, which is much easier to work with, integrate, and all that.

    The one-variable Gaussian distribution has two parameters, sigma and mu, and is a function of a single variable we’ll denote x. You also appear to be carrying around a normalization parameter n (which is useful in a couple of applications). Normalization parameters are usually not included in calculations, since you can just tack them back on at the end (remember, integration is a linear operator: int(n*f(x), x) = n*int(f(x), x) ). But we can carry it around if you like; the notation I like for a normal distribution is then

    N(x | mu, sigma, n) := (n/(sigma*sqrt(2*pi))) * exp((-(x-mu)^2)/(2*sigma^2))

    (read that as ‘the normal distribution of x given sigma, mu, and n is given by…’) So far, so good; this matches the function you’ve got. Notice that the only true variable here is x: the other three parameters are fixed for any particular Gaussian.

    Now for a mathematical fact: it is provably true that all Gaussian curves have the same shape, they’re just shifted around a little bit. So we can work with N(x|0,1,1), called the ‘standard normal distribution’, and just translate our results back to the general Gaussian curve. So if you have the integral of N(x|0,1,1), you can trivially calculate the integral of any Gaussian. This integral appears so frequently that it has a special name: the error function erf. Because of some old conventions, it’s not exactly erf; there are a couple additive and multiplicative factors also being carried around.

    If Phi(z) = integral(N(x|0,1,1), -inf, z); that is, Phi(z) is the integral of the standard normal distribution from minus infinity up to z, then it’s true by the definition of the error function that

    Phi(z) = 0.5 + 0.5 * erf(z / sqrt(2)).

    Likewise, if Phi(z | mu, sigma, n) = integral( N(x|sigma, mu, n), -inf, z); that is, Phi(z | mu, sigma, n) is the integral of the normal distribution given parameters mu, sigma, and n from minus infinity up to z, then it’s true by the definition of the error function that

    Phi(z | mu, sigma, n) = (n/2) * (1 + erf((x - mu) / (sigma * sqrt(2)))).

    Take a look at the Wikipedia article on the normal CDF if you want more detail or a proof of this fact.

    Okay, that should be enough background explanation. Back to your (edited) post. You say ‘The erf(z) in scipy.special would require me to define exactly what t is initially’. I have no idea what you mean by this; where does t (time?) enter into this at all? Hopefully the explanation above has demystified the error function a bit and it’s clearer now as to why the error function is the right function for the job.

    Your Python code is OK, but I would prefer a closure over a lambda:

    def make_gauss(N, sigma, mu):     k = N / (sigma * math.sqrt(2*math.pi))     s = -1.0 / (2 * sigma * sigma)     def f(x):         return k * math.exp(s * (x - mu)*(x - mu))     return f 

    Using a closure enables precomputation of constants k and s, so the returned function will need to do less work each time it’s called (which can be important if you’re integrating it, which means it’ll be called many times). Also, I have avoided any use of the exponentiation operator **, which is slower than just writing the squaring out, and hoisted the divide out of the inner loop and replaced it with a multiply. I haven’t looked at all at their implementation in Python, but from my last time tuning an inner loop for pure speed using raw x87 assembly, I seem to remember that adds, subtracts, or multiplies take about 4 CPU cycles each, divides about 36, and exponentiation about 200. That was a couple years ago, so take those numbers with a grain of salt; still, it illustrates their relative complexity. As well, calculating exp(x) the brute-force way is a very bad idea; there are tricks you can take when writing a good implementation of exp(x) that make it significantly faster and more accurate than a general a**b style exponentiation.

    I’ve never used the numpy version of the constants pi and e; I’ve always stuck with the plain old math module’s versions. I don’t know why you might prefer either one.

    I’m not sure what you’re going for with the quad() call. quad(gen_gauss, -inf, inf, (10,2,0)) ought to integrate a renormalized Gaussian from minus infinity to plus infinity, and should always spit out 10 (your normalization factor), since the Gaussian integrates to 1 over the real line. Any answer far from 10 (I wouldn’t expect exactly 10 since quad() is only an approximation, after all) means something is screwed up somewhere… hard to say what’s screwed up without knowing the actual return value and possibly the inner workings of quad().

    Hopefully that has demystified some of the confusion, and explained why the error function is the right answer to your problem, as well as how to do it all yourself if you’re curious. If any of my explanation wasn’t clear, I suggest taking a quick look at Wikipedia first; if you still have questions, don’t hesitate to ask.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to use this method I've constructed to save the layer of
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng
When attempting to use HttpWebRequest to retrieve a page from my dev server, I
Im attempting to use a C++ extension for Python called PySndObj. and getting an
In attempting to use std::select1st from <functional> in a VS2008 project I found that
Im attempting to use websockets for a project. It needs to use the ipad,
I am attempting to use an MVVM-ish approach to my WPF development. I have
I am attempting to use the code-behind (Page_Load or PreRender) to set a date-time
I'm attempting to use Ramaze, the ruby framework, to implement a RESTful controller. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.