I can't understand something about mat lab coding - matlab

generate a scatter-plot of log-normally distributed values of at
least 1000 random numbers in the interval [5;50] derived from a uniform distribution over the interval
[0;1].
I have done this so far
%declaring intervals
a=5;b=50;
%creating 1000 random number within the interval [5,50]
r = a + (b-a).*rand(1000,1);
I can not understand this part "derived from a uniform distribution over the interval
[0;1]."

rand gives you a pseudorandom number between 0 and 1. The probability of selecting any particular number form this interval is the same for any other number from the interval, hence the probability density function used by rand is called "uniformly distributed".
So, you started out fine :)

I would take this to mean that you should map the range of numbers you want as output into the interval [0,1]. That is to say you partition the interval into 46 equally spaced subintervals, such that the intersection of each interval is empty and their union is [0,1]. Then to each subinterval, you associate a value of your output sample range then a sample from [0,1] lets you create a random sample from the set [5:50]
For example, if you wanted to create a sample from [1:10] you would choose generate a sample from uniform distribution on [0,1]. If the sample is in the interval [0,0.1) your output sample value is 1. If it was in the interval [0.2,0.3) your value would be 3 etc.
If your is asking what a uniform distribution actually is, it is simply a distribution where every point in the sample space has an equal probability of being chosen. See this for more details http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29

Related

Normal distribution for a given range of numbers in Matlab

My question is about estimating a normal distribution for a row of points of an interval that are extracted from the data, just knowing the maximum = 200 and minimum = 100, to distribute them randomly by an average and an associated standard deviation that we will estimate. Is there a method in Matlab, or the literature to do it, such that when you draw the associated law; we must not exceed the min and max values I used this trick there:
x = xmin + rand (1, n) * (xmax-xmin);
but I do not know how to extract the parameters of the normal distribution (the mean, the standard deviation).
I'm looking for the mean and the std.

Plotting x-axis and y-axis with different (indep) limits in Matlab

I developed an Android app such that each scan is set to 1 Minute, and during this time the sensor collects many many readings randomly. I want to plot one sensor data of one scan only as follows:
The time of the scan is put manually in seconds for only 1 minute (from 1:60 sec) in the x-axis. While the vector of random readings collected from the sensor (sometimes reach hundreds of values) in the y-axis.
How I can do this in Matlab?
I tried using this code but gives me an error. "Vectors must be the same length."
This is my code:
x1 = linspace(0,60);
plot(x1,vector1,'o-r',x1,vector2,'+-k','LineWidth',lw,'MarkerSize',msz);
xlabel('Time (s)');
ylabel('sensor readings')
In order to match the amount of values you have to modify the input for linspace:
x1 = linspace(0,60,length(vector1));
This way you will automatically get the right amount of entries for your x-axis vector.
You basically tell linspace to create a vector that ranges from 0 to 60 with length(vector1) entries, so that it matches the length of your data set.
Note that if your second data set has a different amount of entries as your first, you will need to create a different x-axis vector that respectively matches its amount of values.

Matlab function that generates random real numbers in a closed interval

It's there any function in Matlab that generates random real numbers in a closed interval. I found something with unifrnd() but it's generating numbers in an open interval.
If I use unifrnd(x,y); I get (x,y) interval, instead of [x,y].
Given the discussion of accuracy in the comments, you could use something like:
mag = floor(log10( y - x))
num = unifrnd(x-(10^mag)*eps, y+(10^mag)*eps)
This essentially adds one "point" to the discrete interval representation, taking into account the accuracy based on the size of the numbers you're using. unifrnd() is essentially a wrapper around rand() (which means you don't really need the stats toolbox to do this), and thus it is really just scaling the uniform distribution on (0,1). If you're worried about the endpoints though, that matters, because you can't get more granular than the product the magnitude of your interval length with eps.

Dividing a normal distribution into regions of equal probability in Matlab

Consider a Normal distribution with mean 0 and standard deviation 1. I would like to divide this distribution into 9 regions of equal probability and take a random sample from each region.
It sounds like you want to find the values that divide the area under the probability distribution function into segments of equal probability. This can be done in matlab by applying the norminv function.
In your particular case:
segmentBounds = norminv(linspace(0,1,10),0,1)
Any two adjacent values of segmentBounds now describe the boundaries of segments of the Normal probability distribution function such that each segment contains one ninth of the total probability.
I'm not sure exactly what you mean by taking random numbers from each sample. One approach is to sample from each region by performing rejection sampling. In short, for each region bounded by x0 and x1, draw a sample from y = normrnd(0,1). If x0 < y < x1, keep it. Else discard it and repeat.
It's also possible that you intend to sample from these regions uniformly. To do this you can try rand(1)*(x1-x0) + x0. This will produce problems for the extreme quantiles, however, since the regions extend to +/- infinity.

Bootstrap and asymmetric CI

I'm trying to create confidence interval for a set of data not randomly distributed and very skewed at right. Surfing, I discovered a pretty rude method that consists in using the 97.5% percentile (of my data) for the upperbound CL and 2.5% percentile for your lower CL.
Unfortunately, I need a more sophisticated way!
Then I discovered the bootstrap, precisley the MATLAB bootci function, but I'm having hard time to undestand how to used it properly.
Let's say that M is my matrix containing my data (19x100), and let's say that:
Mean = mean(M,2);
StdDev = sqrt(var(M'))';
How can I compute the asymmetrical CI for every row of the Mean vector using bootci?
Note: earlier, I was computing the CI in this very wrong way: Mean +/- 2 * StdDev, shame on me!
Let's say you have a 100x19 data set. Each column has a different distribution. We'll choose the log normal distribution, so that they skew to the right.
means = repmat(log(1:19), 100, 1);
stdevs = ones(100, 19);
X = lognrnd(means, stdevs);
Notice that each column is from the same distribution, and the rows are separate observations. Most functions in MATLAB operate on the rows by default, so it's always preferable to keep your data this way around.
You can compute bootstrap confidence intervals for the mean using the bootci function.
ci = bootci(1000, #mean, X);
This does 1000 resamplings of your data, calculates the mean for each resampling and then takes the 2.5% and 97.5% quantiles. To show that it's an asymmetric confidence interval about the mean, we can plot the mean and the confidence intervals for each column
plot(mean(X), 'r')
hold on
plot(ci')