Loss functions in MATLAB - matlab

I want to know how to interpret the loss functions results in MATLAB?
On other words, for example if I got 0.3247 as a results of kfoldLoss() function, is this mean that it is 32.47% error or it is a 0.3247%, or how correctly can I define/interpret this obtained result?
Thank you very much in advance

It means that the mean of the errors across your k folds was 32.47%.

Related

Matlab: need some help for a seemingly simple vectorization of an operation

I would like to optimize this piece of Matlab code but so far I have failed. I have tried different combinations of repmat and sums and cumsums, but all my attempts seem to not give the correct result. I would appreciate some expert guidance on this tough problem.
S=1000; T=10;
X=rand(T,S),
X=sort(X,1,'ascend');
Result=zeros(S,1);
for c=1:T-1
for cc=c+1:T
d=(X(cc,:)-X(c,:))-(cc-c)/T;
Result=Result+abs(d');
end
end
Basically I create 1000 vectors of 10 random numbers, and for each vector I calculate for each pair of values (say the mth and the nth) the difference between them, minus the difference (n-m). I sum over of possible pairs and I return the result for every vector.
I hope this explanation is clear,
Thanks a lot in advance.
It is at least easy to vectorize your inner loop:
Result=zeros(S,1);
for c=1:T-1
d=(X(c+1:T,:)-X(c,:))-((c+1:T)'-c)./T;
Result=Result+sum(abs(d),1)';
end
Here, I'm using the new automatic singleton expansion. If you have an older version of MATLAB you'll need to use bsxfun for two of the subtraction operations. For example, X(c+1:T,:)-X(c,:) is the same as bsxfun(#minus,X(c+1:T,:),X(c,:)).
What is happening in the bit of code is that instead of looping cc=c+1:T, we take all of those indices at once. So I simply replaced cc for c+1:T. d is then a matrix with multiple rows (9 in the first iteration, and one fewer in each subsequent iteration).
Surprisingly, this is slower than the double loop, and similar in speed to Jodag's answer.
Next, we can try to improve indexing. Note that the code above extracts data row-wise from the matrix. MATLAB stores data column-wise. So it's more efficient to extract a column than a row from a matrix. Let's transpose X:
X=X';
Result=zeros(S,1);
for c=1:T-1
d=(X(:,c+1:T)-X(:,c))-((c+1:T)-c)./T;
Result=Result+sum(abs(d),2);
end
This is more than twice as fast as the code that indexes row-wise.
But of course the same trick can be applied to the code in the question, speeding it up by about 50%:
X=X';
Result=zeros(S,1);
for c=1:T-1
for cc=c+1:T
d=(X(:,cc)-X(:,c))-(cc-c)/T;
Result=Result+abs(d);
end
end
My takeaway message from this exercise is that MATLAB's JIT compiler has improved things a lot. Back in the day any sort of loop would halt code to a grind. Today it's not necessarily the worst approach, especially if all you do is use built-in functions.
The nchoosek(v,k) function generates all combinations of the elements in v taken k at a time. We can use this to generate all possible pairs of indicies then use this to vectorize the loops. It appears that in this case the vectorization doesn't actually improve performance (at least on my machine with 2017a). Maybe someone will come up with a more efficient approach.
idx = nchoosek(1:T,2);
d = bsxfun(#minus,(X(idx(:,2),:) - X(idx(:,1),:)), (idx(:,2)-idx(:,1))/T);
Result = sum(abs(d),1)';
Update: here are the results for the running times for the different proposals (10^5 trials):
So it looks like the transformation of the matrix is the most efficient intervention, and my original double-loop implementation is, amazingly, the best compared to the vectorized versions. However, in my hands (2017a) the improvement is only 16.6% compared to the original using the mean (18.2% using the median).
Maybe there is still room for improvement?

why the regression doesnt work? (MATLAB)

I have a accelerometer signal as below:
and I have a walking signal as below:
I tried to regress these two signal using regression. Here is the code:
regMat=zeros(size(walkVec,1),size(walkVec,2));
for i=1:size(data,1)
b=regress(walkAcc', walkVec(i,:)');
regMat(i,:)=walkVec(i,:)-repmat(b,1,length(walkAcc));
regData(i,:)=[data(i,1:(ipt(1)-1)),regMat(i,:), data(i,
(ipt(2)+1):size(data,2) ) ];
end
figure(1),hold on, plot(data(1,:),'r'), plot(regData(1,:)), title
('regressed (b) and raw(r) ')
legend('raw','regressed')
xlabel('samples')
ylabel('Intensity')
and here is the reslts:
as you can see the regression doesnt really work. Do you have any idea why and how I can fix it?
Thanks a lot and kind Regards
Check if regData has any data in the first column. This can also happen if one data is several magnitudes higher or lower. It becomes negligible in the same chart. I tried your code with dummy data and it works.
If what you are looking for is just a visual representation of regressions, and you don't need to store/process data, I suggest you to use the function plotregression (https://it.mathworks.com/help/nnet/ref/plotregression.html).

Standarized Betas values from regstats function

I am conducting a multiple linear regression analysis with the result = regstats(y,X,model) function. I would like to know where are the Standarized Betas values stored in the result structure? If they are not given by this function, how can I obtain them?
Thank you!
Ok, I think I found how to do it:
standardized_beta_values = zscore(result.tstat.beta)
Am I right?

MATLAB Index exceeds matrix dimensions in programming for explicit euler

I keep getting this error. I received this error after I corrected for logical integer error. The only way I could think of programming this was defining the initial value and then start euler's method at the next t value since it uses the previous solution to find the next one. But by doing this I am getting this error? I'm unsure how to fix it. I tried to end one step from my final value but that didn't work either. Thanks for the help. For the problem we had to create the function and call it. I was initially using n=8.
function [exeuler] = pb3(n)
%using explicit euler to solve ODE with input n and outputting exeuler as
%the answer
%n=steps t,y are initial conditions
h=3/n;
t=logical((1+h):h:4);
back=logical(t-h);
exeuler(1)=2; %defines the initial value
exeuler(t)=exeuler(back)+h*(t.^2-(2*exeuler(back)/t));
end
Well, I am sorry, but you haven't showed much of effort. For the future, please provide the complete code (i.e. with example function and so on). This is very unclear.
I think your problem is in these lines:
t=logical((1+h):h:4);
exeuler(t)=exeuler(back)+h*(t.^2-(2*exeuler(back)/t));
cause t is a vector. You are calling vector with non-integer values as index. Then, I guess, you get the wrong amount of exeuler(t)'s which leads to the error.
And for-loop is missing, isn't it? Because you don't use the Euler method stepwise andd therefore f(t+1) doesn't really depend on f(t).
So, my advice in general would be not to correct this error, but to rethink your algorithm.

Integrate a function in Matlab

I know this is a novice question, but I'm new at Matlab and am trying to integrate a function for n=0, n=1, etc.. This is my code so far:
function x = t^n*(t+5)^-1
int(x,t=0..1)
And I keep on getting this error:
Error: File: a02_IX.m Line: 1 Column: 15
Unexpected MATLAB operator.
Does anyone know what this could be?
Thank you!
Try writing it this way:
function x = t^n/(t+5) int(x,t=0..1)
I think raising the term in parentheses to the -1 power reads badly. Maybe MATLAB is confused, too.
What's the value for n? Don't you need to specify that?
It helps to know the answer before you start. This is easy to integrate analytically.
The function looks like this for n=2:
http://www.wolframalpha.com/input/?i=plot+t%5E2%2F%28t%2B5%29+t%3D0..1
Here's the integral, both indefinite and definite:
http://www.wolframalpha.com/input/?i=integrate+t%5E2%2F%28t%2B5%29+from+t%3D0+to+1