In order to generate Autoregressive model, we have the aryule() command and we can also use filtersEstimating AR model. But how do I generate MA model? For instance, can somebody please show how to generate MA(20) model? I could not find any appropriate technique to do so. The noise is generated from a nonlinear map
epsilon(1) = 0.01;
for i =1 : N
epsilon(i+1) = 4*epsilon(i)*(1-epsilon(i));
end
So, the MA model will regress over epsilon terms.
Q1: Shall be extremely helpful if the code and functional form of an MA model is shown preferably MA(20) using the above noise model.
Q2: This is how I generated an AR(20) using random noise but don't know how to use the above equation as the noise instead of using rand for both MA and AR
%Generate sine wave = A*sin(2*pi*f*t + phi)
t = linspace(0,1,1000);
A = 5;
f = 2;
phi = pi/8;
sinewave = A*sin(2*pi*f*t + phi);
noisy_sine=sinewave+0.5*randn(size(t));
subplot(1,2,1);
plot(t, sinewave)
hold on;
subplot(1,2,2);
plot(t,noisy_sine);
%Generate AR model(20)
order =20;
ARCoeff = aryule(noisy_sine,order);
I had a same problem. The following links can help :
https://www.mathworks.com/examples/econometrics/mw/econ-ex18477389-simulate-an-ma-process
Simulate an MA Process
This example shows how to simulate sample paths from a stationary MA(12) process without specifying presample observations.
Contents
Specify a model.
Generate sample paths.
Plot the simulation variance.
Step 1. Specify a model.
Specify the MA(12) model
where the innovation distribution is Gaussian with variance 0.2.
model = arima('Constant',0.5,'MA',{0.8,0.2},...
'MALags',[1,12],'Variance',0.2);
Step 2. Generate sample paths.
Generate 200 sample paths, each with 60 observations.
rng('default')
Y = simulate(model,60,'NumPaths',200);
Step 3. Plot the simulation variance.
figure
plot(Y,'Color',[.85,.85,.85])
hold on
h = plot(mean(Y,2),'k','LineWidth',2)
legend(h,'Simulation Mean','Location','NorthWest')
title('MA(12) Process')
hold off
You can do that with one line of code using filter.
Let us talk about AR and MA in the Laplace domain. Say that your have a transfer function H = B/A, where B and A are polynomials. B have the coefficients of your MA and A will have your AR coefficients. In equations:
Thus, given your input signal x, you can use Matlab's y=filter(B,A,x) to generate your AR, MA and ARMA processes. The documentation about filter is here. For your specific case of a pure MA model, just make B=1 and your A as a vector with 20 elements.
You probably already know that, but just in case and to help future readers here is a link with a review on how to transform your difference equations to Laplace domain. TL;DR:
Related
This line of code is supposed to generate exponential service times, but I am not able to get the logic behind it.
% Exponential service time with rate 1
mean = 1;
dt = -mean * log(1 - rand());
This is the source link, but MATLAB is needed to open the example.
I was also thinking if exprnd(1) will give the same result of generating numbers from the exponential distribution that has a mean of 1?
You are right!
First, note that MATLAB parameterizes the Exponential distribution by the mean, not the rate, so exprnd(5) would have a rate lambda = 1/5.
This line of code is another way to do the same thing:
-mean * log(1 - rand());
This is the inverse transform for the Exponential distribution.
If X follows an Exponential distribution, then
and rewriting the cumulative distribution function (CDF) and letting U ~ Uniform(0,1), we can derive the inverse transform.
Note the last equality is because 1-U and U are equal in distribution. In other words, 1-U ~ Uniform(0,1) and U ~ Uniform(0,1).
You can test this yourself with this example code with multiple approaches.
% MATLAB R2018b
rate = 1; % mean = 1 % mean = 1/rate
NumSamples = 1000;
% Approach 1
X1 = (-1/rate)*log(1-rand(NumSamples,1)); % inverse transform
% Approach 2
X2 = exprnd(1/rate,NumSamples,1);
% Approach 3
pd = makedist('Exponential',1/rate) % create probability distribution object
X3 = random(pd,NumSamples,1);
EDIT: The OP asked is there was a reason to generate from the CDF rather than from the probability density function (PDF). This is my attempt to answer that.
The inverse transform method uses the CDF to take advantage of the fact that the CDF is itself a probability and so must be on the interval [0, 1]. Then it is very easy to generate very good (pseudo) random numbers which will be on that interval. The CDF is sufficient to uniquely define the distribution, and inverting the CDF means that its unique "shape" will properly map the uniformly distributed numbers on [0, 1] to a non-uniform shape in the domain which will follow the probability density function (PDF).
You can see the CDF performing this nonlinear mapping in this figure.
One use of the PDF would be Acceptance-Rejection methods, which can be useful for some distributions including custom PDFs (thanks to #pjs for jogging my memory).
I'm a newbie to MATLAB and now I'm trying to create a 2-d gaussian distribute data to train my neural network. I just found the code on the official document.
mu = [0 0];
Sigma = [.25 .3; .3 1];
x1 = -3:.2:3; x2 = -3:.2:3;
[X1,X2] = meshgrid(x1,x2);
F = mvnpdf([X1(:) X2(:)],mu,Sigma);
I know "mu" is average of the data. Sigma is something related to
Standard deviation. But I just don't get what is the idea of mesgrid and the interval(x1,x2). And the Geometric meaning of these code.
Also, can someone explain me why is guassian distribution so important in machine learning and data science? Cause all the course keep saying and saying this term.
Meshgrid is a basic matlab function, that is in no way specifically related to neural networks or a gaussian distribution. Check the documentation of Matlab to find out more about it.
The gaussian distribution (also known as normal distribution) is important for datascience because it comes with several nice statistical properties. Unfortunately it is hard to describe them all in a compact way, and this would also not be a question about programming, but more about statistics.
I think the code you provide seems confusing to you because you expect it to generate samples whereas it merely returns values of the Gaussian PDF (probability density function) for some given pairs of (x1,x2).
For example F = mvnpdf(a,b,mu, Sigma) returns the probability of x1=a and x2=b given that they follow a multivariate Gaussian distribution with mean mu and covariance matrix Sigma.
Being in Stack Overflow, I am focusing on the Matlab aspect of your question: for generating 100 samples of a 2-D Gaussian you can use something like the following (taken from the Matlab help of randn function):
mu = [1 2];
Sigma = [1 .5; .5 2];
R = chol(Sigma);
z = repmat(mu,100,1) + randn(100,2)*R;
The array z = [x1,x2] contains the x1 and x2 vectors that you are looking for.
Some statistics textbook or wikipedia could convince you on why the above code indeed generates such samples. The last line of code is related to one of the nice properties of a Gaussian distribution (or any other elliptical distribution).
I am currently fiddling with multivariate kernel density estimations for estimating the probability density functions (PDF) of hydrological data sets using Matlab. I am most familiar with kernel density estimation using Gaussian kernels as outlined in Sharma (2000 and 2014) (where the kernel bandwidths are set using the Gaussian Reference Rule (GRR)). The GRR is written as follows (Sharma, 2000):
where lambda_ref = GRR bandwidth of kernel, n is the sample size, and d is the dimension of the data set we are using for density estimation. To estimate the multivariate density of our data set X we use the following formula (Sharma, 2000):
where lamda is the same as lamda_ref above, S is the sample covariance of X and det() stands for determinant.
My question is: I understand that there are many "fast" methods for calculating the Gaussian kernel function represented by the term exp() such as the method proposed here (using Matlab): http://mrmartin.net/?p=218. Since I will be working with data sets that are quite large in sample size (1000-10,000) I am looking for a fast code. Is anyone aware how I can write a fast code for the second equation that takes into account the inverse of the sample covariance matrix (S^-1)?
I greatly appreciate any help that can be provided on this issue. Thank you!
Note(s):
I understand that there is a Matlab code for calculating the second equation, found as a sub-function in: http://www.mathworks.com/matlabcentral/fileexchange/29039-mutual-information-2-variablle/content/MutualInfo.m. However this code has a bottleneck in how it calculates the kernel matrix.
References:
1 A. Sharma, Seasonal to interannual rainfall probabilistic forecasts for improved water supply management: Part 3 — A nonparametric probabilistic forecast model, Journal of Hydrology, Volume 239, Issues 1–4, 20 December 2000, Pages 249-258, ISSN 0022-1694, http://dx.doi.org/10.1016/S0022-1694(00)00348-6.
2 Sharma, A., and R. Mehrotra (2014), An information theoretic alternative to model a natural system using observational information alone, Water Resour. Res., 50, 650–660, doi:10.1002/2013WR013845.
I have found a code that I am able to modify for my purposes. The original code is listed at the following link: http://www.kernel-methods.net/matlab/kernels/rbf.m.
Code
function K = rbf(coord,sig)
%function K = rbf(coord,sig)
%
% Computes an rbf kernel matrix from the input coordinates
%
%INPUTS
% coord = a matrix containing all samples as rows
% sig = sigma, the kernel width; squared distances are divided by
% squared sig in the exponent
%
%OUTPUTS
% K = the rbf kernel matrix ( = exp(-1/(2*sigma^2)*(coord*coord')^2) )
%
%
% For more info, see www.kernel-methods.net
%
%Author: Tijl De Bie, february 2003. Adapted: october 2004 (for speedup).
n=size(coord,1);
K=coord*coord'/sig^2;
d=diag(K);
K=K-ones(n,1)*d'/2;
K=K-d*ones(1,n)/2;
K=exp(K);
Modified Code incorporating sample covariance scaling:
xcov = cov(x.'); % sample covariance of the data
invxc = pinv(xcov); % inversion of data sample covariance
coord = x.';
sig = sigma; % kernel bandwidth
n = size(coord,1);
K = coord*invxc*coord'/sig^2;
d = diag(K);
K = K-ones(n,1)*d'/2;
K = K-d*ones(1,n)/2;
K = exp(K); % kernel matrix
I hope this helps someone else looking into the same problem.
I have a vector of x and y coordinates drawn from two separate unknown Gaussian distributions. I would like to fit these points to a three dimensional Gauss function and evaluate this function at any x and y.
So far the only manner I've found of doing this is using a Gaussian Mixture model with a maximum of 1 component (see code below) and going into the handle of ezcontour to take the X, Y, and Z data out.
The problems with this method is firstly that its a very ugly roundabout manner of getting this done and secondly the ezcontour command only gives me a grid of 60x60 but I need a much higher resolution.
Does anyone know a more elegant and useful method that will allow me to find the underlying Gauss function and extract its value at any x and y?
Code:
GaussDistribution = fitgmdist([varX varY],1); %Not exactly the intention of fitgmdist, but it gets the job done.
h = ezcontour(#(x,y)pdf(GaussDistributions,[x y]),[-500 -400], [-40 40]);
Gaussian Distribution in general form is like this:
I am not allowed to upload picture but the Formula of gaussian is:
1/((2*pi)^(D/2)*sqrt(det(Sigma)))*exp(-1/2*(x-Mu)*Sigma^-1*(x-Mu)');
where D is the data dimension (for you is 2);
Sigma is covariance matrix;
and Mu is mean of each data vector.
here is an example. In this example a guassian is fitted into two vectors of randomly generated samples from normal distributions with parameters N1(4,7) and N2(-2,4):
Data = [random('norm',4,7,30,1),random('norm',-2,4,30,1)];
X = -25:.2:25;
Y = -25:.2:25;
D = length(Data(1,:));
Mu = mean(Data);
Sigma = cov(Data);
P_Gaussian = zeros(length(X),length(Y));
for i=1:length(X)
for j=1:length(Y)
x = [X(i),Y(j)];
P_Gaussian(i,j) = 1/((2*pi)^(D/2)*sqrt(det(Sigma)))...
*exp(-1/2*(x-Mu)*Sigma^-1*(x-Mu)');
end
end
mesh(P_Gaussian)
run the code in matlab. For the sake of clarity I wrote the code like this it can be written more more efficient from programming point of view.
I am currently studying DSP and i'm using the Matlab software package to work my way through the the problems. I am currently just starting to attempt to learn about the fourier series and am having trouble with the following problem.
Generate an 100hz triangle wave using Fourier Series.
Now, i cant quite understand this part of the problem about using the fourier series.
I have generated a 100hz triangle wave with the following matlab code:
t = 0:1/10000:1;
f=100;
x1 = sawtooth(2*pi*f*t, 0.5);
x2 = fft(x1);
plot(t,x1);
axis([0 0.10 -1 1]);
grid on;
Now what code would i use within matlab to plot the fourier series of this triangle wave?
Thanks to anyone who may have some input for this particular problem.
I think what the question is asking is for you to figure out the 'a' and 'b' coefficients and then generate the sawtooth wave by summing sines and cosines at the appropriate frequencies. It's not too hard to find the Fourier coefficients for a sawtooth wave online, but I encourage you to work it out and use that to check your answer :)
Then do something like this
n_harmonics = 10;
n = zeros(1, n_harmonics);
a = ?; % for you to figure out - probably a function of n
b = ?; % same idea
t = linspace(0, 2*pi);
x = zeros(size(t));
for nx = 1 : n,
x = x + a(nx)*cos(nx*t) + b(nx)*sin(nx*t);
end
plot(t, x)
Note the Fourier series is not the same thing as the Fourier transform, which is what fft is estimating. Most texts on signal processing will start with the Fourier series and build on that to get to the Fourier transform. Note also that there are tons of important and subtle differences when moving from continuous time to discrete time. Again, most textbooks will probably start with continuous time and then use that as a basis to introduce the discrete-time concepts.