Generate integer numbers with mean and variance - numbers

How can I sample (integer numbers) from a t-distribution in R when I want to specify the degrees of freedom (df), the mean and variance? For example, how would I sample from a t-distribution with 8 df, mean = 4, and variance = 16?

Related

Matlab: generate random numbers from custom made probability density function

I have a dataset with 3-hourly precipitation amounts for the month of January in the period 1977-1983 (see attachment). However, I want to generate precipitation data for the period 1984-1990 based upon these data. Therefore, I was wondering if it would be possible to create a custom made probability density function of the precipitation amounts (1977-1983) and from this, generate random numbers (precipitation data) for the desired period (1984-1990).
Is this possible in Matlab and could someone help me by doing so?
Thanks in advance!
A histogram will give you an estimate of the PDF -- just divide the bin counts by the total number of samples. From there you can estimate the CDF by integrating. Finally, you can choose a uniformly distributed random number between 0 and 1 and estimate the argument of the CDF that would yield that number. That is, if y is the random number you choose, then you want to find x such that CDF(x) = y. The value of x will be a random number with the desired PDF.
If you have 'Statistics and Machine Learning Toolbox', you can evaluate the PDF of the data with 'Kernel Distribution' method:
Percip_pd = fitdist(Percip,'Kernel');
Then use it for generating N random numbers from the same distribution:
y = random(Percip_pd,N,1);
Quoting #AnonSubmitter85:
"estimate the CDF by integrating. Finally, you can choose a uniformly
distributed random number between 0 and 1 and estimate the argument of
the CDF that would yield that number. That is, if y is the random
number you choose, then you want to find x such that CDF(x) = y. The
value of x will be a random number with the desired PDF."
%random sampling
N=10; %number of resamples
pdf = normrnd(0, 1, 1,100); %your pdf
s = cumsum(pdf); %its cumulative distribution
r = rand(N,1); %random numbers between 0 and 1
for ii=1:N
inds = find(s>r(ii));
indeces(ii)=inds(1); %find first value greater than the random number
end
resamples = pdf(indeces) %the resamples

How to calculate normalized euclidean distance on two vectors?

Let's say I have the following two vectors:
x = [(10-1).*rand(7,1) + 1; randi(10,1,1)];
y = [(10-1).*rand(7,1) + 1; randi(10,1,1)];
The first seven elements are continuous values in the range [1,10]. The last element is an integer in the range [1,10].
Now I would like to compute the euclidean distance between x and y. I think the integer element is a problem because all other elements can get very close but the integer element has always spacings of ones. So there is a bias towards the integer element.
How can I calculate something like a normalized euclidean distance on it?
According to Wolfram Alpha, and the following answer from cross validated, the normalized Eucledean distance is defined by:
You can calculate it with MATLAB by using:
0.5*(std(x-y)^2) / (std(x)^2+std(y)^2)
Alternatively, you can use:
0.5*((norm((x-mean(x))-(y-mean(y)))^2)/(norm(x-mean(x))^2+norm(y-mean(y))^2))
I would rather normalise x and y before calculating the distance and then vanilla Euclidean would suffice.
In your example
x_norm = (x -1) / 9; % normalised x
y_norm = (y -1) / 9; % normalised y
dist = norm(x_norm - y_norm); % Euclidean distance between normalised x, y
However, I am not sure about whether having an integer element contributes to some sort of bias but we have already gotten kind of off-topic for stack overflow :)
From Euclidean Distance - raw, normalized and double‐scaled coefficients
SYSTAT, Primer 5, and SPSS provide Normalization options for the data so as to permit an investigator to compute a distance
coefficient which is essentially “scale free”. Systat 10.2’s
normalised Euclidean distance produces its “normalisation” by dividing
each squared discrepancy between attributes or persons by the total
number of squared discrepancies (or sample size).
Frankly, I can see little point in this standardization – as the final
coefficient still remains scale‐sensitive. That is, it is impossible
to know whether the value indicates high or low dissimilarity from the
coefficient value alone

how to generate n samples from a line segment using Matlab

Given n samples of 100,how do we generate these random samples in the line segment below using matlab
line_segement:
x between -1 and 1, y=2
If you want to generate n random samples between to given limit (in your question -1 and 1), you can use the function rand.
Here an example:
% Define minimum x value
x_min=-1
% Define maximum x value
x_max=1
% Define the number of sample to be generated
n_sample=100
% Generate the samples
x_samples = sort(x_min + (x_max-x_min).*rand(n_sample,1))
In the example, the sort function is called to sort the values in order to have an ascendent series.
x_min and (x_max-x_min) are used to "shift" the series of random values so that it belongs to the desired interval (in this case -1 1), since rand returns random number on an open interval (0,1).
If you want to have a XY matrix composed by the random samples and the defined constant y value (2):
y_val=2;
xy=[x_samples ones(length(x_samples),1)*y_val]
plot([x_min x_max],[y_val y_val],'linewidth',2)
hold on
plot(xy(:,1),xy(:,2),'d','markerfacecolor','r')
grid on
legend({'xy segment','random samples'})
(in the picture, only 20 samples have been plot to make it more clear)
Hope this helps.

How to calculate the norm of quternion in Matlab?

How can I calculate the norm of quaternion in matlab?
I tried this example
a = [1 4 4 -4];
norm = quatnorm(a)
My expected output is 7 but matlab returns 49.
What am I doing wrong?
As #Dan points out, using the native implementation, you are probably getting the square of the formal norm definition. For some reason quatnorm returns the square, after estimating the Euclidean norm (square root of sum of squares).
q = [1 4 4 -4];
MATLABquatnorm:
for index = size(q, 1):-1:1
qnorm(index,:) = norm(q(index,:), 2);
end
qout = qnorm.*qnorm;
Alternative (for vectors):
sqrt(q*q')
This is equivalent to getting sqrt(quatnorm(q)). As you will note above, quatnorm is also adapted to estimate norms for quaternions stored in successive matrix rows (estimates the norm of each row and then squares)
Alternative (for matrices N x 4):
Q = [q; 2*q]; % example
sqrt(diag(Q*Q'))
You can either take square root of the returned number, or you can use function quatmod(q) instead, which calculates the proper Euclidean norm (modulus) of the complex number by not taking square of it.

Interpolation with MATLAB in EMG processing

I have 3 EMG recordings for 2 muscles, with a sampling rate of 1000Hz. In other words I have 3 matrices of EMG data; each has 2 rows (for 2 muscles).
However the number of samples (columns) in each isn't the same: the first one has 2600 samples, the second has 2500 samples and the third one has 2550 samples.
I want to make their lengths the same as each other, to get 3 matrices with the same number of rows and columns. I think it is foolish to cut the bigger ones and use just 2500 columns. However if I want to do so, I don't know whether I should cut from the start or end of them?
Is there a way in MATLAB to interpolate the data to get 3 matrices, each of size 3 x 2600?
All 3 matrices belong to the same movement, and I want to match the samples.
You most likely want to look at using interp1 in this situation. This performs an interpolation between your points so that you can sample at any position on the x-axis.
http://www.mathworks.com/help/matlab/ref/interp1.html
I have the following example which has some random sample data sample1, sample2 and sample3. These variables are of lengths 2600, 2500 and 2550 respectively.
sample1 = exp(2*linspace(0,1,2600)+rand(1, 2600));
sample2 = exp(linspace(0,1,2500)+rand(1, 2500));
sample3 = exp(3*linspace(0,1,2550)+rand(1, 2550));
I have a desired length (I am using a length which corresponds to your shortest sample size)
desiredlength = 2500;
You can then interpolate your data with the following code (note the default is a linear interpolation):
adjusted = zeros(3, desiredlength);
adjusted(1, :) = interp1(linspace(0,1,length(sample1)), sample1, linspace(0,1,desiredlength));
adjusted(2, :) = interp1(linspace(0,1,length(sample2)), sample2, linspace(0,1,desiredlength));
adjusted(3, :) = interp1(linspace(0,1,length(sample3)), sample3, linspace(0,1,desiredlength));
plot(adjusted')
linspace(a, b, n) is a function which gives you a vector of n points between a and b, and for sample1 I am transforming from linspace(0, 1, 2600) to linspace(0, 1, 2500)
I hope this helps.