Skip to content

Instantly share code, notes, and snippets.

@denis-bz
Created March 31, 2017 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denis-bz/57819ce241a05a09f7260affb8397822 to your computer and use it in GitHub Desktop.
Save denis-bz/57819ce241a05a09f7260affb8397822 to your computer and use it in GitHub Desktop.
L1 min and L2 min of (exponential - x) are both very flat 2017-03-31 Mar
#!/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
import sys
import numpy as np
import etc.numpyutil as nu
np.set_printoptions( threshold=20, edgeitems=10, linewidth=140,
formatter = dict( float = lambda x: "%.2g" % x )) # float arrays %.2g
nx = 100
seed = 1
# to change these params, run this.py a=1 b=None 'c = ...' in sh or ipython
for arg in sys.argv[1:]:
exec( arg )
np.random.seed( seed )
print "nx %d seed %d" % (nx, seed)
#...............................................................................
yrandom = np.sort( np.random.exponential( size=nx ))
ylogu = - np.log( np.linspace( .5 / nx, 1 - .9 / nx, nx ))[::-1] # ~ exp, brokenstick.py
for y in [yrandom, ylogu]:
print "y: av %.3g %s" % (y.mean(), nu.quantiles( y ))
#...............................................................................
for x in np.arange( .6, 1.1 + .01, .05 ):
av1 = nu.av1( y - x )
av2 = nu.av2( y - x )
print "exp - %-5.2g: av1 %-5.3g av2 %-5.3g" % (x, av1, av2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment