Hi Nainsi,
MATLAB code for the asked problem -->
false_alarm_prob = 0.002;
mu_shifted = [shift_magnitude, zeros(1, p-1)];
data_no_shift = mvnrnd(mu, sigma, n);
data_shifted = mvnrnd(mu_shifted, sigma, n);
T2_no_shift = sum((data_no_shift - mu).^2, 2);
T2_shifted = sum((data_shifted - mu).^2, 2);
threshold = chi2inv(1 - false_alarm_prob, p);
false_alarms_no_shift = sum(T2_no_shift > threshold);
false_alarms_shifted = sum(T2_shifted > threshold);
fprintf('Number of variables (p): %d\n', p);
Number of variables (p): 20
fprintf('False alarm probability: %.4f\n', false_alarm_prob);
False alarm probability: 0.0020
fprintf('Number of false alarms (no shift): %d\n', false_alarms_no_shift);
Number of false alarms (no shift): 2
fprintf('Number of false alarms (shifted): %d\n', false_alarms_shifted);
Number of false alarms (shifted): 3
I first set the parameters for the code above, including the intended false alarm probability (false_alarm_prob), the desired number of variables (p), the required number of samples (n), the magnitude of the shift in the x1 variable (shift_magnitude), and so on.
I then use the mvnrnd function from the Statistics and Machine Learning Toolbox to create the multivariate normal data. I produce two sets of data: one with no shift in the x1 variable (data_no_shift), and the other with a shift in that variable (data_shifted).
Then, using the equation `T2 = sum((x - mu).^2, 2)`, I determine the Hotelling's T2 statistic for each sample.2), where mu is the mean vector and x is the data matrix.
Using the intended false alarm probability and the number of variables, I use the chi-square inverse function chi2inv to compute the threshold for the Hotelling's T2 statistic.
Finally, I compare the estimated T2 statistic with the threshold to determine how many false alarms there were for each scenario. The findings are shown, along with the total number of false alarms for both the case with and without the shifted variable.
You can see that as the number of variables increases, the number of false alarms for detecting the shift in the x1 variable likewise increases, showing a drop in the chance of detecting the shift, by running this code for various values of p.