Instantly share code, notes, and snippets.


bump2: an optimization problem from A.J.Keane, .

Keywords: optimization, test case, random search, python

Problems in mathematical optimization have several different aspects:

  • contest: whose program reaches minimum __ in cpu time __ and time-to-understand __
  • learning how to explore particular function terrains
  • learning how to constrain.

Minimize trust-constr works well with no constraints and no bounds, so well that it would be nice if bounds worked too -- make trust-constr the method of choice. But constraints=() and bounds -> ValueError in BarrierSubproblem, n_eq 0 -> arrays of shape (0, n_ineq) (I added a couple of debug prints, but a fix is way over my head).

A test case with logs is under


Gish: sharing files on gist.github by name

Keywords: file sharing, gist, github, CLI, python, remote file server

gish is a command-line program to copy files between local computers and, using file names or gist ids. An example:

Alice:  gish put @Alice  # upload a gist with 3 files
#!/usr/bin/env python2
""" A = noisyUSV( n, d, r, noise ): U S V + noise, n x d, rank r """
from __future__ import division
import numpy as np
from numpy.linalg import norm
from etc import znumpyutil as nu
__version__ = "2018-05-15 May denis-bz-py t-online de" # scale noise * S.max

NO2 in Munich 2016: high traffic => high NO2


This plot shows NO2 levels over the day in Munich in June and December 2016. München-Landshuter-Allee on the left has about the highest NO2 levels in all Germany, and a lot of traffic — 120,000 to 150,000 cars and light trucks per day.
Surprise: high traffic => high NO2.


Gradient descent with 2-point line fit where gradients cross 0

The Gradient_descent method iterates

xnew = xold - rate(t) * grad(xold)

GD is a workhorse in machine learning, because it's so simple, uses gradients only (not function values), and can do very big x.

rate(t) is a step-size or "learning rate" (aka η, Greek eta).

#!/usr/bin/env python2
""" How many of the longest pieces of a randomly-broken stick add up to half its length ? """
from __future__ import division
import sys
import numpy as np
__version__ = "2014-10-26 oct denis-bz-py t-online de"
np.set_printoptions( 1, threshold=100, edgeitems=5, suppress=True )
#!/usr/bin/env python2
""" min_x av |exp - x| at 0.7 -- W Least_absolute_deviations, L1
min_x rms( exp - x ) at 1 -- least squares, L2
are both very flat
which might explain why L1 minimization with IRLS doesn't work very well.
# goo "L1 minimization" irls
# different L1 min problems: sparsity, outliers
from __future__ import division

Adaptive soft threshold and smooth abs: scale by average |X|

The soft threshold and smooth absolute value functions


are widely used in optimization and signal processing. (Soft thresholding squeezes small values to 0; if "noise" is small and "signal" large, this improves the signal-to-noise ratio. Smooth abs, also called