I want to implement a convex-relaxation for the E-optimality design criterion, that is:
By defining
the optimization problem would be
I have implemented in Matlab using this function
function [d] = e_optimality(Uk, s, sigma)
%% E-OPTIMALITY DESIGN CRITERION, CONVEX RELAXATION
%
% OUTPUT: sampling_set= vector with ordered sampling
% set
%
% INPUT: Uk= NxK Laplacian eigenvector in the selected band
% s= number of samples
% sigma: entry noise matrix
N= size(Uk,1);
Sigma= diag(sigma*ones(N,1))
Sigma_inv= inv(Sigma);
cvx_begin
variable d(N)
D= diag(d)
B= Uk'*Sigma_inv* D*Uk;
minimize -lambda_min(B)
subject to
d*ones(N,1)==s;
0<=d<=1;
cvx_end
Unfortunately, when launghing the function I have the following error:
One or more output arguments not assigned during call to "varargout".
Error in minimize (line 14)
x = evalin( 'caller', sprintf( '%s ', varargin{:} ) );
Error in e_optimality (line 20)
minimize -lambda_min(B)
It seems to be the minimize -lambda_min in CVX causing the problem. What could it be?
Thank you
Related
without using conv() command, I want to convolve two signals:
This is the code I write:
syms n k i
f= #(n) 2.*(0<=n<=9);
h(n)=((8/9)^n).*heaviside(n-3);
L = length(f);
M = length(h(n));
out=L+M-1;
y=zeros(1,out);
for i = 1:L
for k = 1:M
y(i+k-1) = y(i+k-1) + h(k)*f;
end
end
However, I get an error:
Unable to perform assignment because value of type 'sym' is not convertible to 'double'.
Error in untitled7 (line 13)
y(i+k-1) = y(i+k-1) + h(k)*f;
Caused by:
Error using symengine
Unable to prove '(0.0 <= 0.0) <= 0.0' literally. Use 'isAlways' to test the statement mathematically.
I cannot find a way to fix it and also I do not know how to plot it at the end because of this. Can you help me in both of these issues please?
example how to convolve without command conv
nstop=20;
n1=[-nstop:1:nstop];
n0=nstop+1 % n1(n0)=0
h=zeros(1,numel(n1));
x1=zeros(1,numel(n1));
h(n0+[3:nstop])=(8/9).^[3:nstop];
x1(n0+[0:9])=2;
X1=flip(hankel(x1),2)
H=repmat(h,numel(n1),1);
conv_x_h=sum(X1.*H,2)';
n2=[0:numel(n1)-1]; % time reference for resulting conv
figure;
stem(n1,x1)
hold on
stem(n1,h);
grid on
legend('x1(n)','h(n)')
figure
stem(n2,conv_x_h)
grid on
title('conv(x1,h)')
I've got this code:
function [sigma,shrinkage]=covMarket(x,shrink)
% function sigma=covmarket(x)
% x (t*n): t iid observations on n random variables
% sigma (n*n): invertible covariance matrix estimator
%
% This estimator is a weighted average of the sample
% covariance matrix and a "prior" or "shrinkage target".
% Here, the prior is given by a one-factor model.
% The factor is equal to the cross-sectional average
% of all the random variables.
% The notation follows Ledoit and Wolf (2003)
% This version: 04/2014
% de-mean returns
t=size(x,1);
n=size(x,2);
meanx=mean(x);
x=x-meanx(ones(t,1),:);
xmkt=mean(x')';
sample=cov([x xmkt])*(t-1)/t;
covmkt=sample(1:n,n+1);
varmkt=sample(n+1,n+1);
sample(:,n+1)=[];
sample(n+1,:)=[];
prior=covmkt*covmkt'./varmkt;
prior(logical(eye(n)))=diag(sample);
if (nargin < 2 | shrink == -1) % compute shrinkage parameters
c=norm(sample-prior,'fro')^2;
y=x.^2;
p=1/t*sum(sum(y'*y))-sum(sum(sample.^2));
% r is divided into diagonal
% and off-diagonal terms, and the off-diagonal term
% is itself divided into smaller terms
rdiag=1/t*sum(sum(y.^2))-sum(diag(sample).^2);
z=x.*xmkt(:,ones(1,n));
v1=1/t*y'*z-covmkt(:,ones(1,n)).*sample;
roff1=sum(sum(v1.*covmkt(:,ones(1,n))'))/varmkt...
-sum(diag(v1).*covmkt)/varmkt;
v3=1/t*z'*z-varmkt*sample;
roff3=sum(sum(v3.*(covmkt*covmkt')))/varmkt^2 ...
-sum(diag(v3).*covmkt.^2)/varmkt^2;
roff=2*roff1-roff3;
r=rdiag+roff;
% compute shrinkage constant
k=(p-r)/c;
shrinkage=max(0,min(1,k/t))
else % use specified number
shrinkage = shrink;
end
% compute the estimator
sigma=shrinkage*prior+(1-shrinkage)*sample;
end
It's a Part of the Matlab code from Ledoit/Wolf (2003). I don't understand why the demeaning the returns before calculating the covariance? Is this Matlab specific? In my opinion, there is no need for demeaning returns before calculating with the cov-function. (The function does it on its own)
Thanks for help in advance!
I'm new on matlab.
How can I integrate this line of code ? ?
p2= polyfit(x,y,length(x));
from= x(1);
to= x(length(x));
I need the integration of p2.
I tried a lot with the Integration function:
value = integral(p2,from,to);
but I got
Error using integral (line 82) First input argument must be a function
handle.
Error in poly_integral (line 5)
value = integral(p2,from,to);
That is because p2, in your code, is not a function. It is just a vector of coefficients. The first argument for integral needs to be handle to the function that you want to integrate.
Judging from your code, it seems that you would want to define a function that evaluates the polynomial p2. If so, you could do something like the following example:
% take an example set of x and y
x = linspace(0, pi, 1000); % uniform samples between 0 to pi
y = sin(x); % assume, for sake of example, output is sine function of input
% polynomial fit
p2 = polyfit(x,y,4); % 4th order polynomial
% Note that, in general, the order should be much smaller than length(x).
% So you probably should review this part of your code as well.
% define a function to evaluate the polynomial
fn = #(x) polyval(p2, x);
% this means: fn(x0) is same as polyval(p2, x0)
% compute integral
value = integral(fn,x(1),x(end));
You can use the polyint function to get the polynomial coefficients for exact integration of the polynomial:
p2 = polyfit(x,y,length(x));
int = diff(polyval(polyint(p2),x([1 end])));
I want to solve the following integral by MATLAB. I written the following code
function f = LO_scheme2a(r,kk)
% Run the Prog by : q = integral(#(r)LO_scheme2a(r,kk), 0, inf)
%% System parmaeters
lambda_m= 10^(-2);
lambda_f= kk.*lambda_m; %can be change based on senario
mu= 10^-4; %user density (find the proper user density or scenario based)
Pdbm_m=43;
P_m=10.^(Pdbm_m./10);
Pdbm_f=20;
P_f=10.^(Pdbm_f./10);
gammadBm_m=0;
gamma_m=10.^(gammadBm_m./10);
gammadBm_f=0;
gamma_f=10.^(gammadBm_f./10);
alpha=4;
k=(P_f/P_m)^(1/alpha);% chose proper value
Pdbm_req=0; % the optimum one should be calculate
P_req=10.^(Pdbm_req./10);
gammadBm_0=-40;
gamma_0=10.^(gammadBm_0./10);
%%
%% scheme 2
P_t=P_f;
C0=1; %By changing C_0 you can investigate the performance of system
Theta_0=C0.* P_f;
%%
D1=(P_req./gamma_0).^(1./alpha);
D2=(Theta_0./gamma_0./P_t).^(1./alpha).*r;
D=D2;
lambda_fnew=lambda_f.*exp(-pi.*mu.*D.^2);
%%
PM_u=lambda_m./(lambda_m+k.^2.*lambda_fnew);
PF_u=lambda_fnew.*k.^2./(lambda_m+k.^2.*lambda_fnew);
%%
fm_M=2.*pi.*r.*(lambda_m+k.^2.*lambda_fnew).*exp(-pi.*r.^2.*(lambda_m+k.^2.*lambda_fnew));
ff_F=2.*pi.*r.*(lambda_m./k.^2+lambda_fnew).*exp(-pi.*r.^2.*(lambda_m./k.^2+lambda_fnew));
%%
%% Interfernce from MBSs to MU an FU
A_i= 1;
S_i=gamma_m .*r.^alpha;
LIm_m=exp(-pi.*lambda_m.*(A_i.*r).^2.*rho1(gamma_m,alpha));
%LIm_m=exp(-pi.*lambda_m.*(A_i.*r).^2.*rho0(S_i./(A_i.*r).^alpha,alpha));
A_i= 1./k;
S_i=gamma_f .*r.^alpha *P_m/P_f;
LIf_m=exp(-pi.*lambda_m.*(A_i.*r).^2.*rho1(gamma_f.*P_m.*k.^alpha./P_f,alpha));
%LIf_m=exp(-pi.*lambda_m.*(A_i.*r).^2.*rho0(S_i./(A_i.*r).^alpha,alpha));
%%
max_x= 10^5;
%% Expectiation over h in lemmma 3 : Interfernce from FAPs to MU an FU
S_i=gamma_f .*r.^alpha .*P_f./P_m;
z=(P_req/gamma_0).^(1/alpha);
%MIm_f=(exp(-h).*exp(-mu.*pi.*z^2.*h.^(2./alpha))).*((-((z.^2).*h^(2/alpha)).*(1-exp(-S_i.*z.^(-alpha))))+(S_i.*h).^(2./alpha).*real(gammainc(1-(2./alpha),S_i.*z.^(-alpha))));
for idx = 1:numel(r)
MIm_f(idx)= integral(#(h)exp(-h).*((exp(-mu.*pi.*z^2.*h.^(2./alpha))).*((-((z.^2).*h.^(2./alpha)).*(1-exp(-S_i.*z.^(-alpha))))+(S_i.*h).^(2./alpha).*real(gammainc(1-(2./alpha),S_i.*z.^(-alpha))))),0,max_x);
end
% %
S_i=gamma_f .*r.^alpha ;
z=(P_req./gamma_0).^(1/alpha);
for idx = 1:numel(r)
MIf_f(idx)= integral(#(h)(exp(-h).*exp(-mu.*pi.*z^2.*h.^(2./alpha))).*((-((z.^2).*h.^(2./alpha)).*(1-exp(-S_i.*z.^(-alpha))))+(S_i.*h).^(2./alpha).*real(gammainc(1-(2./alpha),S_i.*z.^(-alpha)))),0,max_x);
end
%%
f=PM_u.*LIm_m.*exp(-2.*pi.*lambda_f.*MIm_f).*fm_M+PF_u.*LIf_m.*exp(-2.*pi.*lambda_f.*MIf_f).*ff_F;
when
function [ f_rho ] = rho1( x,alpha)
f_rho=x.^(2./alpha).*(pi./2-atan(x.^(-2./alpha)));
end
I can run it for different kk but for some kk, it gives me error. for example when kk=10, I can run this program by
q = integral(#(r)LO_scheme2a(r,kk), 0, inf)
but for kk=100, it gives me some error (Error using .*
Matrix dimensions must agree.)
would you please help me to solve this error. Thanks
The problem resides in the lines
for idx = 1:numel(r)
MIm_f(idx)= integral(#(h)exp(-h).*((exp(-mu.*pi.*z^2.*h.^(2./alpha))).*((-((z.^2).*h.^(2./alpha)).*(1-exp(-S_i.*z.^(-alpha))))+(S_i.*h).^(2./alpha).*real(gammainc(1-(2./alpha),S_i.*z.^(-alpha))))),0,max_x);
end
The outer integral will expand S_i into an horizontal vector, and the inner integral will do the same with h, which means that the multiplications can only be carried out whenever both sizes happen to be the same, and giving wrong results. I would put a transpose on h to let it expand on the vertical dimension, and match the multiplications with bsxfun instead of .*, as follows:
for idx = 1:numel(r)
MIm_f(idx)= integral(#(h) ...
bsxfun(#times, exp(-h.'), ...
bsxfun(#times,(exp(-mu.*pi.*z^2.*(h.').^(2./alpha))), ...
bsxfun(#times, -((z.^2).*(h.').^(2./alpha)), (1-exp(-S_i.*z.^(-alpha)))) + ...
bsxfun(#times, bsxfun(#times, S_i, h.').^(2./alpha), real(gammainc(1-(2./alpha),S_i.*z.^(-alpha))))) ...
) ...
,0,max_x);
end
Do the same for the two inner integrals.
NOTE: I don't have the integral function, but this approach works for me with quadv (you need an integration command able to handle vector functions).
I am trying to write a code for classification of data. I try to implement a sigmoid function and then I try to use that function in calculation the cost.I keep getting errors and I have a feeling that it is because of the sigmoid function.I would like the sigmoid function to return a vector.But it keeps returning a scalar.
function g = sigmoid(z)
%SIGMOID Compute sigmoid functoon
% J = SIGMOID(z) computes the sigmoid of z.
% You need to return the following variables correctly
g=zeros(size(z));
m=ones(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
% vector or scalar).
g=1/(m+exp(-z));
This is my cost function:
m = length(y); % number of training examples
% You need to return the following variables correctly
grad=(1/m)*((X*(sigmoid(X*theta)-y)));//this is the derivative in gradient descent
J=(1/m)*(-(transpose(y)*log(sigmoid((X*theta))))-(transpose(1-y)*log(sigmoid((X*theta)))));//this is the cost function
the dimension of X are 100,4; of theta are 4,1;y is 100,1.
THank you.
Errors:
Program paused. Press enter to continue.
sigmoid answer: 0.500000Error using -
Matrix dimensions must agree.
Error in costFunction (line 11)
grad=(1/m)*((X*(sigmoid(X*theta)-y)));
Error in ex2 (line 69)
[cost, grad] = costFunction(initial_theta, X, y);
Please replace g=1/(m+exp(-z)); with g=1./(m+exp(-z)); in your method sigmoid
z = [2,3,4;5,6,7] ;
%SIGMOID Compute sigmoid functoon
% J = SIGMOID(z) computes the sigmoid of z.
% You need to return the following variables correctly
g=zeros(size(z));
m=ones(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
% vector or scalar).
g=1./(m+exp(-z));