% number of samples
n = 200;

% ratio of p/n
gamma = 2;

% number of features
p = gamma * n;

% isotropic covariance
Sigma = eye(p);

% isotropic features
Z = randn(n, p);
X = Z * sqrtm(Sigma);

% sample covariance
hSigma = X' * X / n;
[V_hSigma, D_hSigma] = eig(hSigma);

% gram matrix
K = X * X' / n;
[V_K, D_K] = eig(K);

s_min = (1 - sqrt(gamma))^2;
s_max = (1 + sqrt(gamma))^2;

n_grid = 100;

lambda_grid_1 = linspace(-10, -1 * s_max, n_grid);
% lambda_grid_2 = linspace(-1 * s_min, - 0.01, n_grid);

lambda_grid = [lambda_grid_1];

pop = zeros(length(lambda_grid), 1);
sam = zeros(length(lambda_grid), 1);

for i = 1:length(lambda_grid)
    
    lambda = lambda_grid(i);
    pop_mat = pinv(hSigma + lambda * eye(p));
    pop(i) = 1 + trace(pop_mat) / n;
    sam_mat = pinv(K + lambda * eye(n));
    sam(i) = 1 / (1 - trace(pop_mat * hSigma) / n);
    
end

plot(lambda_grid, pop)
hold on
grid on
plot(lambda_grid, sam)