Sampling Technique to add uncertainity

4 views (last 30 days)
AMAN GUPTA
AMAN GUPTA on 22 Mar 2022
Answered: Balavignesh on 6 Dec 2023
I have a csv file in which I have data of temperature at 132 points. Now I want to add uncertainity in my data. How can I add uncertainity using monte carlo markov chain?

Answers (1)

Balavignesh
Balavignesh on 6 Dec 2023
Hi Aman,
I understand that you are seeking to introduce uncertainty into your dataset using the Monte Carlo Markov Chain (MCMC) method. I recommend utilizing 'readmatrix' MATLAB function to import the 'TEMP_373.csv' file and subsequently defining a model. For simplicity, I am assuming a normal distribution to characterize the uncertainty. Following this, the MCMC algorithm can be employed to sample from the posterior distribution of the model parameters, enabling the generation of uncertain temperature data.
The provided example code serves to illustrate this process.
% Step 1: Read the CSV file
temperature_data = readmatrix('TEMP_373.csv'); % Assuming the file contains a single column of temperature data
% Step 2: Define the Model
mu_prior = mean(temperature_data); % Prior mean
sigma_prior = std(temperature_data); % Prior standard deviation
% Step 3: Implement Metropolis-Hastings MCMC
numIterations = 10000; % Number of MCMC iterations
chain = zeros(numIterations, 2); % Initialize the parameter chain
% Initialize the first state of the chain
current_mu = mu_prior;
current_sigma = sigma_prior;
% Define the proposal distribution standard deviations
proposal_mu = 0.5;
proposal_sigma = 0.5;
% Run the MCMC algorithm
for i = 1:numIterations
% Propose new parameters
proposed_mu = normrnd(current_mu, proposal_mu);
proposed_sigma = normrnd(current_sigma, proposal_sigma);
% Calculate the likelihood of the proposed parameters
likelihood_current = prod(normpdf(temperature_data, current_mu, current_sigma));
likelihood_proposed = prod(normpdf(temperature_data, proposed_mu, proposed_sigma));
% Calculate the prior probability of the proposed parameters
prior_current = normpdf(current_mu, mu_prior, sigma_prior) * normpdf(current_sigma, sigma_prior, sigma_prior);
prior_proposed = normpdf(proposed_mu, mu_prior, sigma_prior) * normpdf(proposed_sigma, sigma_prior, sigma_prior);
% Calculate the acceptance ratio
acceptance_ratio = min(1, (likelihood_proposed * prior_proposed) / (likelihood_current * prior_current));
% Accept or reject the proposed parameters
if rand() < acceptance_ratio
current_mu = proposed_mu;
current_sigma = proposed_sigma;
end
% Save the parameters to the chain
chain(i, :) = [current_mu, current_sigma];
end
% Step 4: Generate Uncertain Data
numSamples = 1000; % Number of samples to generate
uncertain_temperatures = normrnd(chain(end, 1), chain(end, 2), numSamples, 1); % Generate uncertain temperature data based on the final sampled parameters
Kindly have a look at the following documentation links to have more information on:
Thanks and regards,
Balavignesh

Community Treasure Hunt

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

Start Hunting!