Skip to content

Instantly share code, notes, and snippets.

@jirilukavsky
Created February 15, 2017 08:46
Show Gist options
  • Save jirilukavsky/42139c4c5ef049312e52d471a295d76d to your computer and use it in GitHub Desktop.
Save jirilukavsky/42139c4c5ef049312e52d471a295d76d to your computer and use it in GitHub Desktop.
Fitting psychometric function in Python
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()
@Timothysit
Copy link

In line 7, p1 should be p2. The code then works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment