Rank of a matrix in ojAlgo - ojalgo

I'm currently using the ojAlgo v45.1.0. I have a question regarding how to get the trace and the sum of a matrix. As I store a matrix in the class PrimitiveDenseStore, it is not feasible to see methods to compute the trace and the sum of the matrix. So, does anyone know how to get the trace and the sum of a matrix in the class PrimitiveDenseStore? Thank you in advance!

In ojAlgo BasicMatrix is what best corresponds to EJML's SimpleMatrix (I guess).
With PrimitiveDenseStore things are not done for you, but you can do a lot more. Here's one way to calculate the trace:
double trace = denseStore.aggregateDiagonal(SUM);
There is an interface MatrixDecomposition.RankRevealing that a number of matrix decompositions implement. To instantiate an SVD:
SingularValue<Double> svd = SingularValue.make(denseStore);
svd.decompose(denseStore);
svd.getRank();

Related

Search for the optimal value of x for a given y

Please help me find an approach to solving the following problem: Let X is a matrix X_mxn = (x1,…,xn), xi is a time series and a vector Y_mx1. To predict values ​​from Y_mx1, let's train some model, let linear regression. We get Y = f (X). Now we need to find X for some given value of Y. The most naive thing is brute force, but what are the competent ways to solve such problems? Perhaps there is a use of the scipy.optimize package here, please enlighten me.
get an explanation or matherial to read for understanding
Most scipy-optimize algorithm use gradient method, for those optimization problem, we could apply these into re-engineering of data (find the best date to invest in the stock market...)
If you want to optimize the result, you should choose a good step size and suitable optimize method.
However, we should not classify tge problem as "predict" of xi because what we are doing is to find local/global maximum/minimum.
For example Newton-CG, your data/equation should contain all the information needed/a simulation, but no prediction is made from the method.
If you want to do a pretiction on "time", you could categorize the time data in "year,month..." then using unsupervise learning to "group" the data. If trend is obtained, then we can re-enginning the result to know the time

How to find the value of theta 0 and theta 1? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I am new to ML, I am not sure on how to solve this problem
Could someone tell me how to solve this problem of finding values in a a step by step manner?
From newcomer view point you can actually just test:
h1=0.5+0.5x
h2=0+0.5x
h3=0.5+0x
h4=1+0.5x
h5=1+x
Then which one of the hs(1..5) gives exact observed values of y(0.5,1,2,0) for a given set of dependent variables x(1,2,4,0).
You can answer that by passing sample values of x in the above equation.
I hope i made it simple enough
Here is the cache It's one of most easy problems in machine learning.
Just see that we have to create a linear regression model to fit the following data:-
STEP 1:UNDERSTANDING THE PROBLEM
And as mentioned at the last of question it should completely fit the data.
We have to find theta0 and theta1 in such a way such that given value of x Htheta(x) will give the correct value of y.
STEP 2:FINDING THETA1
In these m examples take any 2 random examples
Htheta(x2)-Htheta(x1) = theta1*(x2)-theta1*(x1)
-----Subtracting those 2 variables(eliminating theta0)
hteta(x2) = y2
(y corresponding to that x in the data as the parameters exactly fit the data provided )
(y2-y1)/(x2-x1) = theta1
----taking common and then dividing by(x2-x1) on both sides of equation
From this:
theta1 = 0.5
STEP3 :CALCULATING THETA0
Take any random example and put the values of theta1, y and x in this equation
y = theta1*x + theta0
theta0 will come out to be 0
My approach would be to view these points by plotting a graph with x,y values. Since it's a straight line, calculate tan(theta) using normal trigonometry, which in this case is y/x(Since it's mentioned they fit perfectly!!). eg:-
tan(theta1) = 0.5/1 or 1/2
Calculate arctan(1/2) // Approx 0.5
Note:- This is not a scalable approach but just some maths fun! Sorry.
In general you would execute some non-iterative algorithmic approach (probably based on solving a system of linear equations) or some iterative approach like GD (Gradient Descent), but this is more simple here, as it's already given that there is a perfect fit.
Perfect fit means: loss/error of zero.
Loss of zero implicates, that sigma0 needs to be zero or else sample 4 (last one) induces a loss
Overall loss is the sum of sample-losses and each loss/component is nonnegative -> we can't tolerate a loss here
When sigma0 is fixed, sample 4 has an infinite amount of solutions producing no loss
But sample 1 shows that it has to be 0.5 to induce no loss
Check the others, it's fitting perfectly
One assumption i made:
Gradient-descent will converge to the optimal solution (which is not always true, even for convex-optimization problems; it's depending learning-rates; one might use line-searches to proof convergence based on some assumptions about the problem; but all that is irrelevant here)

bayesianoptimization in machine learning

Thanks for reading this. I am currently studying bayesoptimization problem and follow the tutorial. Please see the attachment.bayesian optimization tutorial
In page 11, about the acquisition function. Before I raise my question I need state my understanding about bayesian optimization to see if there is anything wrong.
First we need take some training points and assume them as multivariable gaussian ditribution. Then we need use acquisiont function to find the next point we want to sample. So for example we use x1....x(t) as training point then we need use acquisition function to find x(t+1) and sample it. Then we'll assume x1....x(t),x(t+1) as multivariable gaussian ditribution and then use acquisition function to find x(t+2) to sample so on and so forth.
In page 11, seems we need find the x that max the probability of improvement. f(x+) is from the sample training point(x1...xt) and easy to get. But how to get u(x) and that variance here? I don't know what is the x in the eqaution. It should be x(t+1) but the paper doesn't say that. And if it is indeed x(t+1), then how could I get its u(x(t+1))? You may say use equation at the bottom page 8, but we can use that equation on condition that we have found the the x(t+1) and put it into multivariable gaussian distribution. Now we don't know what is the next point x(t+1) so I have no way to calculate, in my opinion.
I know this is a tough question. Thanks for answering!!
In fact I have got the answer.
Indeed it is x(t+1). The direct way is we compute every u and varaince of the rest x outside of the training data and put it into acquisition function to find which one is the maximum.
This is time consuming. So we use nonlinear optimization like DIRECT to get the x that max the acquisition function instead of trying one by one

Calculating marginal and conditional probability distribution

The following is an example question from previous years of Machine Learning. Can anyone help me solve this question.
The correct way to solve part (a) involves marginalizing over all variables in the model.
p(x3,x4)=1/Z \sum_{x1,x2,x5} \phi(x1,x2) \phi(x2,x4) \phi(x3,x4) \phi(x4,x5)
Z=\sum_{x1,x2,x3,x4,x5} \phi(x1,x2) \phi(x2,x4) \phi(x3,x4) \phi(x4,x5)
In a small model like this, you can just compute the sums over the 2^3 and 2^5 respective possibilities. A better method, however, is to compute the sums using belief propagation.
For instance, the sum in the numerator above can be rewritten as
S(x4,x5)=\sum_{x1,x2,x5} \phi(x1,x2) \phi(x2,x4) \phi(x3,x4) \phi(x4,x5)
=\phi(x3,x4) \sum_{x5} \phi(x4,x5) \sum_{x2} \phi(x2,x4) \sum_x1 \phi(x1,x2)
The following intermediate sums can then be computed and used to obtain the final marginal probability:
sx1x2(x2=0)=\phi(x1=0,x2=0)+\phi(x1=1,x2=0)
sx1x2(x2=1)=\phi(x1=0,x2=1)+\phi(x1=1,x2=1)
sx1x2x4(x4=0)=\phi(x2=0,x4=0) sx1x2(x2=0)+\phi(x2=1,x4=0) sx1x2(x2=1)
sx1x2x4(x4=1)=\phi(x2=0,x4=1) sx1x2(x2=0)+\phi(x2=1,x4=1) sx1x2(x2=1)
sx4x5(x4=0)=\phi(x4=0,x5=0)+\phi(x4=0,x5=1)
sx4x5(x4=1)=\phi(x4=1,x5=0)+\phi(x4=1,x5=1)
Then
S(x3,x4)=\phi(x3,x4) sx1x2x4(x4) sx4x5(x4)
and
Z=\sum_{x3,x4} S(x3,x4)

How to find the frequency from FFT data in MATLAB

From the question: How do I obtain the frequencies of each value in an FFT?
I have a similar question. I understand the answers to the previous question, but I would like further clarification on frequency. Is frequency the same as the index?
Let's go for an example: let's assume we have an array (1X200) of data in MATLAB. When you apply 'abs(fft)' for that array it gives the same size array as the result (1X200). So, does this mean this array contains magnitude? Does this mean the indices of these magnitudes are the frequencies? Like 1, 2, 3, 4...200? Or, if this assumption is wrong, please tell me how to find the frequency from the magnitude.
Instead of using the FFT directly you can use MATLAB's periodogram function, which takes care of a lot of the housekeeping for you, and which will plot the X (frequency axis) correctly if you supply the sample rate. See e.g. this answer.
For clarification though, the index of the FFT corresponds to frequency, and the magnitude of the complex value at each frequency (index) tells you the amplitude of the signal at that frequency.

Resources