Skip to content
Snippets Groups Projects
Commit 7277141a authored by Roger Wolf's avatar Roger Wolf
Browse files

cleanup

parent 3a83dd0d
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:c2162620-c8d2-4bd6-a3ee-0b3212728d1c tags:
# Verwendung der `MultiFit`-Klasse von *kafe2*
Das folgende, aus der Dokumentation von *kafe2* entnommene und weiter kommentierte, Code-Beispiel demonstriert die Verwendung der Klasse `MultiFit`aus dem *kafe2* Programm-Paket. Sie finden die Original-Dokumentation [hier](https://kafe2.readthedocs.io/en/latest/parts/beginners_guide.html#multifit).
%% Cell type:markdown id:3cb8c38e-52de-4c01-a080-534e04ee95b3 tags:
%% Cell type:markdown id:37e82252-e2bc-4eb1-a432-0d9888e178eb tags:
## Physikalische Problemstellung
%% Cell type:markdown id:6b2b7efa-6254-4e3d-af44-7a407f2459fc tags:
Das Beispiel basiert auf den Messpunkten der Datei `OhmsLawExperiment.dat`. Es geht um die Bestimmung des ohmschen Widerstands $R$ eines Drahts, aus einer Strom-Spannungs ($I(U)$)-Kennlinie. Die Darstellung von $I$ gegen $U$ fördert das überraschende Resultat zutage, dass zwischen $U$ und $I$ kein linearer Zusammenhang, wie er im Rahmen des ohmschen Gesetzes zu erwarten ist, besteht. Der Grund hierfür liegt in der Erwärmung des Drahts und der Temperaturabhängigkeit von $R$.
**Die Daten bestehen aus drei Messgrößen, die korreliert geloggt wurden: $U$, $I$ und $T$.**
%% Cell type:markdown id:27439a20-a93b-4722-a3d9-90f140f355a5 tags:
## Statistische Problemstellung
%% Cell type:markdown id:e85a5e5a-83d3-406c-918b-e1b186d52878 tags:
Auf Seiten der statistischen Analyse wird das Problem in diesem Beispiel durch eine Erweiterung des anzupassenden Modells gelöst. Dieses Modell hat die Form:
$$
T(U) = p_{2}\,U^{2} + p_{1}\,U + p_{0};
\qquad
I(U) = \frac{U}{R(T)}.
$$
Bei $T(U)$ handelt es sich um einen **rein phänomenologischen Ansatz**. Offenbar macht es keinen Sinn $R$ ohne Berücksichtigung von $T$ anzugeben, daher führen wir $R_{0}$ als den Widerstand bei der Temperatur $T=0^{\circ}$ mit der Temperaturabhängigkeit
$$
R(T) = R_{0}\bigl(1+\alpha_{T}\,T(U)\bigr)
$$
ein. Das gesamte Modell zur Beschreibung der Messreihe $\{(U_{i},\,I_{i},\,T_{i})\}$ hat mit $p_{0}$, $p_{1}$, $p_{2}$, $\alpha_{T}$ und $R_{0}$ **fünf Parameter**. Diesen stehen 20 Messpunkte gegenüber.
Mit den Mitteln einer einfachen Anpassung können wir das Modell für $T(U)$ oder aber das Modell $I(U)$ unabhängig an die Daten anpassen. **Wir wollen jedoch eine gemeinsame Anpassung beider Modelle an den gesamten korrelierten Datensatz vornehmen.** Daraus wollen wir alle fünf Parameter des Modells und insbesondere den uns v.a. interessierenden Parameter $R_{0}$ bestimmen. Die Antwort auf diese Problemstellung ist die `MultiFit`-Klasse aus dem Programm-Paket *kafe2*.
%% Cell type:code id:7a720752-a049-4a6c-960a-a074fd8e4292 tags:
``` python
import numpy as np
from kafe2 import XYFit, MultiFit, Plot
# Data
U, I, T = np.loadtxt('OhmsLawExperiment.dat', unpack=True)
# Convert T from Kelvin to deg Celsius
T -= 273.15
# Uncertainties
dU, dI, dT = 0.2, 0.1, 0.5
# Model; the default values define the start parameter of the fit
def empirical_T_U_model(U, p_2=1.0, p_1=1.0, p_0=0.0):
return p_2*U**2+p_1*U+p_0
def I_U_model(U, R_0=1., alpha=0.004, p_2=1.0, p_1=1.0, p_0=0.0):
_temperature = empirical_T_U_model(U, p_2, p_1, p_0)
return U/(R_0*(1.0+_temperature*alpha))
# Setup the fit of the model to the data
# Step-1: Define XYFit objects "fit_0" and "fit_1"
fit_0 = XYFit(
xy_data=[U, T], # Data: U und T
model_function=empirical_T_U_model # Model: empirical_T_U_model
)
# Uncertainties in T; uncertainties in U will be introduced later
fit_0.add_error(
axis='y',
err_val=dT
)
# Label for the data set
fit_0.data_container.label = "Temperature data"
# Labels for the columns of the data set
fit_0.data_container.axis_labels = (
"Voltage (V)",
"Temperature (°C)"
)
# Label for the model
fit_0.model_label = "Temperature parametrization"
fit_1 = XYFit(
xy_data=[U, I], # Data: U und I
model_function=I_U_model # Model: I_U_model
)
# Uncertainties in I; uncertainties in U will be introduced later
fit_1.add_error(
axis='y',
err_val=dI
)
# Label for the data set
fit_1.data_container.label = "Current data"
# Labels for the columns of the data set
fit_1.data_container.axis_labels = (
"Voltage (V)",
"Current (A)"
)
# Label for the model
fit_1.model_label = "Temperature-dependent current"
# Step-2: Define MultiFit Objekts from "fit_0" und "fit_1"
multi_fit = MultiFit(
fit_list=[fit_0, fit_1],
minimizer='iminuit'
)
# Uncertainties in U are introduced a common uncertainties that apply all fit
# objects "fit_0" and "fit_1" (indicated by the argument "all")
multi_fit.add_error(
axis='x',
err_val=dU,
fits='all'
)
# Introducing an explicit start value for the fit through the "multi_fit" object
#multi_fit.set_parameter_values(alpha=0.004)
# Introduce Latex name for the model parameter alpha through the "multi_fit"
# object
#multi_fit.assign_parameter_latex_names(alpha=r'\alpha_\mathrm{T}')
# Step-3: Do the fit and recieve the results
multi_fit.do_fit()
multi_fit.report(asymmetric_parameter_errors=True)
plot = Plot(multi_fit, separate_figures=True)
plot.plot(asymmetric_parameter_errors=True)
#plot.save()
plot.show()
```
%% Output
#########
# Fit 0 #
#########
########
# Data #
########
X Data X Data Error X Data Correlation Matrix
====== ============ =============================================================
0.5 0.2 [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
1.0 0.2 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
1.5 0.2 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
2.0 0.2 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
2.5 0.2 [0. 0. 0. 0. 1. 0. 0. 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0.2 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
Y Data Y Data Error Y Data Correlation Matrix
====== ============ =============================================================
20.35 0.5 [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
20.65 0.5 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
22.25 0.5 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
23.65 0.5 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
26.25 0.5 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
27.85 0.5 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
29.85 0.5 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
34.25 0.5 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
37.75 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
41.95 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
44.85 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
50.05 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
54.25 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
60.55 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
65.05 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
69.95 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
76.85 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
81.55 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
85.45 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
94.75 0.5 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
#########
# Model #
#########
Model Function
==============
empirical_T_U_model(U; p_2, p_1, p_0)
X Model
=======
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
Y Model
=======
19.9
20.93
22.29
23.98
26.0
28.34
31.01
34.0
37.32
40.97
44.94
49.24
53.87
58.82
64.1
69.71
75.64
81.9
88.49
95.4
#########
# Fit 1 #
#########
########
# Data #
########
X Data X Data Error X Data Correlation Matrix
====== ============ =============================================================
0.5 0.2 [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
1.0 0.2 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
1.5 0.2 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
2.0 0.2 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0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 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
10.0 0.2 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
Y Data Y Data Error Y Data Correlation Matrix
====== ============ =============================================================
0.5 0.1 [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
0.89 0.1 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
1.41 0.1 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
1.67 0.1 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
2.3 0.1 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
2.59 0.1 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
2.77 0.1 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
3.57 0.1 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
3.94 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
4.24 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
4.73 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
4.87 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
5.35 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
5.74 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
5.77 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
6.17 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
6.32 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
6.83 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
6.87 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
7.17 0.1 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
#########
# Model #
#########
Model Function
==============
I_U_model(U; R_0, alpha, p_2, p_1, p_0)
X Model
=======
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
Y Model
=======
0.4524
0.9016
1.346
1.784
2.215
2.637
3.049
3.45
3.839
4.215
4.578
4.926
5.26
5.579
5.882
6.17
6.443
6.699
6.941
7.167
###############
# Fit Results #
###############
Model Parameters
================
p_2 = 0.653 + 0.044 (up) - 0.043 (down)
p_1 = 1.09 + 0.37 (up) - 0.37 (down)
p_0 = 19.19 + 0.52 (up) - 0.52 (down)
R_0 = 1.029 + 0.037 (up) - 0.037 (down)
alpha = 0.00373 + 0.00068 (up) - 0.00062 (down)
Model Parameter Correlations
============================
p_2 p_1 p_0 R_0 alpha
======= ======= ======= ======= =======
p_2 1.0 -0.9579 0.7548 0.6512 -0.7159
p_1 -0.9579 1.0 -0.8557 -0.6719 0.6836
p_0 0.7548 -0.8557 1.0 0.385 -0.3948
R_0 0.6512 -0.6719 0.385 1.0 -0.9683
alpha -0.7159 0.6836 -0.3948 -0.9683 1.0
Cost Function
=============
Cost function: <no description provided>
chi2 / ndf = 19.83 / 35 = 0.5667
chi2 probability = 0.982
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment