我正在尝试在 Julia 中构建一个 Dirichlet Process Gibbs Sampler,并将每个集群表示为一个对象。我做了以下事情:
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)
Run Code Online (Sandbox Code Playgroud)
当我在函数外部运行 logPredPdf 中的行时,它运行良好!但是如果我尝试运行上面的例子,当它最终运行时,它会给我以下错误:
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
Run Code Online (Sandbox Code Playgroud)
正如函数所要求的,“prior”和“x”的类型是 cluster 和 Array{Float64,1}。任何想法我在这里缺少什么?
<-不是 Julia 中的运算符。使用=在朱莉娅分配。任何包(甚至宏)都不允许您编写包含函数<-运算符的Julia 代码。 x <- y总是意味着x小于-y:
julia> expand(:(x <- y))
:(x < -y)
julia> Meta.show_sexpr(ans)
(:call, :<, :x, (:call, :-, :y))
Run Code Online (Sandbox Code Playgroud)
我想您碰巧在全局工作区中按预期定义了这些变量,但在该文件中,它看起来像是kappa绑定到一个符号。
编辑:您还必须:从function logPredPdf(f::cluster,x::Array{Float64,1}):. 函数定义(以及一般的块):在 Julia 中不使用。