Matlab and normal realizations - matlab

I have wrote this code:.
N=10000; % number of experiments
o= 1000+randn(1,N)*sqrt(10^4); % random normal distribution with mean 1000 and variance 10^4
b=700:50:1300; % specify the number of bins (possible values of the realizations)
prob=hist(o,b)/N %create ad histogram
X=[700:50:1300]
**
Now, how I can create a matrix which contains the values of b and prob?
In other words, I want a matrix of this kind:
matrix=[ value of X(i) ; probability associate at the value of X(i) ]
es: matrix=[... X(i)=850... ; ... prob(X(i)=850).. ]
Thank you a lot! ;)

I think you want the probabilities of the intervals for which the histogram is computed:
N = 100000; %// number of experiments
b = 700:50:1300; %// bin centers
mu = 1000; %// mean of distribution
sigma = 100; %// standard deviation of distribution
delta = (b(2)-b(1))/2; %// compute bin half-width
pb = normcdf(b+delta,mu,sigma)-normcdf(b-delta,mu,sigma); %// compute probability
Check:
o = mu + sigma*randn(1,N);
hist(o, b)
hold on
plot(b, N*pb, 'r', 'linewidth', 2)

Related

Plotting two sets of data on one histogram

I have just began using Octave and am attempting to simulate 10,000 outcomes of a Binomial Random Variable defined as:
X ~ Bi(5, 0.2)
I have graphed the result with the following function:
function x = generate_binomial_bernoulli(n,p,m)
% generate Bi(n, p) outcomes m times
x = zeros(1,m); % allocate array for m simulations
for i = 1:m % iterate over m simulations
successes = 0; % count the number of successful trials per simualtion (0-5)
for j = 1:n % iterate through the n trials
u = rand; % generate random nuumber from 0-1
if (u <= p) % if random number is <= p
successes++; % count it as a success
endif
end
x(i) = successes; % store the number of successful trials in this simulation
end
alphabet_x=[0:n]; % create an array from 0 to n
hist(x,alphabet_x); % plot graph
end
I then call the function with generate_binomial_bernoulli(5, 0.2, 10000).
This is simulating 5 Bernoulli Trials, each with a probability of success of 0.2, repeating the 5 trials 10,000 times and graphing the distribution of the number of successes. This plot shows the empirical results of the simulation.
I have now also been asked to plot the theoretical results, which my best guess would be a normally distributed plot around 1 success on the x-axis (0.2 * 5 = 1).
How could I create this plot, and display it on the same histogram?
How could I correclty display my graph with the x-axis spanning just from 0 to 5, both axes labelled, and the two histograms colour-coded with a legend?
EDIT
Here is my current function where I have tried to plot the normalized / theoretical curve:
function x = generate_binomial_bernoulli(n,p,m)
% generate Bi(n, p) outcomes m times
emperical = zeros(1,m); % allocate array for m simulations
for i = 1:m % iterate over m simulations
successes = 0; % count the number of successful trials per simualtion (0-5)
for j = 1:n % iterate through the n trials
u = rand; % generate random nuumber from 0-1
if (u <= p) % if random number is <= p
successes++; % count it as a success
endif
end
emperical(i) = successes; % store the number of successful trials in this simulation
end
close all; % close any existing graphs
x_values = [0:n]; % array of x-axis values
hist(emperical, x_values, "facecolor", "r"); % plot empirical data
xlim([-0.5 (n + 0.5)]); % set x-axis to allow for histogram bar widths
hold on; % hold current graph
mean = n * p; % theoretical mean
norm = normpdf(x_values, mean, 1); % normalised y values
plot(x_values, norm, "color", "b"); % plot theoretical distribution
legend('Emprical', 'Theoretical');
end
As shown below, this curve only extends to a very low height along the y-axis, but I do not know how to span it across the entire dataset.
Get histogram number counts and bins:
[counts,centers] = hist(x);
Normalize:
freq = counts/sum(counts);
Draw normalized histogram:
bar(centers,freq)

random number with p(x)= x^(-a) distribution

How can I generate integer random number within [a,b] with below distribution in MATLAB:
p(x)= x^(-a)
I want the distribution to be normalized.
For continuous distributions: Generate random values given a PDF
For discrete distributions, as later it was specified in the OP:
The same rationale can be used as for continuous distributions: inverse transform sampling.
So from mathematical point of view there is no difference, the Matlab implementation however is different. Here is a simple solution with your distribution function:
% for reproducibility
rng(333)
% OPTIONS
% interval endpoints
a = 4;
b = 20;
% number of required random draws
n = 1e4;
% CALCULATION
x = a:b;
% normalization constant
nc = sum(x.^(-a));
% if a and b are finite it is more convinient to have the pdf and cdf as vectors
pmf = 1/nc*x.^(-a);
% create cdf
cdf = cumsum(pmf);
% generate uniformly distributed random numbers from [0,1]
r = rand(n,1);
% use the cdf to get the x value to rs
R = nan(n,1);
for ii = 1:n
rr = r(ii);
if rr == 1
R(ii) = b;
else
idx = sum(cdf < rr) + 1;
R(ii) = x(idx);
end
end
%PLOT
% verfication plot
f = hist(R,x);
bar(x,f/sum(f))
hold on
plot(x, pmf, 'xr', 'Linewidth', 1.2)
xlabel('x')
ylabel('Probability mass')
legend('histogram of random values', 'analytical pdf')
Notes:
the code is general, just replace the pmf with your function;
it is strange that the same parameter a appears in the distribution function and in the interval too.

Creating a sparse matrix in Matlab with a specified number of independent Bernoulli +-1 nonzero entries

How in Matlab we can form a matrix X, 1000 by 1000, which is sparse with, say,
5% of independent Bernoulli +-1 nonzero entries?
I.e. such a matrix would have rho = ||X||_0/10^6 = 0.05.
Randomly choose 5% of elements
n = numel(X);
ind = randi(n, round(.05*n), 1);
Assign these elements with random variable
X(ind) = binornd(1, .5, length(ind), 1) *2-1;
Check binornd's documentation for more details.
To avoid duplicate randi numbers, you can use randsample from the Statistics Toolbox, or something like randperm as mentioned in this post, or something like
EDIT
ind = [];
t0 = round(.05*n);
t1 = length(ind);
while t1 < t0
ind(end+1:t0) = randi(n, t0-t1, 1);
ind = unique(ind);
t1 = length(ind);
end
If you need to build the matrix as sparse (in Matlab's sense):
M = 1000; %// number of rows
N = 1000; %// number of columns
perc = 5/100; %// percentage (fraction) of +/-1 entries
n = round(M*N*perc); %// compute number of nonzero entries
nz = 2*(rand(1,n)<.5)-1; %// generate nonzero entries: +/-1 with .5 probability
ind = randsample(M*N,n); %// choose linear indices of nonzero entries
X = sparse(ind, 1 ,nz , M*N, 1, n); %// build matrix as linearized
X = reshape(X,M,N); %// put into shape

Obtaining sigma of a Normal distibution in Matlab

I have the mean and the percentile 60 of a Normal Distribution. I need to obtain a random array of numbers form it with those two values in Matlab.
Is this somehow posible?
Thanks
I'm not exactly sure of what your looking. I understood that you want to know the standard deviation for a certain value of a quantile at 0.6 (percentile = quantile * 100)?
From wikipedia. (look at the quantile function for a mean μ and variance σ2).
From this you have all you need:
p = 0.6
F^-1(p) is your percentile
erf^{-1} is erfinv
Get the \sigma it's your standard deviation and then to generate your vector simply use x=randn(nbSamples,1)*sigma + mean
m = 2; %// number of rows
n = 5; %// number of columns
mu = 3.2; %// mean
p60 = 0.4; %// 60-percentile
sigma = p60 / norminv(.6); %// compute sigma from p60
result = mu + sigma*randn(m,n); %// random numbers with desired distribution
%// Or: result = normrnd(mu, sigma, m, n);

Integration of normal probability distribution function with random numbers

function Y=normpdf(X)
syms X
Y = normpdf(X);
int(Y,X,1,inf)
end
I need to integrate normal pdf function from 1 to infinity for the case of N=100 where N is the total numbers generated.I know i need to use randn() for generating random numbers but i dont know how to use it in this situation.
You could have N = 100 random numbers from t = randn(N, 1);. First, we sort with t = sort(t), then the integrated PDF, i.e. cumulative density function is approximated by your samples with p = (1 : N) / N for t as you can see with plot(t, p). It will overlap well with hold on, plot(t, normcdf(t), 'r').
A perhaps more intuitive approach is to partition the x axis into bins in order to estimate the CDF:
N = 100; % number of samples
t = randn(N, 1); % random data
x = linspace(-10,10,200); % define bins
estim_cdf = mean(bsxfun(#le, t, x)); % estimate CDF
plot(x, estim_cdf);
hold on
plot(x, normcdf(x), 'r')
Note that #s.bandara's solution can be interpreted as the limiting case of this as the number of bins tends to infinity, and therefore it probably gives more accurate results.