Normcdf() input arguments - matlab

I am trying to solve:
Use the technique of importance sampling via tilted densities to
estimate the quantity where X is a standard normal random variable.
Generate estimates for a = 1, 2, 3, 4, 5. Use sample sizes 1e3, 1e5,
and 1e7 in each estimation.
theta=Pr{X>a}
and my teacher gave the example code:
n2=1e7;
for a2=1:5
x2=rand(1,n2(1))+a2;
theta2(a2)=mean((x2>a2).*exp(a2^2/2-a2*x2));
end
tp2=1-normcdf(1:5);
abs(tp2-theta2)./tp2
but when I attempt to run it I get:
Not enough input arguments.
Error in normcdf (line 16) cdf = 0.5 * erfc(-(x-mu)/(sigma*sqrt(2)));
Error in HW10 (line 18) tp2=1-normcdf(1:5);
But he didn't have any errors come up in class. I can't see what I am missing to run this.

Related

Matlab feature selection

I am trying to learn relevant features in a 300*299 training matrix by taking a random row from it as my test data and applying sequentialfs on it. I have used the following code:
>> Md1=fitcdiscr(xtrain,ytrain);
>> func = #(xtrain, ytrain, xtest, ytest) sum(ytest ~= predict(Md1,xtest));
>> learnt = sequentialfs(func,xtrain,ytrain)
xtrain and ytrain are 299*299 and 299*1 respectively. Predict will give me the predicted label for xtest(which is some random row from original xtrain).
However, when I run my code, I get the following error:
Error using crossval>evalFun (line 480)
The function '#(xtrain,ytrain,xtest,ytest)sum(ytest~=predict(Md1,xtest))' generated the following error:
X must have 299 columns.
Error in crossval>getFuncVal (line 497)
funResult = evalFun(funorStr,arg(:));
Error in crossval (line 343)
funResult = getFuncVal(1, nData, cvp, data, funorStr, []);
Error in sequentialfs>callfun (line 485)
funResult = crossval(fun,x,other_data{:},...
Error in sequentialfs (line 353)
crit(k) = callfun(fun,x,other_data,cv,mcreps,ParOptions);
Error in new (line 13)
learnt = sequentialfs(func,xtrain,ytrain)
Where did I go wrong?
You should build your classifier inside func, not before.
sequentialfs calls the function each time on different sets, and a classifier must be built specifically for each set, using only the features sequentialfs selected for that iteration.
I'm not sure I managed to be clear, in practice you should move the first line of your code inside the body of func
Source: MathWorks

What is wrong with my plot_lab code in matlab?

The function is as expected:
%% plot 3D plot with true color marker
plot_Lab(4,Lab,1,'',12,0);
plot_Lab(mode,Lab,createnewfig,markercolor,markersize,storeme)
% This function visualizes several different CIE-Lab_plot plots from
% CIE-Lab coordinate data in 'Lab'.
I enter this:
plot_Lab(4,[45.9470,1.5130,5.2120],1,'',12,0);
and get the following error messages
Error using lab2xyz (line 25)
Incorrect number of columns in LAB data.
Error in applycform (line 88)
out = c.c_func(columndata, cdata{:});
Error in applycformsequence (line 11)
out = applycform(out, cforms{k});
Error in applycform (line 88)
out = c.c_func(columndata, cdata{:});
Error in plot_Lab (line 68)
RGB = applycform(Lab',cform);
Does anyone know where I got it wrong? Please help.
plot_Lab is not a built-in function. Therefore you are supposed to provide its code or link, so one could follow you.
According to the link,
% Lab [3 x n] -> Lab coordinates of n datapoints
Lab is supposed to be 3 x n. What you are provided is 1 x 3. So, you probably need to transpose it:
plot_Lab(4, [45.9470,1.5130,5.2120].', 1, '', 12, 0);

Matlab: mix-up of built-in and external functions

Matlab (2015a) is behaving weirdly: a number of builtin functions are not responding as expected. For instance, typing
ttest([1 2], [1 2])
results in
Error using size
Dimension argument must be a positive integer scalar within indexing range.
Error in nanstd (line 59)
tile(dim) = size(x,dim);
Error in ttest (line 132)
sdpop = nanstd(x,[],dim);
If I do a which for each of these functions:
which size
which nanstd
which ttest
I get, respetively:
built-in (C:\Program Files\MATLAB\R2015a\toolbox\matlab\elmat\size)
C:\Program Files\MATLAB\R2015a\toolbox\stats\eml\nanstd.m
C:\Program Files\MATLAB\R2015a\toolbox\stats\stats\ttest.m
Each of these files looks fine, except that size.m has each one of its rows commented out.
What could be the problem here?
Perhaps related to your problem:
ttest for R2013a makes the following call:
sdpop = nanstd(x,[],dim);
The helpfile for R2013a version of nanstd states:
Y = nanstd(X,FLAG,DIM) takes the standard deviation along dimension DIM of X.
On the other hand, nanstd in the 2005 nansuite package downloaded off Mathworks file exchange states:
FORMAT: Y = nanstd(X,DIM,FLAG)
Notice how DIM and FLAG are reversed!
If I call R2013a's ttest such that it makes a call to the old, 2005 nansuite function nanstd, Matlab generates an error similar to yours:
Error using size
Dimension argument must be a positive integer scalar within indexing range.
Error in nanmean (line 46)
count = size(x,dim) - sum(nans,dim);
Error in nanstd (line 54)
avg = nanmean(x,dim);
Error in ttest (line 132)
sdpop = nanstd(x,[],dim);
If [] is passed as DIM instead of FLAG, then nanstd's call to size(x, DIM) triggers an error because [] is not a positive integer scalar. If something like this is the cause, the broader question is, what's going on with your Matlab install or setup or downloads or whatever such that you're calling archaic code? Or why is that archaic code even around? I don't know at what point in Matlab's release history that nanstd(x, FLAG, DIM) became supported (instead of simply nanstd(x, DIM))?
Archive: below is my old answer which misdiagnosed your problem
Both of your sample vectors x and y are the same (i.e. [1,2]). The estimated variance of the difference is 0, and all your stats are going to blow up with NaN.
Do the stats step by step, and it will be clear what's going on.
x = [1; 2]; % Data you used in the example.
y = [1; 2]; % Data you used in the example.
z = x - y; % Your call to ttest tests whether this vector is different from zero at a statistically significant level.
Now we do all the stats on z
r.n = length(z);
r.mu = mean(z);
r.standard_error = sqrt(var(z,1) / (r.n-1)); % For your data, this will be zero since z is constant!
r.t = r.mu ./ r.standard_error; % For your data, this will be inf because dividing by zero!
r.df = r.n - 1;
r.pvals(r.t >= 0) = 2 * (1 - tcdf(r.t(r.t>=0), r.df)); % For your data, tcdf returns NaN and this all fails...
r.pvals(r.t < 0) = 2 * tcdf(r.t(r.t<0), r.df);
etc...
This should match a call to
[h, p, ci, stats] = ttest(x-y);

How to arrange input as an object for Extrinsics() function of matlab?

I am trying to use extrinsics function of matlab to calculate the translation vector. As a requirement i want to give the input camera parameters. i.e. Camera matrix, distortion matrix. But when i give the input i.e. camParam a 3x3 cameraMatrix, it gives me error.
camParam = [994.735326361544, 0, 624.663440953582;
0, 998.166467837258, 364.087425569226;
0, 0, 1];
[rotationMatrix,translationVector] = extrinsics(left_right_eye_points,(face.model)',camParam);
I get the following error:
Error using extrinsics
Expected cameraParams to be one of these types:
cameraParameters
Instead its type was double.
Error in extrinsics>checkInputs (line 140)
validateattributes(cameraParams, {'cameraParameters'}, {}, ...
Error in extrinsics (line 91)
checkInputs(imagePoints, worldPoints, cameraParams);
Error in Simple_conversion_from_World_to_Camera_to_image (line 37)
[rotationMatrix,translationVector] = extrinsics(left_right_eye_points,(face.model)',camParam)
My question is:
1) how should i arrange my camParam, so that the function extrinsics may accept it.
2) In addition does it also need distortion coefficients? if yes than how to arrange that as well.
You have to do what the error message tells you to do. Create a cameraParameters object and use it. Probably you want cp=cameraParameters('IntrinsicMatrix',camParam)
The cameraParams object also allows you to set a distortion, the documentation explains the details.

matlab ARX parameter

I want to use ARX. X is a 1000X13 matrix (1000 sample with 13 features). I want to see the relationship of for example 1st and 2nd column of X. I don't know how to make input parameters right. What should be the size of [na nb nk] for my regression problem. Matlab documentation doesn't have much detail.
Here is my code:
data = iddata(X(:,1),[],1); %I have to make iddata object first.
Y = arx(data,[ [ones(size(X(:,1),2),size(X(:,1),2))] [ones(size(X(:,2),1),size(X(:,1),1))] [ones(size(X(:,1),2),size(X(:,1),1))] ])
Error is:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
I tried to change the dimensions of [na nb nk], but every time, I got an error like:
Y = arx(data,[ [ones(size(X(:,1),2),size(X(:,1),2))] 1 [ones(size(X(:,1),2),size(X(:,1),1))] ])
Invalid ARX orders. Note that continuous-time ARX models are not supported.
Y = arx(data,[ 1 1 1])
Error using arx (line 77)
The model orders must be compatible with the input and output dimensions of the estimation data.