Sensitivity estimate example

Sensitivity estimate example#

import numpy as np
import matplotlib.pyplot as pl
%pylab inline

import sys
sys.path.append('../scripts')
from linear_regression import linreg

from IPython.core.display import Image 
Image(filename='../img/sensitivity_error_example.png',width=400) 
%pylab is deprecated, use %matplotlib inline and import the required libraries.
Populating the interactive namespace from numpy and matplotlib
../_images/2539a693cea3ca7b3efae72fe0b808757ac88799ecadcf02ba89b2800798f88e.png
x = np.array([0.5, 1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0])
y = np.array([0.4, 1.0, 2.3, 6.9, 15.8, 36.4, 110.1, 253.2])
pl.plot(x,y,'--o')
pl.xlabel('$x$ [cm]')
pl.ylabel('$y$ [V]')
pl.title('Calibration curve')
Text(0.5, 1.0, 'Calibration curve')
../_images/d477aafafa7c7011e7c8c40d422541ca027cf37bee674358037528b05f60eb0b.png

Sensitivity, \(K\) is:

\( K_i = \left( \frac{\partial y}{\partial x} \right)_{x_i} \)

K = np.diff(y)/np.diff(x)
print (K)
[1.2        1.3        1.53333333 1.78       2.06       2.45666667
 2.862     ]
pl.plot(x[1:],K,'--o')
pl.xlabel('$x$ [cm]')
pl.ylabel('$K$ [V/cm]')
pl.title('Sensitivity')
Text(0.5, 1.0, 'Sensitivity')
../_images/1bef7b515dbf30ef8b282b3b8f2e5b5240152b7e7e4255188b893b2c1dc77a4a.png

Instead of working with non-linear curve of sensitivity we can use the usual trick: the logarithmic scale

pl.loglog(x,y,'--o')
pl.xlabel('$x$ [cm]')
pl.ylabel('$y$ [V]')
pl.title('Logarithmic scale')
Text(0.5, 1.0, 'Logarithmic scale')
../_images/4e119354bbb0038ddfae41828c92c98b5dd45e106fe3671ac817b67a4caf85e9.png
logK = np.diff(np.log10(y))/np.diff(np.log10(x))
print( logK)
pl.plot(x[1:],logK,'--o')
pl.xlabel('$x$ [cm]')
pl.ylabel('$K$ [V/cm]')
pl.title('Logarithmic sensitivity')
pl.plot([x[1],x[-1]],[1.2,1.2],'r--')
[1.32192809 1.20163386 1.19897785 1.19525629 1.20401389 1.20793568
 1.20146294]
[<matplotlib.lines.Line2D at 0x7efe893c5ac0>]
../_images/82660ab72af6a24973b16fd866ddd85c5df7f56e60081ee3a35d9868046a25b3.png
pl.loglog(x,y,'o',x,x**(1.2))
pl.xlabel('$x$ [cm]')
pl.ylabel('$y$ [V]')
pl.title('Logarithmic scale')
pl.legend(('$y$','$x^{1.2}$'),loc='best')
<matplotlib.legend.Legend at 0x7efe893c61b0>
../_images/14775e32e6e248a3e602feaa81736912a73c3da112f4e48cfdcbaabe9a77ee2b.png
pl.plot(x,y-x**(1.2),'o')
pl.xlabel('$x$ [cm]')
pl.ylabel('$y - y_c$ [V]')
pl.title('Deviation plot')
# pl.legend(('$y$','$x^{1.2}$'),loc='best')
Text(0.5, 1.0, 'Deviation plot')
../_images/a760ad4f2d654cd1313c88df1897f083d954880108f193cbfacc80f644a68abc.png

Regression analysis#

Following the recipe of http://www.answermysearches.com/how-to-do-a-simple-linear-regression-in-python/124/

print (linreg(np.log10(x),np.log10(y)))
Estimate: y = ax + b
N = 8
Degrees of freedom $\nu$ = 6 
a = 1.21 $\pm$ 0.005
b = -0.01 $\pm$ 0.005
R^2 = 1.000
Syx = 0.011
y = 1.21 x + -0.01 $\pm$ 0.010 V
(np.float64(1.2103157469888082), np.float64(-0.012527809481276199), np.float64(0.9998888247342179), np.float64(0.011222369359282008))
pl.loglog(x,y,'o',x,x**(1.21)-0.01252)
pl.xlabel('$x$ [cm]')
pl.ylabel('$y$ [V]')
pl.title('Logarithmic scale')
pl.legend(('$y$','$x^{1.2}$'),loc='best')
<matplotlib.legend.Legend at 0x7efe88f463c0>
../_images/05282580c1144275f4ef7b0132c2b890dd53854ce29e7df07ecadc896d088c5f.png
pl.plot(x,y-(x**(1.21)-0.01252),'o')
pl.xlabel('$x$ [cm]')
pl.ylabel('$y - y_c$ [V]')
pl.title('Deviation plot');
# pl.legend(('$y$','$x^{1.2}$'),loc='best')
../_images/8ed1e4922314483f10fb44ac7a9b6fb59c4845780a91fa99b7b8739c19bd6216.png