mathematical-optimization

minimizing a multivariate, differentiable function using scipy.optimize

北战南征 提交于 2019-12-12 08:45:58
问题 I'm trying to minimize the following function with scipy.optimize : whose gradient is this: (for those who are interested, this is the likelihood function of a Bradley-Terry-Luce model for pairwise comparisons. Very closely linked to logistic regression.) It is fairly clear that adding a constant to all the parameters does not change the value of the function. Hence, I let \theta_1 = 0. Here are the implementation the objective functions and the gradient in python (theta becomes x here): def

r - Portfolio Optimization - solve.QP - Constraints are Inconsistent

若如初见. 提交于 2019-12-12 07:35:32
问题 I am trying to use solve.QP to solve a portfolio optimization problem (quadratic problem) Total 3 assets There are 4 constraints: sum of weights equal to 1 portfolio expected return equals to 5.2% each asset weight greater than 0 each asset weight smaller than .5 Dmat is the covariance matrix Dmat <- matrix(c(356.25808, 12.31581, 261.8830, 212.31581, 27.24840, 18.50515, 261.88302, 18.50515,535.45960), nrow=3, ncol=3) dvec is each asset's expected return dvec <- matrix(c(9.33, 3.33, 9.07),

Gurobi with Java without objective function

妖精的绣舞 提交于 2019-12-12 04:36:37
问题 I'm using the solver Gurobi with Java; I read all the Gurobi's Reference Manual, but I still have a few question it's possible to optimize a model without a objective function or I have to put one? it's possible to add a constraint like "x=0 if c>a" where x is a decision variable and c and a are known? Someone can help me? Thanks. 回答1: You don't need to have an objective function, but if you don't Gurobi will consider any feasible solution as good as the next, even one that is obviously (to

Finding the 10001st prime number (in python)? [duplicate]

别来无恙 提交于 2019-12-12 03:24:56
问题 This question already has answers here : Fastest way to list all primes below N (34 answers) Closed 3 years ago . I'm currently trying to use an implementation of the sieve of erasthonese, but it still takes a very long time to find a long list of prime numbers. def sieve(n=1000000): not_prime = [] prime = [] for i in range(2, n+1): if i not in not_prime: prime.append(i) for j in range(i*i, n+1, i): not_prime.append(j) return prime[10002] I tried to hard code to what value the sieve should

Passing multiple starting values to nlminb

强颜欢笑 提交于 2019-12-12 03:19:39
问题 I've tried do.call and apply, and there was a similar nlminb answer that used the plyr package but still no good. So I turn to you all for any suggestions. I've created the following function: calloptim <- function( under,strike, rf, ttoe,par) {(-(under*par[1] -strike*exp(-rf*ttoe)*par[2]))^2} and then used nlminb to estimate par while holding the other arguments constant: nlminb(c(2,2), calloptim, under= 90, strike = 100, rf =0.05, ttoe=3) which yields: $par [1] 1.953851 2.043045 $objective

How to import data from multiple excel sheets in GAMS using loops?

╄→гoц情女王★ 提交于 2019-12-12 02:42:48
问题 I want to import the data for a three-dimensional parameter p(i,j,k) that is stored in in k excel sheets but GAMS does not let me use dollar control statements in loops. Is there any way to do that using loops or other flow control statements like 'for' or 'while'? I need to do something like this but it is seemingly impossible: loop(k, $call gdxxrw Data.xlsx par=temp rng=k!A1:Z20 rdim=1 cdim=1 $gdxin Data.gdx $load temp $gdxin p(i,j,k)=temp(i,j); ); 回答1: Suppose each sheet looks like: (only

Solving nonlinear minimization equations symbolically in matlab

£可爱£侵袭症+ 提交于 2019-12-12 02:39:47
问题 I have a large underdetermined equation system for which I search an unique solution in respect of any given constraints. I simplified my problem into the following one: x²-4=0, y²-9=0, x*y=myMin, x+y=myMin. What is the best way to implement this in Matlab symbolically, so that it returns x=2 y=-3 I'm searching something like syms x y S=solve(... x²-4==0,... y²-9==0,... x*y==myMin,... x+y==myMin); 回答1: I do not know how specify the min as a function command to solve . But here's an approach

Matlab use fminsearch to optimize a interval of numbers

折月煮酒 提交于 2019-12-12 01:28:36
问题 In Matlab I want to use fminsearch to optimize a interval of numbers given a object function fun to minimize. The integer numbers can be selected from 1 to 30, and the number of integers is fixed to 5 for now. Assume the step size is 1. It will optimize many vectors such as: [1 2 3 4 5] [2 3 4 5 6] [7 8 9 10 11] [12 13 14 15 16] In the long run, I may also try to optimize the step size and number of integers in the vector. I want to know how to use fminsearch to properly realize this or maybe

Find a minimum spanning tree

南笙酒味 提交于 2019-12-11 19:51:23
问题 Could you please tell me why this MATLAB code is wrong? I don't understand why. Thank you so much in advance. function [mst, cost] = prim(A) [n,n] = size(A); A, n, pause, if norm(A-A','fro') ~= 0 , disp(' Error: Adjacency matrix must be symmetric ') return, end; intree = [1]; number_in_tree = 1; number_of_edges = 0; notintree = [2:n]'; number_notin_tree= n-1; in = intree(1:number_in_tree), out = notintree(1:number_notin_tree), pause, while number_in_tree < n, mincost = Inf; for i=1:number_in

How do I efficiently sieve through a selected range for prime numbers?

徘徊边缘 提交于 2019-12-11 18:23:00
问题 I've been working through Project Euler and Sphere Online Judge problems. In this particular problem, I have to find all the prime numbers within two given numbers. I have a function that looks promising (based on the Sieve of Eratosthenes), except it's too slow. Can someone spot what is slowing my function down so much, and hint at how I can fix it? Also, some comments about how to approach optimization in general (or links to such comments/books/articles etc,) would be greatly appreciated.