forked from murraylab/PyDDM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitresult.py
88 lines (79 loc) · 3.32 KB
/
fitresult.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright 2018 Max Shinn <[email protected]>
# 2018 Norman Lam <[email protected]>
#
# This file is part of PyDDM, and is available under the MIT license.
# Please see LICENSE.txt in the root directory for more information.
from paranoid.decorators import paranoidclass, accepts, returns
from paranoid.types import Number, Self, String, Unchecked, Dict, Nothing, ExtendedReal, Maybe
import numpy as np
@paranoidclass
class FitResult:
"""An object to describe the result of a model fit.
This keeps track of information related to the fitting procedure.
It has the following elements:
- method: the name of the solver used to solve the model,
e.g. "analytical" or "implicit"
- fitting_method: the name of the algorithm used to minimize the
loss function method (e.g. "differential_evolution")
- loss: the name of the loss function (e.g. "BIC")
- properties: a dictionary containing any additional values saved
by the loss function or fitting procedure (e.g. "likelihood" for
BIC loss function, or "mess" for a message describing the output).
So, for example, can access FitResult.method to get the name of
the numerical algorithm used to solve the equation.
To access the output value of the loss function, use
FitResult.value().
"""
@staticmethod
def _generate():
yield FitResult(fitting_method="Test method", loss="Likelihood", method="cn",
value=1.1, prop1="xyz", prop2=-2)
@staticmethod
def _test(v):
assert v.val in Maybe(Number)
assert v.method in String()
assert v.loss in String()
assert v.properties in Dict(String, Unchecked)
def __init__(self, fitting_method, method, loss, value, **kwargs):
"""An object for simulation results.
- `fitting_method` - A string describing the fitting method.
- `loss` - A string describing the loss function
- `value` - The optimal value of the loss function for this
model
- `method` - The algorithm used to create the correct/error
PDFs
- `kwargs` is a dict of any additional properties that should
be saved for the fit.
"""
self.val = value
self.method = method
self.loss = loss
self.properties = kwargs
self.fitting_method = fitting_method
def __repr__(self):
components = ["fitting_method=%s" % repr(self.fitting_method),
"method=%s" % repr(self.method),
"loss=%s" % repr(self.loss),
"value=%s" % repr(self.val)]
components += ["%s=%s" % (k,repr(v)) for k,v in self.properties.items()]
return type(self).__name__ + "(" + ", ".join(components) + ")"
@accepts(Self)
@returns(ExtendedReal)
def value(self):
"""Returns the objective function value of the fit.
If there was an error, or if no fit was performed, return
inf.
"""
if self.val is not None:
return self.val
else:
return np.inf
class FitResultEmpty(FitResult):
"""A default Fit object before a model has been fit."""
def __repr__(self):
return type(self).__name__ + "()"
def __init__(self):
self.val = None
self.properties = {}
self.method = ""
self.loss = ""