Reputation: 105
I am trying to build a Dirichlet Process Gibbs Sampler in Julia and represent each cluster as an object. I did the following:
using BayesianNonparametrics
using DataFrames
using Distances
type cluster
m::Vector
Sigma::LinAlg.Cholesky
nu::Int
kappa::Int
nk::Int
end
function logPredPdf(f::cluster,x::Array{Float64,1}):
kappa <- f.kappa
S <- f.Sigma
mu <- f.m
nu <- f.nu
d <- size(x)[1]
v = nu-d+1
U = sqrt((1+1/kappa)/v) * LinAlg.lowrankdowndate!(S, sqrt(f.kappa)*mu)[:U]
x = x - m
Q = \(transpose(U),x)
q= vecdot(Q,Q)
o = -log(1+q/v)*((v+d)/2)
c = lgamma((v+d)/2)-lgamma(v/2)-(d*log(v*pi)+2*sum(log(diag(U))))/2
y = c + o
return y
end
data = readtable("PCA_transformed_data_gt1000.csv",header= true);
data = delete!(data, :1);
n,d = size(data);
s = 6.6172
S0 = s*eye(d)
kappa_0 = 1
nu_0 = d
mu_0 = zeros(d)
S1 = LinAlg.cholfact(S0+kappa_0*(mu_0*mu_0'))
X= DataFrame(Matrix(data)');# transpose data Matrix
prior = cluster(mu_0, S1,nu_0, kappa_0, 0)
x = X[:,1]
kt = logPredPdf(prior,x)
When I run the lines inside the logPredPdf outside of the function it works perfectly! But if I try to run the above example, when it goes in the end, it gives me the following error:
MethodError: no method matching isless(::Symbol, ::Int64)
Closest candidates are:
isless(!Matched::Char, ::Integer) at deprecated.jl:49
isless(::Symbol, !Matched::Symbol) at strings/basic.jl:137
isless(!Matched::DataArrays.NAtype, ::Any) at /Users/u1560476/.julia/v0.5/DataArrays/src/operators.jl:510
...
in logPredPdf(::cluster, ::Array{Float64,1}) at Dirichlet_Process_Gibbs_Sampler.jl:33
in include_string(::String, ::String) at loading.jl:441
in include_string(::String, ::String, ::Int64) at eval.jl:30
in include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N}) at eval.jl:34
in (::Atom.##53#56{String,Int64,String})() at eval.jl:50
in withpath(::Atom.##53#56{String,Int64,String}, ::String) at utils.jl:30
in withpath(::Function, ::String) at eval.jl:38
in macro expansion at eval.jl:49 [inlined]
in (::Atom.##52#55{Dict{String,Any}})() at task.jl:60
The types of "prior" and "x" are cluster and Array{Float64,1}, as the function asks for. Any ideas what I am missing here?
Upvotes: 1
Views: 6655
Reputation: 31342
<-
is not an operator in Julia. Use =
for assignment in Julia. No package (or even macro) would ever allow you to write Julia code that contains a functional <-
operator. x <- y
always means x
is less than -y
:
julia> expand(:(x <- y))
:(x < -y)
julia> Meta.show_sexpr(ans)
(:call, :<, :x, (:call, :-, :y))
I imagine you happened to have those variables defined as you expected in your global workspace, but within that file it looks like kappa
is bound to a Symbol instead.
Edit: You also must remove the :
from function logPredPdf(f::cluster,x::Array{Float64,1}):
. Function definitions (and blocks in general) do not use :
in Julia.
Upvotes: 5
Reputation: 105
Changing all <-
to =
didn't fix the problem, but adding
let
global k
after
function logPredPdf(f::cluster,x::Array{Float64,1}):
seems to solve the problem! Still don't know why though!
Upvotes: 0