Skip to content

Instantly share code, notes, and snippets.

@cpfiffer
Created January 13, 2021 06: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 cpfiffer/eddf1b06fab562d6d904987539f44b7c to your computer and use it in GitHub Desktop.
Save cpfiffer/eddf1b06fab562d6d904987539f44b7c to your computer and use it in GitHub Desktop.
A super simple estimation of a function using its derivative.
# Set up environment
import Pkg; Pkg.activate(".")
# Imports
using Distributions
using Plots
using Polynomials
using Optim
using ForwardDiff
# Test the projection method on the sin function
f(x) = sin(x)
df(x) = cos(x)
lower, upper = -5, 5
K = 20 # Polynomial basis
T = 100
xs = range(lower, upper, length=T)
ys = f.(xs)
dys = df.(xs)
function fit_model(a, xs)
poly = Polynomial(a)
f_hat = poly.(xs)
df_hat = (derivative(poly)).(xs)
ℓ = sum((dys - df_hat) .^ 2)
# p = plot(xs, ys)
# plot!(p, xs, f_hat)
# display(p)
return ℓ
end
init = zeros(K)
init[1] = 0.5
init[2] = 0.25
target(x) = fit_model(x, xs)
function dtarget(G, x)
return ForwardDiff.gradient!(G, target, x)
end
# opt = optimize(target, dtarget, init, SimulatedAnnealing(), Optim.Options(iterations=1_000))
# opt = optimize(target, dtarget, init, NelderMead(), Optim.Options(iterations=10_000))
opt = optimize(target, init, LBFGS(), Optim.Options(iterations=1_000))
# opt = optimize(target, dtarget, init, LBFGS(), Optim.Options(iterations=100_000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment