1,151 questions
0
votes
0
answers
81
views
Standard deviations of fit parameters suspiciously small
I am attempting to perform a nonlinear curve fit to some experimental data obtained from an oscilloscope. The time signal is a sinusoidal voltage over time and I want to evaluate the phase of the ...
2
votes
1
answer
127
views
Why is my code for generating a curve fit of a cosine to a sum of gaussians working so poorly?
I have this code in which I'm trying to use scipy.optimize.curve_fit() to take a cos^2(x) function and approximate it as a sum of gaussian peaks. However, it is generating a very poor fit and I can't ...
2
votes
1
answer
83
views
Use `curve_fit` with `partial` using named parameters instead of positional parameters
I'm trying to use curve_fit on a function for which I want to freeze one or more parameters using partial.
E.g. this pattern, which is working:
from scipy.optimize import curve_fit
from functools ...
1
vote
1
answer
128
views
Why does my logistic curve_fit prediction return huge values although R² ≈ 0.99?
I’m trying to fit a logistic function to GDP data (China GDP dataset) using scipy.optimize.curve_fit.
The fitting looks very good (R² ≈ 0.99, curve matches the data), but when I try to predict a ...
7
votes
2
answers
180
views
When using scipy.optimize.curve_fit what is the optimal formulation of the function being fitted?
I noticed that the formulation matters when trying to fit a non-linear equation of the form y = a + b * x ** c and I wonder which formulation in general results in the best fit?
Formulation 1
y = a + ...
0
votes
1
answer
132
views
Why doesn't scipy.optimize.milp respect the time_limit parameter?
I'm running an optimization problem using scipy MILP function. I get to the point where the solving report pops up, either through time limit or solution (see below). When the solving report pops up, ...
0
votes
0
answers
60
views
SHGO optimisation ending upon None. Tried all three sampling methods: sobol, halton, simplicial. Evaluation variability reason for None output?
I am minimising the objective function which is -100 * fill rate(floors) together with L2 penalty on epsilon and gamma:
-100 * fill_rate(floors) + l1 * ||epsilon|| + l2 * ||gamma||
I have constraints ...
0
votes
1
answer
76
views
mpi4py: only rank 0 participating in scipy minimize after first iteration
Given the code below, I am unable to remedy the fact that only rank 0 participates in evaluations of Objective after the first iteration of the (SciPy) minimizer. Obviously the rank!=0 workers finish ...
1
vote
2
answers
104
views
mpi4py deadlock with scipy.minimize
I am trying to do something similar as described in Parallelize a function call with mpi4py
But there are some things there that make me skeptical of the provided answer. Additionally, I have a class ...
3
votes
1
answer
162
views
scipy curve_fit fails on specified function
I am trying to find the optimal projection parameters when transforming spherical coordinates into Cartesian ones for a given projection. The mathematical description of the projection is presented in ...
2
votes
3
answers
127
views
Multiprocessing with SciPy Optimize
Question: Does scipy.optimize have minimizing functions that can divide their workload among multiple processes to save time? If so, where can I find the documentation?
I've looked a fair amount ...
0
votes
1
answer
83
views
Python - SciPy: Monitoring the progress of the optimization (as debugging)
I am trying to apply scipy optimization for an application. I am not able to monitor the progress of the optimization. Can someone help me with an example (pointer is enough) or sources from where I ...
2
votes
1
answer
131
views
Is it possible to increase precision in scipy's least squares optimization?
I am trying to use SciPy's scipy.optimize.least_squares to solve a system of 35 non linear equations related to multilateration.
For context :
There are 5 "reference points" which are fixed ...
2
votes
2
answers
117
views
Find correct root of parametrized function given solution for one set of parameters
Let's say I have a function foo(x, a, b) and I want to find a specific one of its (potentially multiple) roots x0, i.e. a value x0 such that foo(x0, a, b) == 0. I know that for (a, b) == (0, 0) the ...
1
vote
1
answer
70
views
Scipy Minimize throwing bounds error when constraint is added
I am trying to optimize a matrix given some bounds and constraints. When I run the minimize function with only the bounds everything works fine, but when adding the constraints I get the following ...
1
vote
1
answer
65
views
Solution to syntax problem in using "differential_evolution()" in scipy
I am trying to minimise a complex problem (non-linear) with differential_evolution method in scipy. Key syntaxes are given below
a) I am creating an object assigned to differential_evolution
res = ...
1
vote
1
answer
92
views
Access scipy differential evolution object through custom strategy
A recent addition to scipy's differential_evolution implementation is to allow custom strategies, which should be a callable. Here is a simplistic example.
from scipy.optimize import ...
1
vote
1
answer
67
views
supply extra parameter as function argument for scipy optimize curve_fit
I am defining a piecewise function for some data,
def fit_jt(x, e1, e2, n1, E1, E2, N1, N2):
a = 1.3
return np.piecewise(x, [x <= a, x > a], [
lambda x: 1 / e1 +
(1 - np.float128(...
0
votes
0
answers
67
views
sc.minimize ignoring the constraints
I'm having some troubles with this optimization below. x0 is an array with -1 and 1, the result i'm trying to achieve is that the code choose to let the number as +/- 1 or change to 0, the main ...
3
votes
0
answers
67
views
scipy.optimize.minimize ignores NonlinearConstraint object
I'm making a trajectory propagation optimiser, running into an issue where the result of SciPy optimise seems to completely ignore a non linear constraint. I've used the exact same format in a similar ...
4
votes
2
answers
206
views
Curve-fitting a non linear equation
I'm trying to fit my thermal conductivity into the Debye-Callaway equation. However, one of my parameters is coming back negative. I've tried different initial guesses. So I'm attaching a code with ...
-1
votes
1
answer
114
views
How do I stop curve fit from overfitting one point? [closed]
I’m trying to fit the function C * np.sqrt(a - x) + D (a is a number, not a parameter) to some data, and it’s really overfitting one point.
I’ve plotted it on Desmos based on the popt values python ...
0
votes
1
answer
132
views
Differential evolution fails when worker argument is added
To set up my differential_evolve curve-fit function, I borrowed heavily from https://bitbucket.org/zunzuncode/ramanspectroscopyfit/src/master/RamanSpectroscopyFit.py. My implemented function works ...
14
votes
8
answers
762
views
Finding solutions to linear system of equations with integer constraint in scipy
I have a system of equations where each equation is a linear equation with boolean constraints. For example:
x1 + x2 + x3 = 2
x1 + x4 = 1
x2 + x1 = 1
And each x_i is either 0 or 1. Sometimes there ...
1
vote
1
answer
113
views
Why is curve_fit giving me different results in different computers?
I have the following model:
-dc/dt = kc(t) - k'n(t)
I compute c(t) and n(t) from a simulation trajectory with CuPy. I then compute k and k' with curve_fit, through the following code:
# compute dc/dt
...
1
vote
0
answers
105
views
Running SciPy Optimizer Dual Annealing
What is the correct way to call dual_annealing scipy optimizer?
Tried both the variants, neither of them works.
import scipy.optimize as spo
optimizer_fn = 'spo.dual_annealing'
optimizer = eval(...
0
votes
2
answers
139
views
solve Sympy functions in Scipy (optimize.root)
I'm trying to find the roots with the root function in Scipy for functions/variables created in Sympy.
sympy:
AllEquations = [x1 - 5, y1 - 5, z1 - 5, ((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2)**0.5 -...
0
votes
1
answer
158
views
How to calculate daily weights which satisfy certain conditions
I have the following pandas dataframe which represents the consumption of 7 days (day_0 is today, day_-1 is yesterday etc) of 10 people (ids):
import pandas as pd
import numpy as np
df = pd.DataFrame(...
1
vote
2
answers
78
views
curve_fit popt is only returning one parameter when my function has two
I am trying to understand why my curve_fit is only producing one optimized parameter when I have created the function using two, td and tr. The number is it producing also does not make sense as I am ...
0
votes
0
answers
87
views
Preconditioning conjugate gradient solvers in scipy.optimize.minimize
I'm solving a large unconstrained optimization problem and experimenting with the trust-kyrlov / trust-ncg methods in scipy.optimize.minimize. Unfortunately, these methods can be quite slow when my ...
3
votes
3
answers
215
views
Scipy Curve_fit: how would I go about improving this fit?
I've been working on a standard potential which I am trying to fit with a given model:
ax2 - bx3 + lx4
The x and y values for the fit are generated from the code as well, the x values are generated by ...
0
votes
1
answer
101
views
Issues with y-axis offset in curve fitting for sinusoidal wave data
I am working on a curve-fitting optimization problem using sinusoidal wave data. I have around 16 cycles of almost identical sine wave data, but I am encountering an issue with the y-axis offset. The ...
1
vote
1
answer
81
views
Scipy.optimize not respecting constraints
I'm trying to create a script that will minimize the maximum distance between the ends of two lines at three different orientations. The first line, A, has a fixed origin and length, while line B's ...
3
votes
2
answers
217
views
Using SciPy minimize to solve the Catenary problem
I am attempting to use SciPy optimization to find the function that minimizes the potential energy of a hanging rope between 2 points, the catenary problem. The objective function is defined by
The ...
0
votes
0
answers
102
views
How to make the scipy minimization function multithread?
I try to use scipy minimize to solve a minimization functions. My objective function and gradient function require multi-threaded computation. However, when calling these functions, there is no ...
1
vote
1
answer
125
views
Optimization on Financial Data in python
I am trying to calculate the breakeven price for a given IRR and getting stuck in scope's root solver. A simple example:
n = 10. # no of periods
years = np.arange(n) + 1
initial_investment = [-1000]
...
0
votes
2
answers
184
views
fmin_slsqp taking too long
I am using fmin_slsqp to find the weights that minimize mean squared error. The weights need to be positive. For each pair of X and y, it takes ~10 seconds. (Each X is (10, 1000) and y is (10,)). I ...
0
votes
0
answers
121
views
Problem for Fitting data with a lognormal function via Maximum Likelihood Estimation for Fragility Curves
I am working with a dataset where I want to estimate the parameters of a lognormal distribution using Maximum Likelihood Estimation (MLE). The data represents observed probabilities of events (like ...
0
votes
2
answers
111
views
Multi-dimensional scipy.optimize.LinearConstraint?
My linear constraint for scipy.optimize.minimize is
ones = np.ones_like(x)
np.outer(x, ones) - np.outer(ones, x) > something
where something is a given matrix.
(Mathematically, a_ij < x_i - x_j ...
0
votes
1
answer
67
views
NonlinearConstraint in SciPy Optimize not working with vector bounds?
I asked ChatGPT and it wasn't helpful. Basically I want to implement a constraint on a Transformed vector of X, let's say T(X). How do I do that? Here is my code:
def TransformFunction(X):
...
1
vote
0
answers
74
views
Scipy.optimize.minimize with constraints
I need to minimize a two variables function and I have a constraint to respect.
I wrote the following code
def deflection_constraint(inputs):
#return value must come back as 0 to be accepted
#...
0
votes
1
answer
80
views
how do fit probolic on data with python [duplicate]
sorry for asking low level questions. I'm a physicist and I don't know much about programming
I have data want to do fit probolic on it and I have no solution to solve it
example file data
x
y
z
t
-2....
0
votes
1
answer
135
views
SciPy Minimize Constrain Around Internal Parameter, Not Input
EDIT 1: This question has been completely modified for improved clarity and to better state my intent. The original post is at the end.
I think a lot of people are confused by my example code. It was ...
1
vote
2
answers
121
views
Python curve fitting gone wrong
I am trying to fit some data to a theoretical Laser interferometric vibrating speaker system characterization model, and I am running into some problems. Here's the mathematical model:
Here's the ...
0
votes
1
answer
62
views
Fitting a curve with sin(atan)
I'm trying to fit a curve using the curve_fit from scipy, but I'm getting RuntimeError. I have an input data (x,y) and I'd like to fit it to an equation defined in the y_fit function. Does anyone know ...
0
votes
1
answer
129
views
2D scipy.optimize.curve_fit using a 1D approach
My goal is to achieve a two dimensional curve fit using scipy curve_fit function (I'm a bit lazy and just wanted to apply my typical 1D curve fitting to a 2D surface).
I begin with two lambda ...
0
votes
1
answer
97
views
Scipy minimize to optimize ODE parameters
I have defined an ode function, langmuir_ode, as follows:
def langmuir_ode(t, AL, Rmax, kon, koff):
A = np.interp(t, time, concs)
L = Rmax - AL
return kon * A * L - koff * AL
t is some ...
0
votes
1
answer
98
views
Why are parameters returned by scipy's weibull_min inaccurate?
I'm trying to (1) simulate data with a Weibull distribution using known parameters, and then (2) trying to fit that simulated data with a Weibull function and get parameters back. The idea is that I ...
0
votes
1
answer
87
views
Python scipy.optimize gives wrong answer, how to deal with semidefinite positive condition?
The goal is to calculate an optimization problem using python scipy.optimize.
Suppose C is a given 4-dimension matrix (in the code I use a random matrix to denote that). The optimized variables are A0 ...
2
votes
1
answer
89
views
New column in Pandas dataframe using least squares from scipy.optimize
I have a Pandas dataframe that looks like the following:
Race_ID Date Student_ID feature1
1 1/1/2023 3 0.02167131
1 1/1/2023 4 ...