Skip to content

Instantly share code, notes, and snippets.

@milktrader
Created August 25, 2014 01:00
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 milktrader/8919439db67bc5783cbb to your computer and use it in GitHub Desktop.
Save milktrader/8919439db67bc5783cbb to your computer and use it in GitHub Desktop.
Minkowski criterion function
julia> function L(c)
sum([abs(xs-c)^2 for xs in x])/length(x)
end
L (generic function with 2 methods)
julia> x=[1,2,3,4,5,6,9,12]
8-element Array{Int64,1}:
1
2
3
4
5
6
9
12
julia> mean(x)
5.25
@milktrader
Copy link
Author

You can see that the smallest error is in the 5.25 slot here

julia> [L(i) for i in 4:0.25:6]
9-element Array{Any,1}:
 13.5
 12.9375
 12.5
 12.1875
 12.0
 11.9375
 12.0
 12.1875
 12.5

@milktrader
Copy link
Author

Here is a method that brute forces it

julia> function optimizeL(x; p=2, start=minimum(x), stop=maximum(x), step=0.1)
           rng = [start:step:stop]
           res = [L(x,p,r) for r in rng]
           best = minimum(res)
           idx = findfirst(res, best)
           rng[idx]
       end

@milktrader
Copy link
Author

First though L needs to be defined better

julia> function L(x,p,c)
       sum([abs(xs-c)^p for xs in x])/length(x)
       end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment