multiple restarts for optimization fit seems to solve occasional misses
@@ -131,6 +131,9 @@ def fit_optimize(model, data, method='cosine', pattern_idx=None,
pattern_idx=pattern_idx,
pattern_descriptor=pattern_descriptor,
sigma_k=sigma_k, ridge_weight=ridge_weight)
+ thetas = []
+ losses = []
+ for _ in range(2 * model.n_param):
theta0 = np.random.rand(model.n_param)
theta = opt.minimize(
_loss_opt,
@@ -138,11 +141,14 @@ def fit_optimize(model, data, method='cosine', pattern_idx=None,
method='BFGS',
tol=0.000001
)
- theta = theta.x
+ thetas.append(theta.x)
+ losses.append(theta.fun)
+ id = np.argmin(losses)
+ theta = thetas[id]
norm = np.sum(theta ** 2)
if norm == 0:
return theta.flatten()
- return theta.flatten() / np.sqrt(np.sum(theta ** 2))
+ return theta.flatten() / np.sqrt(norm)
def fit_optimize_positive(
@@ -201,7 +207,7 @@ def fit_optimize_positive(
norm = np.sum(theta ** 2)
if norm == 0:
return theta.flatten()
- return theta.flatten() / np.sqrt(np.sum(theta ** 2))
+ return theta.flatten() / np.sqrt(norm)
def fit_interpolate(model, data, method='cosine', pattern_idx=None,
