STOR-i / GaussianProcesses.jl

A Julia package for Gaussian Processes
https://stor-i.github.io/GaussianProcesses.jl/latest/
Other
308 stars 53 forks source link

MeanLin MethodError? #227

Open mis0butter opened 1 year ago

mis0butter commented 1 year ago

Hello,

I am trying to use the MeanLin function but keep running into method errors.

I saw that these issues related to MeanLin were opened and closed, but I have been unable to recreate their solution: https://github.com/STOR-i/GaussianProcesses.jl/issues/222 https://github.com/STOR-i/GaussianProcesses.jl/issues/47

In issue #47, they used the following code (in Julia 0.5):

x = [-4.0,-3.0,-1.0,0.0,2.0];
y = 2.0x + 0.5rand(5);
xpred = collect(-5.0:0.1:5.0);
mean = MeanLin([0.5]) # linear mean function
kern = SE(0.0,0.0) # squared exponential kernel function
gp = GP(x,y,mean,kern) # fit the GP 

When I try running above code, here is the error text and the (beginning of the) stacktrace:

ERROR: MethodError: no method matching *(::Vector{Float64}, ::Vector{Float64})    
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:591
  *(::StridedMatrix{T}, ::StridedVector{S}) where {T<:Union{Float32, Float64, ComplexF32, ComplexF64}, S<:Real} at C:\Users\junet\AppData\Local\Programs\Julia-1.8.3\share\julia\stdlib\v1.8\LinearAlgebra\src\matmul.jl:49
  *(::StridedVecOrMat, ::LinearAlgebra.Adjoint{<:Any, <:LinearAlgebra.LQPackedQ}) at C:\Users\junet\AppData\Local\Programs\Julia-1.8.3\share\julia\stdlib\v1.8\LinearAlgebra\src\lq.jl:269
  ...
Stacktrace:
  [1] mean(mLin::MeanLin, X::LinearAlgebra.Adjoint{Float64, Vector{Float64}})     
    @ GaussianProcesses C:\Users\junet\.julia\packages\GaussianProcesses\dOato\src\means\mLin.jl:27
  [2] update_mll!(gp::GPE{LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Vector{Float64}, MeanLin, SEIso{Float64}, GaussianProcesses.FullCovariance, GaussianProcesses.IsotropicData{Matrix{Float64}}, PDMats.PDMat{Float64, Matrix{Float64}}, GaussianProcesses.Scalar{Float64}}; noise::Bool, domean::Bool, kern::Bool)

I then tried updating the code so that MeanLin would use an actual vector input. Since the mean function is for the model prior, I set the length of the mean vector to be the same as the test / predict points, still didn't work. Just to try everything, I then set the mean vector length to be the same as the training points, no dice:

xtrain = [-4.0, -3.0, -1.0, 0.0, 2.0] 
ytrain = 2.0 * x + 0.5 * rand(5) 
xpred  = collect( -5.0 : 0.1 : 5.0 )
# mLin   = MeanLin( 0 * xtrain ) # linear mean function
mLin   = MeanLin( 0 * xpred ) # linear mean function
kern   = SE( 0.0, 0.0 )                         # squared exponential kernel function
gp     = GP( x, y, mLin, kern )                 # fit the GP 

In Issue #222, I cannot recreate his code at all as he uses code inputs that he doesn't include in his working example.

From mLin.jl, it looks like I should just be able to define some mean = MeanLin(β) where β is a vector and it should work? How to use MeanLin ?

mutable struct MeanLin <: Mean
    "Linear coefficients"
    β::Vector{Float64}
    "Priors for mean parameters"
    priors::Array 

    """
        MeanLin(β::Vector{Float64})

    Create `MeanLin` with linear coefficients `β`.
    """
    MeanLin(β::Vector{Float64}) = new(β, [])
end
chris-nemeth commented 1 year ago

If I remember correctly, it used to be possible to fix this issue by taking the transpose of the vector. Have you tried MeanLin(β') instead of MeanLin(β)?

Jarrod-Angove commented 2 months ago

Using the transpose of the vector no longer works. The method error remains. Have there been any updates on this?

daniel-frisch-kit commented 2 months ago

You can use MeanPoly([β;;]

chris-nemeth commented 2 months ago

Thanks @daniel-frisch-kit. @Jarrod-Angove does that work?