CELL 45
# Figure 3: Scaling Laws for Random Feature EnsemblesensErrFuncs
CELL 46
## Gaussian Data Experiment Sweeping $\ell$ and $M$.  Adding a sweep over $r$
CELL 47
importlib.reload(LearningCurveExperiments)
importlib.reload(EnsembleRFs)
importlib.reload(EnsRFTheory)
CELL 48
# Parameters
num_trials = 5
M_list = np.unique(np.round(np.logspace(2, 3, 6)))
ell_list = np.linspace(.2, 1, 9)
lamVals = np.logspace(-3, 3, 100)
P = 30000
nonlinearity = None  # Assuming linear model
CELL 49
alpha = 1.5
r_list = [.4, .8, 1.2]
D = 30000  # Dimensionality of data
sigma_eps = 0
CELL 50
for r in r_list:
    print('Starting r = ' + str(r))

    # Perform training using the new function
    test_errors = LearningCurveExperiments.train_random_feature_models_ell_synthetic(
        num_trials,
        ell_list,
        M_list,
        P,
        lamVals,
        D,
        alpha,
        r,
        Fvar=1,
        sigma_eps=sigma_eps
    )

    # Generate sigma_s and w_star for theoretical computations
    sigma_s, w_star = LearningCurveExperiments.makeGaussianParams(D, alpha, r)

    # Perform theoretical computations
    #NOTE: During Theory Computations, you must use fractional values for N and K to get the true exponents.  This is ok because only the ratios matter.
    test_errors_theory = LearningCurveExperiments.compute_theoretical_learning_curves_ell(
        sigma_s.cpu().numpy(),
        w_star.cpu().numpy(),
        ell_list,
        lamVals,
        P,
        M_list,
        sigma_eps=sigma_eps
    )

    # Define the experiment output and parameters as a dictionary
    experiment_results = {
        'num_trials': num_trials,
        'ell_list': ell_list,
        'M_list': M_list,
        'lamVals': lamVals,
        'P': P,
        'nonlinearity': nonlinearity,
        'alpha': alpha,
        'r': r,
        'D': D,
        'sigma_eps': sigma_eps,
        'test_errors': test_errors,
        'test_errors_theory': test_errors_theory
    }

    # Include key parameters in the filename for clarity
    M_min = int(M_list[0])
    M_max = int(M_list[-1])
    filename = f'RF_Gaussian_alpha{alpha}_r{r}_M_{M_min}-{M_max}_P_{P}_trials_{num_trials}.pkl'

    # Define the save path
    save_path = '/n/holystore01/LABS/pehlevan_lab/Lab/bruben/Ensemble_Random_Features'

    # Ensure the directory exists
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    # Save the dictionary as a pickle file
    with open(os.path.join(save_path, filename), 'wb') as f:
        pickle.dump(experiment_results, f)

    print(f"Experiment saved to {os.path.join(save_path, filename)}")