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

  • Home
  • SEARCH
  • 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 6747201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:24:50+00:00 2026-05-26T12:24:50+00:00

I am doing some data fitting using the pyminuit Python bindings for the minuit

  • 0

I am doing some data fitting using the pyminuit Python bindings for the minuit minimisation code (http://code.google.com/p/pyminuit/). The minimiser accepts a function and uses introspection to extract the parameters to be minimised. In general, I want to minimise the chi squared value for a dataset given a particular function to describe the dataset.

My question: Is there a way to define a chi squared function which, given an arbitrary function with varying numbers of parameters, returns a function which gives the chi squared value for that function and only contains the parameters to be minimised in the function argument specification?

Example:

from scipy import *
import minuit
# Generate some data to fit
data_x = arange(50)
noise = 0.3
data_y = data_x**3 + normal(0.0, noise)
# Fit function, e.g. a cubic
fit_func = lambda x, a1, a2, a3, a4: a1 + a2*x + a3*x**2 + a4*x**3

# Minimisation function e.g. chi squared
# Note this has only the parameters to be minimised in the definition (eg not data_x)
min_func = lambda a1, a2, a3, a4: sum( (fit_func(data_x, a1, a2, a3, a4) - data_y)**2 / noise**2 )

THIS is where I’d like to write something like min_func = make_chi2(fit_func). I don’t know what to do as data_x and data_y are only defined outside of the function. The rest of the minimisation routine, for completeness, looks like:

# Initialise minimiser object with initial values
m = minuit.Minuit(min_func, {'a1': 1.0, 'a2': 1.0, 'a3': 1.0, 'a4': 1.0})
# Run minimiser
m.migrad()
# Print minimised values - example output
print m.values
>>> {'a1': 0.000, 'a2': 0.000, 'a3': 0.000, 'a4': 1.000}

Thanks for your help in advance!

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-05-26T12:24:51+00:00Added an answer on May 26, 2026 at 12:24 pm

    Since PyMinuit uses introspection, you have to use introspection, too. make_chi_squared() could be implemented like this:

    import inspect
    
    chi_squared_template = """
    def chi_squared(%(params)s):
        return (((f(data_x, %(params)s) - data_y) / errors) ** 2).sum()
    """
    
    def make_chi_squared(f, data_x, data_y, errors):
        params = ", ".join(inspect.getargspec(f).args[1:])
        exec chi_squared_template % {"params": params}
        return chi_squared
    

    Example usage:

    import numpy
    
    def f(x, a1, a2, a3, a4):
        return a1 + a2*x + a3*x**2 + a4*x**3
    
    data_x = numpy.arange(50)
    errors = numpy.random.randn(50) * 0.3
    data_y = data_x**3 + errors
    
    chi_squared = make_chi_squared(f, data_x, data_y, errors)
    print inspect.getargspec(chi_squared).args
    

    printing

    ['a1', 'a2', 'a3', 'a4']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have lots of code which is doing some data processing (in C# to
Using mysql 5.1.41 and innodb I'm doing some data import, but can't use load
I am doing some data conversion(like csv) to xml with SAX then using transformer
I'm doing some data analysis over the judge appointment system in France, and using
I'm doing some data analysis in Matlab, and anytime I call the hold function
I am doing some research about how to access data in a UniData database
I've been doing some Web-Projects lately that rely on heavy Data-Binding and have been
I'm doing some archiving to a property list and when I unarchive my data
One thing I've started doing more often recently is retrieving some data at the
We're doing a complex bit of data accumulation. Our customer sends us some stuff

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.