Matlab 生成具有异常值的正态随机样本

Matlab generate normal random sample with outliers

提问人:Alex 提问时间:11/9/2023 更新时间:11/9/2023 访问量:28

问:

我的烦恼:

我想要

  1. 创建随机正态样本
  2. 选择1至20的随机指数(只有一个指数),例如每个样品中的OBE元素必须增加10-12倍
  3. 找到这个指数的元素,并在10-12倍内增加
  4. 在 MATLAB 中使用函数后,计算样本的平均值和中位数bootstrap
  5. 我存储在一个单元中的每个样本
  6. 步骤 1-5 我想对每个单元格重复,最后得到all_y和all_stats....

我的代码:

clear
clc
clf
close all
format long
warning('off','all')

location = 17;
scale = 1;
num_samples = 20;
num_bootstraps = 1;

y = cell(1, num_samples);
stats = cell(1, num_samples);

for i = 1:num_samples
    % Generate a random normal sample
    sample = normrnd(location, scale, [1, num_samples]);

    % Choose a random index
    idx = randi([1, num_samples]);

    % Increase the element at the chosen index by 10-12 times
    sample(idx) = sample(idx) * randi([10, 12]);

    y{i} = sample;

    % Perform bootstrap resampling to calculate mean and median
    bootstrap_means = zeros(num_bootstraps, 1);
    bootstrap_medians = zeros(num_bootstraps, 1);
    for j = 1:num_bootstraps
        % Resample with replacement
        resampled_data = randsample(sample, num_samples, true);

        % Calculate mean and median for the resampled data
        bootstrap_means(j) = mean(resampled_data);
        bootstrap_medians(j) = median(resampled_data);
    end

    stats{i} = [bootstrap_means, bootstrap_medians];
end

% Combine all samples into one array
all_y = cat(1, y{:});

% Combine all statistics (mean and median) into one array
all_stats = cat(1, stats{:});

% Combine the original samples, means, and medians into a single dataset
data_3 = [all_y, all_stats];

这段代码不能正常工作,在一些样本中找不到异常值,有些样本在上面的代码中包含多个异常值?如何解决这个问题?

上面提供的问题解决方案的 Coode。

MATLAB 单元格

评论


答: 暂无答案