Julia跳跃多元ML估计

Julia跳跃多元ML估计,julia,julia-jump,Julia,Julia Jump,我试图在Julia中使用跳跃和NLopt解算器在线性回归设置中执行正态分布变量的ML估计 有一个很好的工作示例,但是如果我尝试估计回归参数(斜率),代码编写起来会变得非常乏味,特别是当参数空间增加时 也许有人知道如何写得更简洁。这是我的密码: #type definition to store data type data n::Int A::Matrix β::Vector y::Vector ls::Vector err::Vector end

我试图在Julia中使用跳跃和NLopt解算器在线性回归设置中执行正态分布变量的ML估计

有一个很好的工作示例,但是如果我尝试估计回归参数(斜率),代码编写起来会变得非常乏味,特别是当参数空间增加时

也许有人知道如何写得更简洁。这是我的密码:

#type definition to store data
type data
    n::Int
    A::Matrix
    β::Vector
    y::Vector
    ls::Vector
    err::Vector
end

#generate regression data
function Data( n = 1000 )
    A = [ones(n) rand(n, 2)]
    β = [2.1, 12.9, 3.7]
    y = A*β + rand(Normal(), n)
    ls = inv(A'A)A'y
    err = y - A * ls
    data(n, A, β, y, ls, err)
end

#initialize data
d = Data()
println( var(d.y) )

function ml(  )
    m = Model( solver = NLoptSolver( algorithm = :LD_LBFGS ) )
    @defVar( m, b[1:3] )
    @defVar( m, σ >= 0, start = 1.0 )

    #this is the working example. 
    #As you can see it's quite tedious to write 
    #and becomes rather infeasible if there are more then, 
    #let's say 10, slope parameters to estimate 
    @setNLObjective( m, Max,-(d.n/2)*log(2π*σ^2) \\cont. next line
                            -sum{(d.y[i]-d.A[i,1]*b[1] \\
                                        -d.A[i,2]*b[2] \\
                                        -d.A[i,3]*b[3])^2, i=1:d.n}/(2σ^2) )

    #julia returns:
    > slope: [2.14,12.85,3.65], variance: 1.04

    #which is what is to be expected
    #however:

    #this is what I would like the code to look like:
    @setNLObjective( m, Max,-(d.n/2)*log(2π*σ^2) \\
                            -sum{(d.y[i]-(d.A[i,j]*b[j]))^2, \\
                            i=1:d.n, j=1:3}/(2σ^2) )

    #I also tried:
    @setNLObjective( m, Max,-(d.n/2)*log(2π*σ^2) \\
                            -sum{sum{(d.y[i]-(d.A[i,j]*b[j]))^2, \\
                            i=1:d.n}, j=1:3}/(2σ^2) )

    #but unfortunately it returns:
    > slope: [10.21,18.89,15.88], variance: 54.78

    solve(m)
    println( getValue(b), " ",  getValue(σ^2) )
end
ml()
有什么想法吗

编辑

如Reza所述,工作示例如下:

@setNLObjective( m, Max,-(d.n/2)*log(2π*σ^2) \\
                        -sum{(d.y[i]-sum{d.A[i,j]*b[j],j=1:3})^2,
                        i=1:d.n}/(2σ^2) )

我没有跟踪您的代码,但在任何地方,我希望以下内容适用于您:

sum([(d.y[i]-sum([d.A[i,j]*b[j] for j=1:3]))^2 for i=1:d.n])
正如@iaindanning所提到的,跳转包在其宏中有一种特殊的求和语法,因此更有效和抽象的方法是:

sum{sum{(d.y[i]-d.A[i,j]*b[j], j=1:3}^2,i=1:d.n}

sum{}
语法是一种特殊的语法,仅在跳转宏中有效,是sums的首选语法

因此,您的示例可以写成:

function ml(  )
    m = Model( solver = NLoptSolver( algorithm = :LD_LBFGS ) )
    @variable( m, b[1:3] )
    @variable( m, σ >= 0, start = 1.0 )

    @NLobjective(m, Max,
        -(d.n/2)*log(2π*σ^2)
        - sum{
            sum{(d.y[i]-d.A[i,j]*b[j], j=1:3}^2,
            i=1:d.n}/(2σ^2) )
我将其扩展到多行,使其尽可能清晰


从技术上讲,雷扎的回答没有错,但不是惯用的跳跃,对于大型车型来说也没有那么有效。

的确如此。非常感谢,谢谢你的回答。我知道这在技术上是错误的(由于括号,正如你正确指出的那样),但他完全按照我的需要写出了问题。