forked from gajomi/enzymatica
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_model_example.py
More file actions
39 lines (28 loc) · 865 Bytes
/
Copy pathlinear_model_example.py
File metadata and controls
39 lines (28 loc) · 865 Bytes
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
import numpy as np
import scipy.optimize as optimize
import pylab as plt
def linear_model(args):
"""A simple linear model"""
def f(x):
return args[0]*x + args[1]
return f
N = 10
x = np.linspace(0,1,N)
m_exact = 1.5
b_exact = 0.5
params_exact = (m_exact,b_exact)
f_exact = linear_model(params_exact)
y_exact = f_exact(x)
sigma = .005
y_data = y_exact + sigma*np.random.randn(N)
f = lambda args: (linear_model(args)(x) - y_data)/sigma
params_0 = (1.8,.4)
params_est, params_est_cov, infodict, message,flag = optimize.leastsq(f,params_0,full_output=1)
f_est = linear_model(params_est)
y_est = f_est(x)
m_range = np.linspace(0,3,200)
b_range = np.linspace(0,1,200)
mm,bb = np.meshgrid(m_range,b_range)
f_squared = lambda m,b: sum((linear_model((m,b))(x) - y_data)**2/sigma**2)
f_squared = np.vectorize(f_squared)
error = f_squared(mm,bb)