File: datafit.py

package info (click to toggle)
mystic 0.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,656 kB
  • sloc: python: 40,894; makefile: 33; sh: 9
file content (69 lines) | stat: -rw-r--r-- 2,002 bytes parent folder | download
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
#!/usr/bin/env python
#
# Problem definition:
# Example in reference documentation for scipy.optimze
# http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
# 
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
# Copyright (c) 1997-2016 California Institute of Technology.
# Copyright (c) 2016-2024 The Uncertainty Quantification Foundation.
# License: 3-clause BSD.  The full license text is available at:
#  - https://github.com/uqfoundation/mystic/blob/master/LICENSE
"""
    Fit parameters to noisy data:
               y(x) ~ a * exp(-b * x) + c

    where:
               0 >= x >= 4
               y(x) = y0(x) + yn
               y0(x) = a0 * exp(-b0 * x) + c0
               a0,b0,c0 = 2.5,1.3,0.5
               yn = 0.2 * Normal(0,1)
"""
from numpy import exp, linspace
from numpy.random import normal
from mystic import reduced

def y0(coeffs, x):
    a,b,c = coeffs
    return a * exp(-b * x) + c

coeffs = (2.5, 1.3, 0.5)

# Create noisy data from the 'solution' parameters
x = linspace(0, 4, 50)
y = y0(coeffs, x) + 0.2 * normal(size=len(x))

@reduced(lambda x,y: abs(x)+abs(y))
def objective(coeffs, x, y):
    return y0(coeffs, x) - y

bounds = [(0, 10),(0, 10),(0, 10)]
args = (x,y)

# 'solution' is:
try:
    from scipy.optimize import curve_fit
    xs,pcov = curve_fit(lambda x,*coeffs: y0(coeffs,x), x, y, p0=[1,1,1])
except ImportError:
    xs = coeffs
ys = objective(xs, x, y)


if __name__ == '__main__':

  from mystic.solvers import diffev2
  from mystic.math import almostEqual
# from mystic.monitors import VerboseMonitor
# mon = VerboseMonitor(10)

  result = diffev2(objective, args=args, x0=bounds, bounds=bounds, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True)#, itermon=mon)
# print("%s %s" % (result[0], xs))
  assert almostEqual(result[0], xs, rel=5e-1)
  assert almostEqual(result[1], ys, rel=5e-1)

#XXX: how approximate the covariance matrix of estimates (pcov) w/ mystic?
#XXX: mystic should have leastsq


# EOF