Code Monkey home page Code Monkey logo

Comments (7)

bungernut avatar bungernut commented on September 4, 2024

+1
I would like this feature, to prevent having to construct a function for each combination of parameters I want to float.

I get this error when trying to set limits to prevent floating.
ValueError: limit for parameter Po218_N0 is invalid. (0.0, 0.0)

I really like lmfit-py method of handleing this via Parameters class.
Also is there no way to set only a low or high limit?

from iminuit.

cdeil avatar cdeil commented on September 4, 2024

@piti118 What do you think?

from iminuit.

cdeil avatar cdeil commented on September 4, 2024

@marcocamma @bungernut Sorry that this got lost ... I agree a better interface to fix / free parameters is needed in iminuit.

My understanding based on the Minuit class API docs is that it is possible to fix parameters via the fix_ keywords on construction: m = Minuit(f, fix_x=True). It's even possible to interact with those flags via the fitarg member:

>>> from iminuit import Minuit
>>> def f(x, y, z):
...         return (x - 2) ** 2 + (y - 3) ** 2 + (z - 4) ** 2
... 
>>> m = Minuit(f, pedantic=False)
>>> m.fitarg
{'fix_z': False, 'y': 0.0, 'z': 0.0, 'fix_y': False, 'error_x': 1.0, 'error_z': 1.0, 'limit_x': None, 'error_y': 1.0, 'limit_y': None, 'fix_x': False, 'limit_z': None, 'x': 0.0}
>>> m.fitarg['fix_x'] = True
>>> m.fitarg['fix_x']
True

I didn't look yet if this actually works and if this is covered by tests.
I also don't know yet if this is related to #163 or independent.

At the very least this feature should be documented in a high-level Sphinx docs section.
Probably it would also be possible to improve the API, i.e. introduce a Parameter class like lmfit or Sherpa or Astropy has it and expose it as an attribute on Minuit that makes it easy to set / get values / errors / fix flag, instead of having to go via fitarg and fix_ strings.

Let's wait for @piti118 to comment first.

I'm not sure if someone will have the time to implement this though ... help needed!

from iminuit.

piti118 avatar piti118 commented on September 4, 2024

One side limit is supported for quite a while.

We could add method that wraps around fitarg. That should be easy enough.

from iminuit.

marcocamma avatar marcocamma commented on September 4, 2024

It does not look like the suggested method m.fitarg["fix_x"] = True is working as shown by the code below:

from __future__ import print_function
import iminuit
import numpy as np

print("Iminuit version",iminuit.__version__)

def generateData(N=100,order=2,err=0.1):
  x = np.arange(N)
  coefs = np.random.random(order+1)
  y = np.polyval(coefs,x)+np.random.normal(scale=err,size=N)
  return coefs,x,y,err

def func(x,pars):
  return np.polyval(pars,x)
f1 = lambda x,a0,a1: a1*x+a0
f2 = lambda x,a0,a1,a2: a2*x**2+a1*x+a0
f3 = lambda x,a0,a1,a2,a3: a3*x**3+a2*x**2+a1*x+a0

class GenericChi2:
  def __init__(self, f, x, y,e=1.):
    self.f = f
    self.x = x; self.y=y; self.e=e
    args = iminuit.describe(f)#extract function signature
    self.func_code = iminuit.Struct(
      co_varnames = args[1:],#dock off independent param
      co_argcount = len(args)-1
    )
  def __call__(self, *arg):
    f = ( self.f(self.x,*arg)-self.y )/self.e
    return np.sum(f**2)

def doFit(N=100,order=2):
  # generate data
  right_values,x,y,e = generateData(N,order)
  right_values = right_values[::-1]
  # get fitting function
  f = globals()["f%d"%order]
  pars_dict = {}
  for i in range(order+1):
    pars_dict["error_a%d" % i] = 0.01
    pars_dict["a%d" % i] = 0.5
  im = iminuit.Minuit( GenericChi2(f,x,y,e),errordef=1,**pars_dict )
  im.migrad()
  p = im.values.keys(); p.sort()
  for i,pi in enumerate(p):
    print("Par %s, best fit %.4f +/- %.4f, exact value %.4f" % 
      (pi,im.values[pi],im.errors[pi],right_values[i]))


  im.fitarg["fix_a0"]=True
  im.fitarg["a0"]=0

  print("Is a0 fixed: %s"%im.is_fixed("a0"))
  im.migrad()
  p = im.values.keys(); p.sort()
  for i,pi in enumerate(p):
    print("Par %s, best fit %.4f +/- %.4f, exact value %.4f" % 
      (pi,im.values[pi],im.errors[pi],right_values[i]))

  return im

doFit()

(sorry for the less than minimal script)
thanks,
marco

from iminuit.

giacomov avatar giacomov commented on September 4, 2024

Hi,
this is somewhat related.

Let's say I have a fit with two parameters p1 and p2, and I instance Minuit with p1 fixed to 1 and p2 free to vary.

How can I then change the value which p1 is fixed to, for example to 10? This doesn't work:

minuit.values['p1'] = 10

because (I guess) the internal minuit object is not updated, so the new fit still shows p1 fixed to 1.

from iminuit.

HDembinski avatar HDembinski commented on September 4, 2024

This is implemented in release 1.3.

from iminuit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.