Norms of 1D data sets

In [1]:
# randomly generate a sorted list of numbers
srand(0)
N = 9
y = sort(10*rand(N))
Out[1]:
9-element Array{Float64,1}:
 0.423017
 0.682693
 1.64566 
 1.77329 
 2.03477 
 2.7888  
 3.61828 
 8.23648 
 9.10357 
In [ ]:

In [2]:
# minimize the 2-norm
using JuMP, Gurobi
m = Model(solver=GurobiSolver(OutputFlag=0))
@variable(m, x)
@objective(m, Min, sum( (y-x).^2 ) )

solve(m)

println("The optimal x value is: ", getvalue(x))
Academic license - for non-commercial use only
The optimal x value is: 3.3673940778786826
In [14]:
mean(y)
Out[14]:
3.367394077878682
In [15]:
# minimize the 1-norm
using JuMP
m = Model()
@variable(m, x)
@variable(m, t[1:N])
@constraint(m, y-x .<= t )
@constraint(m, -t .<= y-x )
@objective(m, Min, sum(t) )

solve(m)

println("The optimal x value is: ", getvalue(x))
The optimal x value is: 2.0347655804192266
In [16]:
median(y)
Out[16]:
2.0347655804192266
In [17]:
y[5]
Out[17]:
2.0347655804192266
In [18]:
# minimize the infinity-norm
using JuMP
m = Model()
@variable(m, x)
@variable(m, r)
@constraint(m, y-x .<= r )
@constraint(m, -r .<= y-x )
@objective(m, Min, r )

solve(m)

println("The optimal x value is: ", getvalue(x))
The optimal x value is: 4.76329101929233
In [19]:
(y[1] + y[N])/2
Out[19]:
4.76329101929233
In [ ]: