BUG: slsqp: ValueError: failed to initialize intent(inout) array -- input 'O' not compatible to 'd'
See original GitHub issueSLSQP in 1.5.0
Reproducing code example:
Failed Travis Job with full TraceBack:
https://travis-ci.com/github/bashtage/arch/jobs/353137519#L323
Using package arch
from arch.univariate import FixedVariance, ConstantMean
from numpy.random import RandomState
rng = RandomState(1234)
y = rng.standard_normal(1000)
variance = 2 + rng.standard_normal(y.shape[0]) ** 2.0
mod = ConstantMean(y, volatility=FixedVariance(variance))
res = mod.fit(disp=True)
Error message:
arch/univariate/base.py:698: in fit
options=options,
/usr/local/lib/python3.7/site-packages/scipy/optimize/_minimize.py:626: in minimize
constraints, callback=callback, **options)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
func = <bound method ARCHModel._loglikelihood of Constant Mean(constant: yes, no. of exog: 0, volatility: Fixed Variance, distribution: Normal distribution), id: 0x11ca0cfd0>
x0 = array([0.01036698, 0.46188348])
args = (array([1.14198973, 0.92445126, 3.79687201, 1.63495773, 1.19634908,
1.29228233, 1.44831208, 1.07279768, 0.92376... [1.74476525e-06, 1.74476525e+06],
[1.64513522e-06, 1.64513522e+06],
[2.20889479e-06, 2.20889479e+06]]))
jac = None
bounds = [(-inf, inf), (array([4.61883478e-06]), array([4.61887937]))]
constraints = [{'fun': <function constraint.<locals>.factory.<locals>.f at 0x11cb92710>, 'type': 'ineq'}]
maxiter = 100, ftol = 1e-06, iprint = 0, disp = False
eps = 1.4901161193847656e-08, callback = <function _callback at 0x119e70710>
finite_diff_rel_step = None, unknown_options = {}, iter = 99
acc = array(1.e-06)
cons = {'eq': (), 'ineq': ({'args': (), 'fun': <function constraint.<locals>.factory.<locals>.f at 0x11cb92710>, 'jac': <function _minimize_slsqp.<locals>.cjac_factory.<locals>.cjac at 0x11cb92830>},)}
ic = 0
con = {'fun': <function constraint.<locals>.factory.<locals>.f at 0x11cb92710>, 'type': 'ineq'}
ctype = 'ineq'
cjac = <function _minimize_slsqp.<locals>.cjac_factory.<locals>.cjac at 0x11cb92830>
def _minimize_slsqp(func, x0, args=(), jac=None, bounds=None,
constraints=(),
maxiter=100, ftol=1.0E-6, iprint=1, disp=False,
eps=_epsilon, callback=None, finite_diff_rel_step=None,
**unknown_options):
"""
Minimize a scalar function of one or more variables using Sequential
Least Squares Programming (SLSQP).
Options
-------
ftol : float
Precision goal for the value of f in the stopping criterion.
eps : float
Step size used for numerical approximation of the Jacobian.
disp : bool
Set to True to print convergence messages. If False,
`verbosity` is ignored and set to 0.
maxiter : int
Maximum number of iterations.
finite_diff_rel_step : None or array_like, optional
If `jac in ['2-point', '3-point', 'cs']` the relative step size to
use for numerical approximation of `jac`. The absolute step
size is computed as ``h = rel_step * sign(x0) * max(1, abs(x0))``,
possibly adjusted to fit into the bounds. For ``method='3-point'``
the sign of `h` is ignored. If None (default) then step is selected
automatically.
"""
_check_unknown_options(unknown_options)
iter = maxiter - 1
acc = ftol
epsilon = eps
if not disp:
iprint = 0
# Constraints are triaged per type into a dictionary of tuples
if isinstance(constraints, dict):
constraints = (constraints, )
cons = {'eq': (), 'ineq': ()}
for ic, con in enumerate(constraints):
# check type
try:
ctype = con['type'].lower()
except KeyError:
raise KeyError('Constraint %d has no type defined.' % ic)
except TypeError:
raise TypeError('Constraints must be defined using a '
'dictionary.')
except AttributeError:
raise TypeError("Constraint's type must be a string.")
else:
if ctype not in ['eq', 'ineq']:
raise ValueError("Unknown constraint type '%s'." % con['type'])
# check function
if 'fun' not in con:
raise ValueError('Constraint %d has no function defined.' % ic)
# check Jacobian
cjac = con.get('jac')
if cjac is None:
# approximate Jacobian function. The factory function is needed
# to keep a reference to `fun`, see gh-4240.
def cjac_factory(fun):
def cjac(x, *args):
if jac in ['2-point', '3-point', 'cs']:
return approx_derivative(fun, x, method=jac, args=args,
rel_step=finite_diff_rel_step)
else:
return approx_derivative(fun, x, method='2-point',
abs_step=epsilon, args=args)
return cjac
cjac = cjac_factory(con['fun'])
# update constraints' dictionary
cons[ctype] += ({'fun': con['fun'],
'jac': cjac,
'args': con.get('args', ())}, )
exit_modes = {-1: "Gradient evaluation required (g & a)",
0: "Optimization terminated successfully",
1: "Function evaluation required (f & c)",
2: "More equality constraints than independent variables",
3: "More than 3*n iterations in LSQ subproblem",
4: "Inequality constraints incompatible",
5: "Singular matrix E in LSQ subproblem",
6: "Singular matrix C in LSQ subproblem",
7: "Rank-deficient equality constraint subproblem HFTI",
8: "Positive directional derivative for linesearch",
9: "Iteration limit reached"}
# Transform x0 into an array.
x = asfarray(x0).flatten()
# SLSQP is sent 'old-style' bounds, 'new-style' bounds are required by
# ScalarFunction
if bounds is None or len(bounds) == 0:
new_bounds = (-np.inf, np.inf)
else:
new_bounds = old_bound_to_new(bounds)
# clip the initial guess to bounds, otherwise ScalarFunction doesn't work
x = np.clip(x, new_bounds[0], new_bounds[1])
# Set the parameters that SLSQP will need
# meq, mieq: number of equality and inequality constraints
meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args']))
for c in cons['eq']]))
mieq = sum(map(len, [atleast_1d(c['fun'](x, *c['args']))
for c in cons['ineq']]))
# m = The total number of constraints
m = meq + mieq
# la = The number of constraints, or 1 if there are no constraints
la = array([1, m]).max()
# n = The number of independent variables
n = len(x)
# Define the workspaces for SLSQP
n1 = n + 1
mineq = m - meq + n1 + n1
len_w = (3*n1+m)*(n1+1)+(n1-meq+1)*(mineq+2) + 2*mineq+(n1+mineq)*(n1-meq) \
+ 2*meq + n1 + ((n+1)*n)//2 + 2*m + 3*n + 3*n1 + 1
len_jw = mineq
w = zeros(len_w)
jw = zeros(len_jw)
# Decompose bounds into xl and xu
if bounds is None or len(bounds) == 0:
xl = np.empty(n, dtype=float)
xu = np.empty(n, dtype=float)
xl.fill(np.nan)
xu.fill(np.nan)
else:
bnds = array(bounds, float)
if bnds.shape[0] != n:
raise IndexError('SLSQP Error: the length of bounds is not '
'compatible with that of x0.')
with np.errstate(invalid='ignore'):
bnderr = bnds[:, 0] > bnds[:, 1]
if bnderr.any():
raise ValueError('SLSQP Error: lb > ub in bounds %s.' %
', '.join(str(b) for b in bnderr))
xl, xu = bnds[:, 0], bnds[:, 1]
# Mark infinite bounds with nans; the Fortran code understands this
infbnd = ~isfinite(bnds)
xl[infbnd[:, 0]] = np.nan
xu[infbnd[:, 1]] = np.nan
# ScalarFunction provides function and gradient evaluation
sf = _prepare_scalar_function(func, x, jac=jac, args=args, epsilon=eps,
finite_diff_rel_step=finite_diff_rel_step,
bounds=new_bounds)
# Initialize the iteration counter and the mode value
mode = array(0, int)
acc = array(acc, float)
majiter = array(iter, int)
majiter_prev = 0
# Initialize internal SLSQP state variables
alpha = array(0, float)
f0 = array(0, float)
gs = array(0, float)
h1 = array(0, float)
h2 = array(0, float)
h3 = array(0, float)
h4 = array(0, float)
t = array(0, float)
t0 = array(0, float)
tol = array(0, float)
iexact = array(0, int)
incons = array(0, int)
ireset = array(0, int)
itermx = array(0, int)
line = array(0, int)
n1 = array(0, int)
n2 = array(0, int)
n3 = array(0, int)
# Print the header if iprint >= 2
if iprint >= 2:
print("%5s %5s %16s %16s" % ("NIT", "FC", "OBJFUN", "GNORM"))
# mode is zero on entry, so call objective, constraints and gradients
# there should be no func evaluations here because it's cached from
# ScalarFunction
fx = sf.fun(x)
try:
fx = float(np.asarray(fx))
except (TypeError, ValueError):
raise ValueError("Objective function must return a scalar")
g = append(sf.grad(x), 0.0)
c = _eval_constraint(x, cons)
a = _eval_con_normals(x, cons, la, n, m, meq, mieq)
while 1:
# Call SLSQP
slsqp(m, meq, x, xl, xu, fx, c, g, a, acc, majiter, mode, w, jw,
alpha, f0, gs, h1, h2, h3, h4, t, t0, tol,
iexact, incons, ireset, itermx, line,
> n1, n2, n3)
E ValueError: failed to initialize intent(inout) array -- input 'O' not compatible to 'd'
/usr/local/lib/python3.7/site-packages/scipy/optimize/slsqp.py:419: ValueError
Scipy/Numpy/Python version information:
numpy 1.18.1
scipy 1.5.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
How to fix "failed to initialize intent(inout|inplace|cache) array ...
I'm trying to use scipy.optimize.fmin_l_bfgs_b function in order to minimize my function evaluateFunc(params) which returns an error and the ...
Read more >[SciPy-user] Scipy optimize fmin_l_bfgs_b gives me an ...
I am using Scipy 0.6 on windows vista with python 2.5.2 (same thing ... ValueError: failed to initialize intent(inout) array -- input not...
Read more >scipy.optimize.minimize — SciPy v1.9.3 Manual
Minimization of scalar function of one or more variables. ... The objective function to be minimized. ... where x is a 1-D array...
Read more >[SciPy-User] Weird error in fmin_l_bfgs_b
ValueError : failed to initialize intent(inout) array -- input not fortran contiguous. My code looks like this: # x0 is the starting point,...
Read more >04 - Optimization - Jupyter Notebooks Gallery
Returns | ------- | Ax : array | 1-d or 2-d array (depending on the shape of ... if convergence is not achieved...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think we should just cast the items to Python floats, given that all the algorithms anyway work in float and will ultimately do the cast. In Numpy, 1-element arrays have always been castable to float; there are plans in Numpy to deprecate this, and I believe we should leave this deprecation as something for Numpy to do instead of trying to do it inconsistently piecemeal in different places in Scipy. Moreover, this is a backward compat break and I’d rather restore the previous behavior than do deprecation cycles with this on our side.
gh-12437
I would guess int, float, np.integer or np.floating might all be OK. Pretty much anything that can be cast to double.
On Thu, Jun 25, 2020, 00:18 Andrew Nelson notifications@github.com wrote: