[Template Fitting] Is it possible to apply this constraint? #857
-
Aloha Dr. Dembinski, Thank you for always being available and helpful! I wonder if you could give me some hints on implementing the following constraint in my template fitting. I have 6 components, say, A, B, C, D, E, F, and a constraint: Mahalo, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
When you have an equality constraint like this, you would normally express one of the degrees of freedom in your cost function by the others. I assume that yield(A), yield(B) etc are numbers, and free fit parameters (if we ignore the constraint). def original_cost_function(yield_a, yield_b, yield_c, yield_d):
...
def modified_cost_function(yield_b, yield_c, yield_d):
yield_a = <compute from yield_b, yield_c, yield_d using the equality constraint>
return original_cost_function(yield_a, yield_b, yield_c, yield_d) I assume you are doing a template fit, so in your case, it would work something like this. template_cost = Template(...) # insert your templates and data
def modified_cost_function(yield_b, yield_c, yield_d):
yield_a = <compute from yield_b, yield_c, yield_d using the equality constraint>
return template_cost(yield_a, yield_b, yield_c, yield_d)
m = Minuit(modified_cost_function, *initial_yields)
m.migrad() An alternative is to use the Scipy minimizer and use a NonlinearConstraint. In your case that's scipy.optimize.NonlinearConstraint(lambda yields: yields[0]/yields[1] - yields[2]/yields[3] * constant, 0, 0) I don't have experience how well the fit works with equality constraints. So far, I always used the first solution to this problem. |
Beta Was this translation helpful? Give feedback.
-
def modified_c4(a,b,c,d,e,f):
b = a*e/d/1.5
return c4(a,b,c,d,e,f) This cannot work, you need this: def modified_c4(a,c,d,e,f):
b = a*e/d/1.5
return c4(a,b,c,d,e,f) or you need to fix the parameter "b" from the point of view of Minuit. If you write a function which effectively does not use the value that you pass to the cost function, then derivatives with respect to that parameter which Minuit computes are always zero, including the second derivatives. That's why HESSE fails, you cannot invert a Hessian matrix which contains a row and column of zeros, it does not have full rank. |
Beta Was this translation helpful? Give feedback.
When you have an equality constraint like this, you would normally express one of the degrees of freedom in your cost function by the others.
I assume that yield(A), yield(B) etc are numbers, and free fit parameters (if we ignore the constraint).
I assume you are doing a template fit, so in your case, it would work something like this.