Comments (7)
+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.
@piti118 What do you think?
from iminuit.
@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.
One side limit is supported for quite a while.
We could add method that wraps around fitarg. That should be easy enough.
from iminuit.
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.
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.
This is implemented in release 1.3.
from iminuit.
Related Issues (20)
- Please make iminuit 2.11.2 compatible to scipy 1.10 HOT 3
- test_matrix fails on i386 HOT 4
- Specify required version of numpy HOT 1
- [bug]: annotation evaluation breaks non-standard delayed annotation evalutation HOT 2
- 2 tests fail HOT 3
- Problems with iminuit on ubuntu version 22.04.2 ? HOT 1
- FitResult may raise ImportError if matplotlib is not installed in Jupyter notebooks
- Fails to build from source on Fedora 38 x86_64 HOT 9
- Support Minuit.fixed[...] = number
- Proof-read iminuit basic manual HOT 1
- Improve Minuit.fmin._repr_html_ and repr
- Cannot redirect output to text file HOT 1
- Suppression of Migrad output HOT 1
- 1 test fails: AssertionError: assert True == ('no convergence' in 'jacobi: iter=0 d=[3.194528] ... HOT 1
- `test_Template_with_model_2D ` fails on aarch64 HOT 11
- Error in iminuit.util.describe HOT 2
- 7 tests fail HOT 1
- Tests fail without ipywidgets HOT 3
- `_safe_log` is not safe HOT 5
- iminuit hangs when trying to visualize step function HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from iminuit.