randomjuliajulia-jumpstochastic

In Julia using StochasticPrograms.jl package how can I get the @sampler object as a n*m matrix not a vector?


In the julia programming language I use StochasticPrograms.jl package to model a two stage stochastic problem. I use @sampler object to develop scenarios and random values. The random variable follows a normal distribution. The @sampler output should be a 3*3 matrix to match the random variable dimension ( d[1:J,1:T], J=3, T=3) defined in the problem.

I have used a few different techniques to get the output as a 3*3 matrix (one of them is below) but no luck.

using StochasticPrograms
@sampler SimpleSampler = begin
        N::MvNormal 
    
        SimpleSampler(µ, Σ) = new(MvNormal(µ, Σ))
    
    @sample Scenario begin

        x = rand(sampler.N) 
        
        return @scenario d = reshape(x,3,3)
    end
end

μ=[4,4,4,4,4,4,4,4,4]

Σ=[
    1 0 0 0 0 0 0 0 0;
    0 1 0 0 0 0 0 0 0;
    0 0 1 0 0 0 0 0 0;
    0 0 0 1 0 0 0 0 0;
    0 0 0 0 1 0 0 0 0;
    0 0 0 0 0 1 0 0 0;
    0 0 0 0 0 0 1 0 0;
    0 0 0 0 0 0 0 1 0;
    0 0 0 0 0 0 0 0 1;
]

s = SimpleSampler(μ,Σ)
s()

and the output is:

d: [3.2222636794881696 1.9732554309220443 3.0941572984285233; 3.421615040079402 3.145688781906985 2.856241404036557; 3.0571013553323985 4.24134467488927 5.800220182172864]

Solution

  • As @OscarDowson mentions in the comment, the output is a 3x3 matrix. The semi-colons in the output denote the end of one row and the beginning of the next.

    It seems like you are outputting this with something like: print("d: ", d). Instead, try using display(d). As the documentation for print says:

    Write ... a canonical (un-decorated) text representation
    The representation used by print includes minimal formatting

    the output of a print call is pretty barebones. I haven't looked into the reasons, but my interpretation is that its priority is to be consistent and machine-readable. For human readability, display is better.

    Edit: Oh, and please read through the Performance tips in the manual in case you haven't yet. Having everything as global variables like here will incur performance penalties once you start building this into anything more computationally intensive.