def test_Model(): """ """ M = sb.Model() x = np.linspace(0.1, 0.9, 22) target_mu = 0.5 target_sigma = 1 target_y = sb.cumgauss(x, target_mu, target_sigma) F = M.fit(x, target_y, initial=[target_mu, target_sigma]) npt.assert_equal(F.predict(x), target_y)
def test_params_regression(): """ Test for regressions in model parameter values from provided data """ model = sb.Model() ortho_x, ortho_y, ortho_n = sb.transform_data( op.join(data_path, 'ortho.csv')) para_x, para_y, para_n = sb.transform_data(op.join(data_path, 'para.csv')) ortho_fit = model.fit(ortho_x, ortho_y) para_fit = model.fit(para_x, para_y) npt.assert_almost_equal(ortho_fit.params[0], 0.46438638) npt.assert_almost_equal(ortho_fit.params[1], 0.13845926) npt.assert_almost_equal(para_fit.params[0], 0.57456788) npt.assert_almost_equal(para_fit.params[1], 0.13684096)
def error_func(x, w): return x * w coefs = np.zeros([len(noise_levels), n_boots]) for ii, n_level in enumerate(noise_levels): # Generate y for this noise level y = w * x + n_level * np.random.randn(N) for jj in range(n_boots): # Pull subsets of data ixs_boot = np.random.randint(0, N, N) x_boot = x[ixs_boot] y_boot = y[ixs_boot] # Fit the model and return the coefs model = sb.Model(error_func) fit = model.fit(x_boot, y_boot, (.5,)) coefs[ii, jj] = fit.params[0] ############################################################################### # Assessing coefficient stability # ------------------------------- # # Now we'll assess the stability of the fitted coefficient for varying levels # of noise. Let's plot the raw values for each noise level, as well as the # 95% confidence interval. percentiles = np.percentile(coefs, [2.5, 97.5], axis=1).T fig, ax = plt.subplots() for n_level, i_coefs, percs in zip(noise_levels, coefs, percentiles): ax.scatter(np.repeat(n_level, len(i_coefs)), i_coefs)
# First, we'll load some data into shablona. data_path = op.join(sb.__path__[0], 'data') ortho_x, ortho_y, ortho_n = sb.transform_data(op.join(data_path, 'ortho.csv')) para_x, para_y, para_n = sb.transform_data(op.join(data_path, 'para.csv')) ############################################################################### # Fitting a model # --------------- # # With shablona, models are created with the :ref:Model class. # This class has a `fit` method that returns the coefficients for the given # input data. # Instantiate our model and fit it on two datasets model = sb.Model() ortho_fit = model.fit(ortho_x, ortho_y) para_fit = model.fit(para_x, para_y) # These are the parameters that our model has discovered print(ortho_fit.params) print(para_fit.params) ############################################################################### # Visualizing results # ------------------- # # Now we will visualize the results of our model fit. We'll generate # a vector of input points, and use them to determine the model's output # for each input. Then we'll plot what these curves look like.