# How to detect the probability of a shift in mean value when number of variables increases?

10 views (last 30 days)
Nainsi Gupta on 9 Jul 2023
Answered: Anuj on 10 Jul 2023
Probability of a shift in mean value of a multivariate normal vector decreases as the number of variables increaes in case of Hotelling T2 chart. How can I prove this with the help of matlab code.
for example: we are monitoring p variables. The shift in one variable x1 with magnitude 1 and no shift in other (p-1) variable. There is identity correlation matrix. I need to detect the shift in x1 for achieving the same level of false alarm probability when dimension of p increases from 1 to 20.
Set false alarm probability as 0.002.

Divyam on 10 Jul 2023
We can perform this by simply calculating the Hotelling T2 value for each observation of the data we generate and then comparing it to the threshold obtained from a chi-sqaure distribution. Then we can count the number of observations which exceed the threshold and determine the probability of shift detection using the result:
Here is the workflow with code
• Create mean vector and the identity correlation matrix for the variables to create data without shift. Then add shift to the first variable.
mu = zeros(1, v); % Mean vector
Sigma = eye(v); % Identity correlation matrix
data = mvnrnd(mu, Sigma, n); % Generate data without shift
data(:, 1) = data(:, 1) + shiftMagnitude; % Add shift to x1 variable
%Note:
% v is the number of variables
% n is the number of observations we have for each variable.
%To obtain clear results keep n > 1000
• Calculate the Hostelling T2 statistic for each observation.
T2 = zeros(n, 1);
for i = 1:n
x = data(i, :);
T2(i) = (x - mu) * inv(Sigma) * (x - mu)';
end
• Calculate the threshold from the chi-square distribution (keep the false probability = 0.002).
falseAlarm = 0.002; % False alarm probability
threshold = chi2inv(1 - falseAlarm, p);
• Calculate the number observations with T2 value greater than threshold.
detections = sum(T2 > threshold);
• Calculate the probability of shift detection.
probabilityOfDetection = detections/n;

Anuj on 10 Jul 2023
Hi Nainsi,
MATLAB code for the asked problem -->
% Set the parameters
p = 20; % Number of variables
n = 1000; % Number of samples
shift_magnitude = 1; % Magnitude of shift in x1 variable
false_alarm_prob = 0.002; % Desired false alarm probability
% Generate the multivariate normal data
mu = zeros(1, p); % Mean vector for no shift
mu_shifted = [shift_magnitude, zeros(1, p-1)]; % Mean vector with shift in x1 variable
sigma = eye(p); % Identity correlation matrix
data_no_shift = mvnrnd(mu, sigma, n); % Generate data with no shift
data_shifted = mvnrnd(mu_shifted, sigma, n); % Generate data with shift
% Calculate the Hotelling's T2 statistic for each sample
T2_no_shift = sum((data_no_shift - mu).^2, 2);
T2_shifted = sum((data_shifted - mu).^2, 2);
% Calculate the threshold for the Hotelling's T2 statistic
threshold = chi2inv(1 - false_alarm_prob, p);
% Count the number of false alarms for each scenario
false_alarms_no_shift = sum(T2_no_shift > threshold);
false_alarms_shifted = sum(T2_shifted > threshold);
% Display the results
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.

### Categories

Find more on Noncentral t Distribution in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!