neurotools.stats.regressions module

Routines for common regression tasks.

neurotools.stats.regressions.damped_cosine(X, Y, W)[source]

Regress a damped cosine impulse response to point data X and Y using weighting W.

Todo: constrain b, L to be positive

Parameters:
  • X (1D array-like) – List of distances

  • Y (1D array-like) – List of average pairwise distances

  • W (1D array-like) – List of weights

Returns:

result – Optimization result returned by scipy.optimize.minimize. See scipy.optimize documentation for details.

Return type:

object

Example

X = 0.4*arange(9)
Y = np.exp(-X/4+1)*np.cos(X)
Z = Y+randn(*shape(X))
W = ones(shape(X))
w,L,b = damped_cosine(X,Z,W).x
plot(X,Y)
plot(X,Z)
plot(X,np.cos(w*X)*np.exp(-X/L+b))
neurotools.stats.regressions.weighted_least_squares(X, Y, W)[source]

Initialize power law fit EPS = 1e-10 use = (X>EPS)&(Y>EPS) weighted_least_squares(np.log(X+EPS)[use],np.log(Y+EPS)[use],1/(EPS+X[use]))

Parameters:
  • X (List of distances)

  • Y (List of amplitudes)

  • W (Weights for points)

Returns:

result – Optimization result returned by scipy.optimize.minimize. See scipy.optimize documentation for details.

Return type:

object

neurotools.stats.regressions.power_law(X, Y, W)[source]

Fit a power law, but with error terms computed by r^2 in the original space.

Parameters:
  • X (List of distances)

  • Y (List of amplitudes)

  • W (Weights for points)

neurotools.stats.regressions.gaussian_function(X, Y)[source]
Parameters:
  • X (List of distances)

  • Y (List of amplitudes)

Returns:

result – Optimization result returned by scipy.optimize.minimize. See scipy.optimize documentation for details.

Return type:

object

neurotools.stats.regressions.half_gaussian_function(X, Y)[source]
Parameters:
  • X (List of distances)

  • Y (List of amplitudes)

neurotools.stats.regressions.exponential_decay(X, Y)[source]

Fit exponential decay from an initial value to a final value with some time (or length, etc) constant.

tau,scale,dc = exponential_decay(X,Y) fits z = np.exp(-X/tau)*scale+dc using least squares.

Parameters:
  • X (List of distances)

  • Y (List of amplitudes)

Returns:

  • tau (float) – Length constant of exponential fit

  • scale (float) – Scale parameter (magnitude at zero) of exponential fit

  • dc (float) – DC offset of exponential fit (asymptotic value)

neurotools.stats.regressions.robust_line(X, Y)[source]

2-variable linear regression with L1 penalty returns the tuple (m,b) for line in y = mx+b format

Parameters:
  • X (List of distances)

  • Y (List of amplitudes)

Returns:

result.x – Optimization result returned by scipy.optimize.minimize. See scipy.optimize documentation for details.

Return type:

array-like

neurotools.stats.regressions.cubic_spline_regression(x, y, x_eval, df=5, NBOOTSTRAP=1000, reg=1e-15, show_progress=False)[source]

Bivariate x→y cubic spline regression with bootstrap convidence intervals.

Splines are spaced according to the points in x_eval, not x.

Depends on the cr() function in the patsy package.

Parametrs

x: length NSAMPLES iterable of scalars

Independent variable

y: length NSAMPLES iterable of scalars

Dependent variable

x_eval: iterable of scalars

Points at which to evalute the resulting model.

param df:

Degrees of freedom for cubic spline regression.

type df:

int>0; default 5

param NBOOTSTRAP:

Number of samples to use for bootstrap

type NBOOTSTRAP:

int>0; default 1000

param reg:

Regularization for linear least squares

type reg:

positive float; default 1e-15

param show_progress:

Show progress bar while sampling bootstrap

type show_progress:

boolean; default False

returns:
  • y_hat (np.float32) – Smooted curve evaluated at x_eval

  • samples (np.float32) – NBOOTSTRAP × len(x_eval) bootstrap samples.

class neurotools.stats.regressions.CircregressResult(theta, y, nboot=1000, nshuff=1000, parallel=True, show_progress=False)[source]

Bases: object

get_theta_samples()[source]
get_d2_samples()[source]
get_R2_samples()[source]
neurotools.stats.regressions.circregress(*args, **kwargs)[source]