function New_RSV=Reduced_Space_Variance_Updater(CURRENT_FLS, PRIORS, KK)
% function New_RSV=Reduced_Space_Variance_Updater(CURRENT_FLS, PRIORS, KK)
% UPDATES the reduced-space variances, treating the N levels of a factor as
% a transform of N-1 IID normal draws. 

%extract the current estimate of the levels for this factor:
levels=eval(['CURRENT_FLS.Levels_', num2str(KK)]);
%prior parameters:
prior_pars=eval(['PRIORS.Red_space_var_', num2str(KK)]);
%transform matrix:
Q_M=eval(['PRIORS.Q_M_', num2str(KK)]);

%get the first parameter of the Inverse Gamma:
alpha_post=(length(levels)-1)/2 + prior_pars(1);

%get the inner product of the levels in the reduced space.
%recall that d_star = Q_M' d, where d are the levels and d_star is the
%reduced space representation, so that d_star' * d_star = d' * Q_M * Q_M' * d
inner_prod=levels' * Q_M * Q_M' * levels;

%get the second parameter:
beta_post=inner_prod/2 + prior_pars(2);

%make the draw:
New_RSV=1/randraw('gamma', [0,1/beta_post, alpha_post], [1,1]);