# Norms of 1D data sets¶

In :
# randomly generate a sorted list of numbers
srand(0)
N = 9
y = sort(10*rand(N))

Out:
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 :
# 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 :
mean(y)

Out:
3.367394077878682
In :
# 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 :
median(y)

Out:
2.0347655804192266
In :
y

Out:
2.0347655804192266
In :
# 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 :
(y + y[N])/2

Out:
4.76329101929233
In [ ]: