Hello,
I am trying to use Quadrature.jl to calculate the expected values of some random variables (code pasted below). This function works fine and it gives me the values I would expected; however, when I try and take the derivative of this function with Zygote (a supported feature per the docs) I get an error:
MethodError: no method matching length(::SciMLBase.NullParameters)
I can paste more of the stack trace in if necessary but this only comes up when trying to take the derivative of my function. I think this has something to do with me not passing parameters to the QuadratureProblem() but I don’t know if I’m doing something wrong or if the library is.
The result should be 0.5 for all temperatures and I have verified this by evaluating the function at two temperatures and calculating the central difference.
using Quadrature
function expected_value_U(T::Float64, U::Function)
numerator_integrand(x,p) = U(x)*exp(-U(x)/T)
denominator_integrand(x,p) = exp(-U(x)/T)
numerator = QuadratureProblem(numerator_integrand, -Inf, Inf)
numerator_val = solve(numerator,QuadGKJL(),reltol=1e-7,abstol=1e-7)
denominator = QuadratureProblem(denominator_integrand, -Inf, Inf)
denominator_val = solve(denominator,QuadGKJL(),reltol=1e-7,abstol=1e-7)
return numerator_val[1]/denominator_val[1]
end
function U_harmonic(x)
return 2.0*x + 40.0*(x^2)
end
temp = 2.0
Cv_harmonic = Zygote.gradient( (T) -> expected_value_U(T, U_harmonic), temp)