Generate a heterokskedastic series in MATLAB? - matlab

I am trying to generate a heteroskedastic error term in MATLAB.
I have found a good link to help here, the problem I am having is replicating this in MATLAB.
Here is my attempt:
n = [(1:100) (1:100)]';
sigma2 = n.^(1.3);
t = size(n,1);
for i = 1:200
eps(i) = normrnd(0, sqrt(sigma2(1)));
end
eps = eps'
h = archtest(eps)
However, the test for hetero indicates I still do not have heteroskedastic data, can anyone see where I am going wrong.

Looks like you're keeping sigma2 fixed in the first value inside the loop.
Replace
eps(i) = normrnd(0, sqrt(sigma2(1)));
with
eps(i) = normrnd(0, sqrt(sigma2(i)));

Related

for cycle in copulapdf

I have to perform a copulapdf analysis and i need to create 8 different figures starting from varying nu (in this code called nuvar) from 1 to 8. I'm new in Matlab. I tried to write this for cycle but it doesn't works. Can someone help me please?
for nuvar= 1:1:8
for numvar1= 1:1:8
r=0.5;
nu=1;
u = linspace(0,1,20);
[u1,u2] = meshgrid(u,u);
rho1 = copulaparam('t',r,nu);
H(nuvar, numvar1) = copulapdf('t',[u1(:),u2(:)],rho1,nuvar);
surf(u1,u2,reshape(y,20,20))
end
end
I also tried to correct the script in this way:
r=0.5;
nu=1;
u = linspace(0,1,20);
rho1 = copulaparam('t',r,nu);
[u1,u2] = meshgrid(u,u);
for numvar1= 1:1:8
H(nuvar, numvar1) = copulapdf('t',[u1(:),u2(:)],rho1,nuvar);
surf(u1,u2,reshape(y,20,20))
end
I have the same error "Subscripted assignment dimension mismatch".

Matlab: Working for-loop breaks in parfor while fitting curves

Hoping you may be able to assist me with this error. I am running some code to fit curves to ages using a cross validation regime. I iterate the curve fitting 1000 times to assess the best fit.
I define my models as:
linear_ft = fittype({'x', '1'});
monotonic_ft= fittype({'-1/x', '1'});
quadratic_ft = fittype('poly2');
I then run the following to iterate through different selections of data splitting, recording the residuals following the curve fit...
Data = randn(4,300,10,10);
Ages = randn(300,1);
for thisDim1 = 1:4
for thisDim2 = 1:10
for thisDim3 = 1:10
for nIts = 1:1000
RandomOrder = randperm(300,300);
Fit_Subs = RandomOrder(1:length(Ages)/2); % Take random subs to fit to
Test_Subs = RandomOrder(length(Ages)/2+1:300); % Take random subs to test fit to
Fit_Data = squeeze(Data(thisDim1,Fit_Subs,thisDim2,thisDim3)); % Take data to fit to
Test_Data = squeeze(Data(thisDim1,Test_Subs,thisDim2,thisDim3)); % Take data to test fit
Fit_Ages = Ages;
Fit_Ages(Fit_Subs) = []; %Take ages of Fit Subs only
Test_Ages = Ages;
Test_Ages(Test_Subs) = []; % Take ages of Test Subs only
Nsubs = (length(Ages)/2);
% Model Data using Curves
fFit_Lin = fit(Fit_Ages,Fit_Data',linear_ft);
fFit_Mon = fit(Fit_Ages,Fit_Data',monotonic_ft);
fFit_Quad = fit(Fit_Ages,Fit_Data',quadratic_ft);
% Fit Modelled Data to Test Data
tFit_Lin = fFit_Lin(Test_Ages);
tFit_Mon = fFit_Mon(Test_Ages);
tFit_Quad = fFit_Quad(Test_Ages);
% Calculate Median Residual
Lin_Med_Resid(nIts) = median(tFit_Lin - Test_Data');
Mon_Med_Resid(nIts) = median(tFit_Mon - Test_Data');
Quad_Med_Resid(nIts) = median(tFit_Quad - Test_Data');
end
end
end
end
If you run this with the fourth loop (nIts) as a for-loop it will run. If you run it as a parfor-loop it won't stating the error:
Error using fit>iFit (line 264)
The name 'lower' is not an accessible property for an instance of class
'llsqoptions'.
Error in fit (line 108) [fitobj, goodness, output, convmsg] = iFit(
xdatain, ydatain, fittypeobj, ...
Does anyone have any idea how to fix this? I would be most grateful for any advice!!
Thanks,
Ben
Try restarting MATLAB or typing clear all to see if it clears things up for you.
Your code works for me, but the parallel toolbox can be a bit finicky in my experience.

Matlab - setting up a nested parfor loop

I got trouble setting up a parfor loop in matlab. I know its rather easy but I am a bit stuck here thats why I would appreciate all help.
I have tried the following
valuesforM = zeros(901,100);
valuesforOPratio = zeros(100,1);
counter=1;
x = xlsread ('gtc.xlsx', 'A2:A10000');
y = xlsread ('gtc.xlsx','C2:C10000');
z = xlsread ('gtc.xlsx','B2:B10000');
parfor M = 100:1000;
counter=counter+1
for OPratio = 1:100;
NPVtotal = cut_off_optimisation(M,OPratio,x,y,z);
valuesforOPratio(OPratio)=NPVtotal;
end
valuesforM(M-99,:) = valuesforOPratio;
end
And i get the following error:
Error using senitivity_script_10000_steps (line 10)
Error: The variable valuesforOPratio in a parfor cannot be classified.
How can i fix this ? Thanks a lot.
EDIT
following the commments advice I tried the following:
valuesforM = zeros(901,100);
x = xlsread ('gtc.xlsx', 'A2:A10000');
y = xlsread ('gtc.xlsx','C2:C10000');
z = xlsread ('gtc.xlsx','C2:C10000');
parfor M = 100:1000;
NPVtotal = cut_off_optimisation(M,1:100,x,y,z);
valuesforM(M-99,:) = NPVtotal;
end
which gives the following error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in parforscript (line 8)
parfor M = 100:1000;
Any idea how to solve either of the both problems?
As you said in the comments, since counter is not required I have removed it and also counter won't work like that because different iterations will be running in a non sequential way and trying to update the same variable. Which is not allowed.
Next, you have to do valuesforOPratio = zeros(1,100) inside parfor loop because if you put it outside the loop then each iteration will be trying to access the same variable. This is not allowed. So that is why you are getting that error. When you put it inside, each iteration will be accessing a locally created variable. That is also the reason why you don't find it in base workspace. Here is the corrected code.
valuesforM = zeros(901,100);
x = xlsread ('gtc.xlsx', 'A2:A10000');
y = xlsread ('gtc.xlsx','C2:C10000');
z = xlsread ('gtc.xlsx','B2:B10000');
parfor M = 100:1000;
valuesforOPratio = zeros(1,100);
for OPratio = 1:100;
NPVtotal = cut_off_optimisation(M,OPratio,x,y,z);
valuesforOPratio(OPratio)=NPVtotal;
end
valuesforM(M-99,:) = valuesforOPratio;
end

Error creating a plot in MATLAB using values I got from excel using actxserver.

Hello so I am attempting to write a MATLAB script that reads multiple excel files at ones and gathers values from the worksheet. through some research I found that using actxserver is a very quick way to get the data. The values are stored in the out and out1 variables and by using vertcat function I create an array that has all the 300,000 values that I need to store. This seems to work. However when I try to plot the values in a simple x vs y graph using plot I get an error that states:
Error using plot
Not enough input arguments.
Error in opener (line 36)
plot(out0,out1)
Below is the code. I am sure it is a simple fix but I have looked and not found anything helpful.
clc;
filename = 'C:\Users\Public\Documents\DASYLab\13.0.0\eng\data\TRIAL\';
D = dir([filename, '\*.csv']);
Num = length(D(not([D.isdir])));
f = Num;
ex = actxserver('excel.application');
o = 0;
k = 0;
for i=1:f
if(i<=10)
ex.Workbooks.Open([filename,'mydataIII_0',num2str(i-1)]);
else
ex.Workbooks.Open([filename,'mydataIII_',num2str(i-1)]);
end
out0 = get(ex.Range('A8:A40967'),'Value');
out = vertcat(o,out0);
o = out;
out1 = get(ex.Range('C8:C40967'),'Value');
outone = vertcat(k,out1);
k = outone;
end
figure;
plot(out0,out1)
ex.Quit
Hello sorry I but I figured it out. The issue was that when the get function was used to get the value it created a cell array.
http://www.mathworks.com/help/matlab/cell-arrays.html
if you want to convert the data to a ordinary array one can use the cell2mat() function

sprintf confusion (Matlab)

Quick question,
I would like to make a count from 50-70 using sprintf in Matlab. This example prints 0101-0120
for i = 1:20
filename = sprintf('Brain_01%02d.dcm', i);
[X(:,:,1,i), amap] = dicomread(filename);
end
How would I change this to print 0151-0170?
The answer seemed obvious at first, but it seems like another issue might be related to the indexing of X getting broken if i doesn't start at one. Here's one way to address that while handling pre-allocation of X,
imgInds = 151:170;
di = dicominfo(sprintf('Brain_%04d.dcm',imgInds(1)));
X = zeros(di.Height,di.Width,1,numel(imgInds),class(dicomread(di))); % modify
for i = 1:numel(imgInds),
filename = sprintf('Brain_%04d.dcm', imgInds(i));
[X(:,:,1,i), amap] = dicomread(filename);
end
For clarity, I think it is better to build your sprintf with %04d instead of 01%02d. You should set the size of X accordingly on the line labeled modify, particularly the third dimension since I assume your actual code will not have this be 1.
I'm guessing this should do it:
for i = 51:70
filename = sprintf('Brain_01%02d.dcm', i);
[X(:,:,1,i), amap] = dicomread(filename);
end
Thank you for your responses! Actually all I needed to do (for my purposes) was:
for i = 1:20
filename = sprintf('Brain_01%02d.dcm', i + 49);
[X(:,:,1,i), amap] = dicomread(filename);
end
which made the count start from 50.