I'm a total novice to this and I need some help implementing a solver in Python to optimize the following.
I want to minimize (1/4b)[(Π1-s)'K(Π1-s)+(Π'1-t)'K(Π'1-t)] - tr(KΠ) with respect to Π.
Π is an nxn matrix and 1 denotes the all ones' vector. Also s and t are vectors of dimension n and b is a fixed scalar. So the only quantity that varies is Π, and for that matrix, we have the constraint that all the entries sum up to 1.
How would I do this? Or if this isn't the correct place, where should I ask this?
First you need to express your equation as a python code. Raw python is not that great at pure number crunching, so you should consider a library like Numpy to do the heavy lifting for you.
Once you do that. You can try using one of the optimizers that come with scikit-learn package
If the domain of Π is weird (non continous for example) try using a HyperOpt package
Related
I have been trying for some days to calculate the nearest positive semi-definite matrix from a very large covariance matrix to be able to sample from it.
I have tried MATLAB for the effect, but the memory usage is insane and it always crashes eventually, no error message or log file as far as I searched. The function used for the calculation can be found here https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd. Optimizing the function to remove intermediate matrices seemed to reduce the memory usage, but it eventually crashes much in the same way.
Found this approach for doing the calculation https://stackoverflow.com/a/63131309/18660401 and switched to Python, in hopes of finding some GPU libraries to accelerate the calculations, but it seems I cannot find an up-to-date library that suports calculating eigenvectors using the numpy function. This is the function I am using:
import numpy as np
def get_near_psd(A):
C = (A + A.T)/2
eigval, eigvec = np.linalg.eig(C)
eigval[eigval < 0] = 0
return eigvec.dot(np.diag(eigval)).dot(eigvec.T)
I am currently trying to run the same function with numba in hopes that the translation to LLVM is enough to make the calculations in reasonable time, only modified the above version to include the #jit decorator from numba.
There does not seem to be a very optimized way to do this as far as I can find on my own, so any suggestion is very appreciated to crack this.
Edit: The matrix is a two-dimensional 60416x60416 covariance matrix and it is to be used to generate new samples from the distribution of the mean and covariance matrix calculated from a set of samples using a GAN. For training purposes, samples also need to be generated from randomly sampling the distribution, which I am intending to use the function multivariate_normal from numpy for.
A very up to date library that does have these capabilities including GPU support is pytorch, check out the examples on the torch.linalg.eig-function and the corresponding accelerated function torch.linalg.eigh for symmetric/hermitian matrices. You do have to convert these matrices from numpy to pytorch-tensors first to do the computation (and then convert it back), but you can definitely use it in a very similar way.
Of course also this library can't just magically give you more memory, but it is highly optimized.
I have a matrix on Z^2 with large dimensions (e.g. 20000 vectors of 200 elements). Each vector contains the same number of ones. I want to find minimal set of the vectors that gives a vector of ones in bitwise OR. This is solved by dynamic programming, but the time complexity of the solution is atrocious. I want to apply some optimization like annealing or genetic algorithm or something else to find less or more good approximation of the answer. But I have no experience in optimizing such functions and just don't know what to try first and what to start with. I want to learn some optimization in Python working on this problem, so some advice on pythonic way of discrete optimization here will be appreciated!
I would like to get the numerical jacobian matrix of a system of nonlinear equations. I use the module numdifftools 0.9.16 on python 3.4.
I built a function 'fun' that takes an array X and returns an array Y.
I get the jacobian with the command: jac=nd.Jacobian(fun)(X) and works fine.
I also controlled the step size using the command : jac=nd.Jacobian(fun,step=1e-1)(X) and it is fine too.
Now, I would like the have two different step sizes depending of the equations such as: jac=nd.Jacobian(fun,step=[1,1,1e-1,1e-1])(X), assuming 4 equations. I want to use 1 for the first two equations and 1e-1 for the others.
Unfortunately this command doesn't work. I couldn't find any example that forces different step size but reading the manual this should be feasible. Nobody has experience on it ? Thanks.
I am working on a model describing a protein. Molecule may be found in few states, transitions from one state to another are described by matrix containing transition rates. Model can be resolved as set of ordinary differential equations giving nice numeric results (starting from adjacency matrix -> transition rate matrix -> Kolmogorow forward equations -> numeric integration). However, to get the stochastic nature of the process, I would like to use Monte Carlo method. As far as I know, the Gillespie algorithm is made for this. Are there any packages you recommend for this kind of task?
You could just try using the random module and perform the Monte Carlo simulation yourself, using a random float for the values in your initial adjacency matrix. Just be sure to randomly select a number within the accepted range of what your matrix can take.
To iterate over the simulation, use a for or while loop, depending on how many simulations you want to run.
Beside working with the random module as proposed by #PeterWang and create the matrices yourself, alternatively you can use the numpy module, which also provides random sampling. This way you can create random numbers in any matrix dimensions you prefer. Especially regarding your further tasks a module working with matrices numpy might be a good solution.
For more details, see reference of numpy.random
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