File: example-fitting-line.rst

package info (click to toggle)
astropy 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 41,972 kB
  • sloc: python: 219,331; ansic: 147,297; javascript: 13,556; lex: 8,496; sh: 3,319; xml: 1,622; makefile: 185
file content (152 lines) | stat: -rw-r--r-- 4,450 bytes parent folder | download | duplicates (2)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
.. _example_fitting_line:

Fitting a Line
==============

Fitting a line to (x,y) data points is a common case in many areas.
Examples fits are given for fitting, fitting using the uncertainties
as weights, and fitting using iterative sigma clipping.

Simple Fit
----------

Here the (x,y) data points are fit with a line.  The (x,y) data
points are simulated and have a range of uncertainties to give
a realistic example.

.. plot::
    :include-source:

    import numpy as np
    import matplotlib.pyplot as plt
    from astropy.modeling import models, fitting

    # define a model for a line
    line_orig = models.Linear1D(slope=1.0, intercept=0.5)

    # generate x, y data non-uniformly spaced in x
    # add noise to y measurements
    npts = 30
    rng = np.random.default_rng(10)
    x = rng.uniform(0.0, 10.0, npts)
    y = line_orig(x)
    yunc = np.absolute(rng.normal(0.5, 2.5, npts))
    y += rng.normal(0.0, yunc, npts)

    # initialize a linear fitter
    fit = fitting.LinearLSQFitter()

    # initialize a linear model
    line_init = models.Linear1D()

    # fit the data with the fitter
    fitted_line = fit(line_init, x, y)

    # plot
    plt.figure()
    plt.plot(x, y, 'ko', label='Data')
    plt.plot(x, line_orig(x), 'b-', label='Simulation Model')
    plt.plot(x, fitted_line(x), 'k-', label='Fitted Model')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()

Fit using uncertainties
-----------------------

Fitting can be done using the uncertainties as weights.
To get the standard weighting of 1/unc^2 for the case of
Gaussian errors, the weights to pass to the fitting are 1/unc.

.. plot::
    :include-source:

    import numpy as np
    import matplotlib.pyplot as plt
    from astropy.modeling import models, fitting

    # define a model for a line
    line_orig = models.Linear1D(slope=1.0, intercept=0.5)

    # generate x, y data non-uniformly spaced in x
    # add noise to y measurements
    npts = 30
    rng = np.random.default_rng(10)
    x = rng.uniform(0.0, 10.0, npts)
    y = line_orig(x)
    yunc = np.absolute(rng.normal(0.5, 2.5, npts))
    y += rng.normal(0.0, yunc, npts)

    # initialize a linear fitter
    fit = fitting.LinearLSQFitter()

    # initialize a linear model
    line_init = models.Linear1D()

    # fit the data with the fitter
    fitted_line = fit(line_init, x, y, weights=1.0/yunc)

    # plot
    plt.figure()
    plt.errorbar(x, y, yerr=yunc, fmt='ko', label='Data')
    plt.plot(x, line_orig(x), 'b-', label='Simulation Model')
    plt.plot(x, fitted_line(x), 'k-', label='Fitted Model')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()

Iterative fitting using sigma clipping
--------------------------------------

When fitting, there may be data that are outliers from the fit
that can significantly bias the fitting.  These outliers can
be identified and removed from the fitting iteratively.
Note that the iterative sigma clipping assumes all the data
have the same uncertainties for the sigma clipping decision.

.. plot::
    :include-source:

    import numpy as np
    import matplotlib.pyplot as plt
    from astropy.stats import sigma_clip
    from astropy.modeling import models, fitting

    # define a model for a line
    line_orig = models.Linear1D(slope=1.0, intercept=0.5)

    # generate x, y data non-uniformly spaced in x
    # add noise to y measurements
    npts = 30
    rng = np.random.default_rng(10)
    x = rng.uniform(0.0, 10.0, npts)
    y = line_orig(x)
    yunc = np.absolute(rng.normal(0.5, 2.5, npts))
    y += rng.normal(0.0, yunc, npts)

    # make true outliers
    y[3] = line_orig(x[3]) + 6 * yunc[3]
    y[10] = line_orig(x[10]) - 4 * yunc[10]

    # initialize a linear fitter
    fit = fitting.LinearLSQFitter()

    # initialize the outlier removal fitter
    or_fit = fitting.FittingWithOutlierRemoval(fit, sigma_clip, niter=3, sigma=3.0)

    # initialize a linear model
    line_init = models.Linear1D()

    # fit the data with the fitter
    fitted_line, mask = or_fit(line_init, x, y, weights=1.0/yunc)
    filtered_data = np.ma.masked_array(y, mask=mask)

    # plot
    plt.figure()
    plt.errorbar(x, y, yerr=yunc, fmt="ko", fillstyle="none", label="Clipped Data")
    plt.plot(x, filtered_data, "ko", label="Fitted Data")
    plt.plot(x, line_orig(x), 'b-', label='Simulation Model')
    plt.plot(x, fitted_line(x), 'k-', label='Fitted Model')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()