Python Quadratic Programming with CVXOPT - python

I am looking for a definitive guide on formulating a CVXOPT quadratic programming problem with quadratic constraints. There are good documents provided here:
The problem statement I am dealing with is identical to the problem here:
What is the matrix G supposed to look like? I've formulated as a system of linear equations, but looking at examples this does not appear to be correct?
The best resource I've found is https://courses.csail.mit.edu/6.867/wiki/images/a/a7/Qp-cvxopt.pdf, but the links at the end are dead for more reading.
I have an ipython notebook trying to use this programming method but it continually fails: https://gist.github.com/jaredvacanti/62010beda0ccfc20d2eac3c900858e50
Edit: I have edited the data source file in the notebook to provide access to the real data used in this optimization problem.

The notebook you have posted seems to have it all figured out. The problem I had is that the source file for data is not available.
Now to your question:
What is the matrix G supposed to look like? I've formulated as a system of
linear equations, but looking at examples this does not appear to be correct?
Rewrite your "linear equations" into matrix form, i.e.
2x + 2y = 4
x - y = 1
is equivalent to
matrix([[2,2],[1,-1]]) * matrix([[x],[y]]) = matrix([[4],[1]])
where matrix is from cvxopt.

Related

How to use Python to optimize an objective function with multiple terms (SVM Dual Problem)?

I am trying to find the solution for the following SVM dual problem using python. The problem is formatted as a quadratic programming problem:
I have been trying to use the Python library, CVXOPT, but according to its docs, and this example, It can only solve SVM problem in the form of:
Which would work fine for problems in the form of:
However, the problem I am trying to solve has two extra terms at the end (first image).
I am wondering how I can adjust the way my problem is formulated such that it is able to be solved with CVXOPT or any other python optimization package.

Is there a way to solve any linear equation and system of linear equations all in one?

Note: I made this a while ago and have learned a lot more since then, enough to understand why what I was asking for was unrealistic. I also should have done more research into sympy before asking this question.
I want to create a program where you enter a linear equation or system of linear equations as a string and in return you get the value of the variable(s) you entered, like so:
equation = input('Expression: ')
# Code to solve your linear equation here
print(answer)
Just to be clear, I want to be able to solve things like x+5=10, or things like 2x+3y=29, and get an output that looks something like "5", or "4, 7" respectively.
I've tried searching multiple websites and some stack overflow questions, but all I've come up with are ways to solve systems of linear equations like 2x+3y=29 using numpy or ways to solve normal linear equations like x+5=10 but none that can do both.
Here's a list of 3 answers I found and why they didn't help:
https://www.mybluelinux.com/how-solve-any-linear-equation-in-python/
Offers a solution to solve normal linear equations, but cannot solve systems of linear equations
https://stackabuse.com/solving-systems-of-linear-equations-with-pythons-numpy/
Solves systems of linear equations, but can't solve linear equations like x+5=10
Is there a python module to solve linear equations?
Same reason as 2, most answers suggested NumPy, and the other answers there didn't solve my problem either.
I also searched some other websites and stack overflow questions, but they didn't work either for the same reasons as the 3 examples I listed.
There are plenty of existing packages to solve linear equations. However, they won't do your normalization work. You have to first transform your existing equations into standard form: an augmented matrix.
If you want to accept arbitrary linear equations, then you have to write code to do the preprocessing, such as turning
3*x + 10 = y - 5
into the standard form
v1 v2 c
(3, -1, -15)
Once you have every equation in canonical form, you pass the coefficients and constants to the existing package.

I don't understnad this particular machine learning solution in python

I started learning machine learning in Python. I plotted the following graph following the author's notes:
One can notice that up until 3.5 weeks, there is a linear regression, and beyond it, there is a 2nd-order polynomial regression. The textbook question is: when will there be 100 000 requests/day?
The author says to use fsolve
reached_max = fsolve(myFunct-100000, x0=800)/(7*24)
The last point has x = 743 (hours). I am confused when the author says that we need to provide an initial starting position (e.g. 800). Why can it be any number after 743 and why does it have to be after 743? Thank you for your help!
The answer is that x0 will serve as an educated initial guess. In general, fsolve should work fine with its default value (x0 = 0), but in some cases will give a too far answer from the true root.
To better understand the math behind finding the root, I encourage to read about Newton's method which serves as good introduction. Of course, fsolve uses more complicated and efficient techniques, but this should be a good baseline.
Hope I was clear and understood, and this has helped you!

Calculating adjusted p-values in Python

So, I've been spending some time looking for a way to get adjusted p-values (aka corrected p-values, q-values, FDR) in Python, but I haven't really found anything. There's the R function p.adjust, but I would like to stick to Python coding, if possible. Is there anything similar for Python?
If this is somehow a bad question, sorry in advance! I did search for answers first, but found none (except a Matlab version)... Any help is appreciated!
It is available in statsmodels.
http://statsmodels.sourceforge.net/devel/stats.html#multiple-tests-and-multiple-comparison-procedures
http://statsmodels.sourceforge.net/devel/generated/statsmodels.sandbox.stats.multicomp.multipletests.html
and some explanations, examples and Monte Carlo
http://jpktd.blogspot.com/2013/04/multiple-testing-p-value-corrections-in.html
According to the biostathandbook, the BH is easy to compute.
def fdr(p_vals):
from scipy.stats import rankdata
ranked_p_values = rankdata(p_vals)
fdr = p_vals * len(p_vals) / ranked_p_values
fdr[fdr > 1] = 1
return fdr
You can try the module rpy2 that allows you to import R functions (b.t.w., a basic search returns How to implement R's p.adjust in Python).
Another possibility is to look at the maths an redo it yourself, because it is still relatively easy.
Apparently there is an ongoing implementation in scipy: http://statsmodels.sourceforge.net/ipdirective/_modules/scikits/statsmodels/sandbox/stats/multicomp.html . Maybe it is already usable.
You mentioned in your question q-values and no answer provided a link which addresses this. I believe this package (at least it seems so from the documentation) calculates q-values in python
https://puolival.github.io/multipy/
and also this one
https://github.com/nfusi/qvalue

Multilateration Algorithm

I'm trying to call upon the famous multilateration algorithm in order to pinpoint a radiation emission source given a set of arrival times for various detectors. I have the necessary data, but I'm still having trouble implementing this calculation; I am relatively new with Python.
I know that, if I were to do this by hand, I would use matrices and carry out elementary row operations in order to find my 3 unknowns (x,y,z), but I'm not sure how to code this. Is there a way to have Python implement ERO, or is there a better way to carry out my computation?
Depending on your needs, you could try:
NumPy if your interested in numerical solutions. As far as I remember, it could solve linear equations. Don't know how it deals with non-linear resolution.
SymPy for symbolic math. It solves symbolically linear equations ... according to their main page.
The two above are "generic" math packages. I doubt you will find (easily) any dedicated (and maintained) library for your specific need. Their was already a question on that topic here: Multilateration of GPS Coordinates

Categories