Example #1
0
def test1():
    import numpy as np
    import pylab
    from scipy import sparse

    from regreg.algorithms import FISTA
    from regreg.atoms import l1norm
    from regreg.container import container
    from regreg.smooth import quadratic

    Y = np.random.standard_normal(500); Y[100:150] += 7; Y[250:300] += 14

    sparsity = l1norm(500, lagrange=1.0)
    #Create D
    D = (np.identity(500) + np.diag([-1]*499,k=1))[:-1]
    D = sparse.csr_matrix(D)

    fused = l1norm.linear(D, lagrange=19.5)
    loss = quadratic.shift(-Y, lagrange=0.5)

    p = container(loss, sparsity, fused)
    
    soln1 = blockwise([sparsity, fused], Y)

    solver = FISTA(p)
    solver.fit(max_its=800,tol=1e-10)
    soln2 = solver.composite.coefs

    #plot solution
    pylab.figure(num=1)
    pylab.clf()
    pylab.scatter(np.arange(Y.shape[0]), Y, c='r')
    pylab.plot(soln1, c='y', linewidth=6)
    pylab.plot(soln2, c='b', linewidth=2)
Example #2
0
def test1():
    import numpy as np
    import pylab
    from scipy import sparse

    from regreg.algorithms import FISTA
    from regreg.atoms import l1norm
    from regreg.container import container
    from regreg.smooth import signal_approximator, smooth_function

    Y = np.random.standard_normal(500)
    Y[100:150] += 7
    Y[250:300] += 14

    sparsity = l1norm(500, l=1.0)
    # Create D
    D = (np.identity(500) + np.diag([-1] * 499, k=1))[:-1]
    D = sparse.csr_matrix(D)
    fused = l1norm(D, l=19.5)

    p = container(loss, sparsity, fused)

    soln1 = blockwise(pen, Y)

    solver = FISTA(p.problem())
    solver.fit(max_its=800, tol=1e-10)
    soln2 = solver.problem.coefs

    # plot solution
    pylab.figure(num=1)
    pylab.clf()
    pylab.scatter(np.arange(Y.shape[0]), Y, c="r")
    pylab.plot(soln1, c="y", linewidth=6)
    pylab.plot(soln2, c="b", linewidth=2)
Example #3
0
def test1():
    import numpy as np
    import pylab
    from scipy import sparse

    from regreg.algorithms import FISTA
    from regreg.atoms import l1norm
    from regreg.container import container
    from regreg.smooth import signal_approximator, smooth_function

    Y = np.random.standard_normal(500)
    Y[100:150] += 7
    Y[250:300] += 14

    sparsity = l1norm(500, l=1.0)
    #Create D
    D = (np.identity(500) + np.diag([-1] * 499, k=1))[:-1]
    D = sparse.csr_matrix(D)
    fused = l1norm(D, l=19.5)

    p = container(loss, sparsity, fused)

    soln1 = blockwise(pen, Y)

    solver = FISTA(p.problem())
    solver.fit(max_its=800, tol=1e-10)
    soln2 = solver.problem.coefs

    #plot solution
    pylab.figure(num=1)
    pylab.clf()
    pylab.scatter(np.arange(Y.shape[0]), Y, c='r')
    pylab.plot(soln1, c='y', linewidth=6)
    pylab.plot(soln2, c='b', linewidth=2)
Example #4
0
from regreg.atoms import l1norm, maxnorm
from regreg.seminorm import seminorm


sparsity = l1norm(500, l=1.3)
D = (np.identity(500) + np.diag([-1]*499,k=1))[:-1]
D = scipy.sparse.csr_matrix(D)
fused = l1norm(D, l=20)

penalty = seminorm(sparsity,fused)


Y = np.random.standard_normal(500); Y[100:150] += 7; Y[250:300] += 14
loss = l2normsq.shift(-Y, l=0.5)
problem = loss.add_seminorm(penalty)
solver = FISTA(problem)
solver.fit(max_its=100, tol=1e-10)
solution = solver.problem.coefs

import pylab
pylab.scatter(np.arange(Y.shape[0]), Y, c='r')
pylab.plot(solution, color='yellow', linewidth=5)

l1_fused = np.fabs(D * solution).sum()
l1_sparsity = np.fabs(solution).sum()

new_fused = l1norm(D, l=l1_fused)
new_sparsity = l1norm(500, l=l1_sparsity)
conjugate = l2normsq.shift(Y, l=0.5)
from regreg.constraint import constraint
loss_constraint = constraint(conjugate, new_fused, new_sparsity)
Example #5
0
from regreg.algorithms import FISTA
from regreg.atoms import positive_part
from regreg.container import container
from regreg.smooth import l2normsq

n = 100
Y = np.random.standard_normal(n)
Y[:-30] += np.arange(n - 30) * 0.2

D = (np.identity(n) - np.diag(np.ones(n - 1), -1))[1:]
nisotonic = positive_part.linear(-sparse.csr_matrix(D), l=3)

loss = l2normsq.shift(-Y, l=0.5)
p = container(loss, nisotonic)
solver = FISTA(p.problem())

vals = solver.fit(max_its=25000, tol=1e-05)
soln = solver.problem.coefs.copy()

nisotonic.atoms[0].l = 100.
solver.fit(max_its=25000, tol=1e-05)
soln2 = solver.problem.coefs.copy()

nisotonic.atoms[0].l = 1000.
solver.fit(max_its=25000, tol=1e-05)
soln3 = solver.problem.coefs.copy()

X = np.arange(n)
pylab.clf()
pylab.scatter(X, Y)
Example #6
0
from regreg.atoms import positive_part
from regreg.container import container
from regreg.smooth import l2normsq


n = 100
Y = np.random.standard_normal(n)
Y[:-30] += np.arange(n-30) * 0.2

D = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:]
nisotonic = positive_part.linear(-sparse.csr_matrix(D), l=3)


loss = l2normsq.shift(-Y,l=0.5)
p = container(loss, nisotonic)
solver=FISTA(p.problem())

vals = solver.fit(max_its=25000, tol=1e-05)
soln = solver.problem.coefs.copy()

nisotonic.atoms[0].l = 100.
solver.fit(max_its=25000, tol=1e-05)
soln2 = solver.problem.coefs.copy()

nisotonic.atoms[0].l = 1000.
solver.fit(max_its=25000, tol=1e-05)
soln3 = solver.problem.coefs.copy()

X = np.arange(n)
pylab.clf()
pylab.scatter(X, Y)
Example #7
0
from regreg.algorithms import FISTA
from regreg.smooth import quadratic
from regreg.atoms import l1norm, maxnorm
from regreg.seminorm import seminorm

D = (np.diag(np.ones(500)) - np.diag(np.ones(499),1))[:-1]
DT = scipy.sparse.csr_matrix(D.T)
D = scipy.sparse.csr_matrix(D)

Y = np.random.standard_normal(500); Y[100:150] += 7; Y[250:300] += 14
loss = quadratic.shift(-Y, l=0.5)
penalty = l1norm(D, l=20)

problem = loss.add_seminorm(seminorm(penalty))
solver = FISTA(problem)
solver.fit(max_its=100, tol=1e-10)
solution = solver.problem.coefs

l1_soln = np.fabs(D * solution).sum()

tfocs_penalty = maxnorm(499, l=l1_soln)
tfocs_loss = quadratic.affine(DT, -Y, l=0.5)
tfocs_loss.coefs = np.zeros(499)
tfocs_problem = tfocs_loss.add_seminorm(tfocs_penalty)
tfocs_solver = FISTA(tfocs_problem)
tfocs_solver.debug = True
tfocs_solver.fit(max_its=1000, tol=1e-10)
tfocs_dual_solution = tfocs_problem.coefs
tfocs_primal_solution = Y - DT * tfocs_dual_solution
Example #8
0
import numpy as np
import pylab
from scipy import sparse

from regreg.algorithms import FISTA
from regreg.atoms import nonnegative
from regreg.container import container
from regreg.smooth import signal_approximator, smooth_function

n = 100
Y = np.random.standard_normal(n)
Y[:-30] += np.arange(n - 30) * 0.2

D = (np.identity(n) - np.diag(np.ones(n - 1), -1))[1:]

isotonic = nonnegative.linear(sparse.csr_matrix(D))
loss = signal_approximator(Y)
p = container(loss, isotonic)
solver = FISTA(p.problem(initial=np.zeros(n)))
solver.debug = True

vals = solver.fit(max_its=25000, tol=1e-08, backtrack=True)
soln = solver.problem.coefs

X = np.arange(n)
pylab.clf()
pylab.scatter(X, Y)
pylab.step(X, soln, 'r--')
Example #9
0
from regreg.algorithms import FISTA
from regreg.atoms import l1norm
from regreg.container import container
from regreg.smooth import l2normsq
 
Y = np.random.standard_normal(500); Y[100:150] += 7; Y[250:300] += 14
loss = l2normsq.shift(-Y, coef=0.5)

sparsity = l1norm(len(Y), 1.4)
# TODO should make a module to compute typical Ds
D = sparse.csr_matrix((np.identity(500) + np.diag([-1]*499,k=1))[:-1])
fused = l1norm.linear(D, 25.5)
problem = container(loss, sparsity, fused)
   
solver = FISTA(problem.composite())
solver.fit(max_its=100, tol=1e-10)
solution = solver.composite.coefs

delta1 = np.fabs(D * solution).sum()
delta2 = np.fabs(solution).sum()

fused_constraint = l1norm.linear(D, bound=delta1)
sparsity_constraint = l1norm(500, bound=delta2)

constrained_problem = container(loss, fused_constraint, sparsity_constraint)
constrained_solver = FISTA(constrained_problem.composite())
constrained_solver.composite.lipshitz = 1.01
vals = constrained_solver.fit(max_its=10, tol=1e-06, backtrack=False, monotonicity_restart=False)
constrained_solution = constrained_solver.composite.coefs
Example #10
0
from regreg.smooth import l2normsq
from regreg.atoms import l1norm, maxnorm
from regreg.seminorm import seminorm

D = (np.diag(np.ones(500)) - np.diag(np.ones(499), 1))[:-1]
DT = scipy.sparse.csr_matrix(D.T)
D = scipy.sparse.csr_matrix(D)

Y = np.random.standard_normal(500)
Y[100:150] += 7
Y[250:300] += 14
loss = l2normsq.shift(-Y, l=0.5)
penalty = l1norm(D, l=20)

problem = loss.add_seminorm(seminorm(penalty))
solver = FISTA(problem)
solver.fit(max_its=100, tol=1e-10)
solution = solver.problem.coefs

l1_soln = np.fabs(D * solution).sum()

tfocs_penalty = maxnorm(499, l=l1_soln)
tfocs_loss = l2normsq.affine(DT, -Y, l=0.5)
tfocs_loss.coefs = np.zeros(499)
tfocs_problem = tfocs_loss.add_seminorm(tfocs_penalty)
tfocs_solver = FISTA(tfocs_problem)
tfocs_solver.debug = True
tfocs_solver.fit(max_its=1000, tol=1e-10)
tfocs_dual_solution = tfocs_problem.coefs
tfocs_primal_solution = Y - DT * tfocs_dual_solution
Example #11
0
import numpy as np
import pylab
from scipy import sparse

from regreg.algorithms import FISTA
from regreg.atoms import nonnegative
from regreg.container import container
from regreg.smooth import signal_approximator, smooth_function

n = 100
Y = np.random.standard_normal(n)
Y[:-30] += np.arange(n-30) * 0.2

D = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:]


isotonic = nonnegative.linear(sparse.csr_matrix(D))
loss = signal_approximator(Y)
p = container(loss, isotonic)
solver=FISTA(p.problem(initial=np.zeros(n)))
solver.debug=True

vals = solver.fit(max_its=25000, tol=1e-08, backtrack=True)
soln = solver.problem.coefs

X = np.arange(n)
pylab.clf()
pylab.scatter(X, Y)
pylab.step(X, soln, 'r--')
        
Example #12
0
import pylab
from scipy import sparse

from regreg.algorithms import FISTA
from regreg.atoms import l1norm
from regreg.container import container
from regreg.smooth import signal_approximator

Y = np.random.standard_normal(500); Y[100:150] += 7; Y[250:300] += 14

sparsity = l1norm(500, l=1.3)
#Create D
D = (np.identity(500) + np.diag([-1]*499,k=1))[:-1]
D = sparse.csr_matrix(D)
fused = l1norm.linear(D, l=25.5)

loss = signal_approximator(Y)

problem = container(loss, sparsity, fused)
solver = FISTA(problem.problem())
solver.fit(max_its=800,tol=1e-10)
soln = solver.problem.coefs

#plot solution
pylab.figure(num=1)
pylab.clf()
pylab.plot(soln, c='g')
pylab.scatter(np.arange(Y.shape[0]), Y)
    

Example #13
0
from regreg.atoms import l1norm
from regreg.container import container
from regreg.smooth import l2normsq

Y = np.random.standard_normal(500)
Y[100:150] += 7
Y[250:300] += 14
loss = l2normsq.shift(-Y, coef=0.5)

sparsity = l1norm(len(Y), 1.4)
# TODO should make a module to compute typical Ds
D = sparse.csr_matrix((np.identity(500) + np.diag([-1] * 499, k=1))[:-1])
fused = l1norm.linear(D, 25.5)
problem = container(loss, sparsity, fused)

solver = FISTA(problem.composite())
solver.fit(max_its=100, tol=1e-10)
solution = solver.composite.coefs

delta1 = np.fabs(D * solution).sum()
delta2 = np.fabs(solution).sum()

fused_constraint = l1norm.linear(D, bound=delta1)
sparsity_constraint = l1norm(500, bound=delta2)

constrained_problem = container(loss, fused_constraint, sparsity_constraint)
constrained_solver = FISTA(constrained_problem.composite())
constrained_solver.composite.lipshitz = 1.01
vals = constrained_solver.fit(max_its=10,
                              tol=1e-06,
                              backtrack=False,
Example #14
0
import pylab
from scipy import sparse

from regreg.algorithms import FISTA
from regreg.atoms import l1norm
from regreg.container import container
from regreg.smooth import signal_approximator

Y = np.random.standard_normal(500)
Y[100:150] += 7
Y[250:300] += 14

sparsity = l1norm(500, l=1.3)
#Create D
D = (np.identity(500) + np.diag([-1] * 499, k=1))[:-1]
D = sparse.csr_matrix(D)
fused = l1norm.linear(D, l=25.5)

loss = signal_approximator(Y)

problem = container(loss, sparsity, fused)
solver = FISTA(problem.problem())
solver.fit(max_its=800, tol=1e-10)
soln = solver.problem.coefs

#plot solution
pylab.figure(num=1)
pylab.clf()
pylab.plot(soln, c='g')
pylab.scatter(np.arange(Y.shape[0]), Y)