Created
February 15, 2017 08:46
-
-
Save jirilukavsky/42139c4c5ef049312e52d471a295d76d to your computer and use it in GitHub Desktop.
Fitting psychometric function in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy.optimize import curve_fit | |
import scipy as sy | |
import matplotlib.pyplot as plt | |
d = np.array([75, 80, 90, 95, 100, 105, 110, 115, 120, 125], dtype=float) | |
p1 = np.array([6, 13, 25, 29, 29, 29, 30, 29, 30, 30], dtype=float) / 30. # scale to 0..1 | |
# psychometric function | |
def pf(x, alpha, beta): | |
return 1. / (1 + np.exp( -(x-alpha)/beta )) | |
# fitting | |
par0 = sy.array([100., 1.]) # use some good starting values, reasonable default is [0., 1.] | |
par, mcov = curve_fit(pf, d, p2, par0) | |
print(par) | |
plt.plot(d, p2, 'ro') | |
plt.plot(d, pf(d, par[0], par[1])) | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In line 7,
p1
should bep2
. The code then works.