Skip to content

Commit

Permalink
Rel 9.5.0 - Fix for matrix input using JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
goedman committed May 3, 2022
1 parent 3b9fb01 commit 68b8b07
Show file tree
Hide file tree
Showing 15 changed files with 975 additions and 42 deletions.
27 changes: 11 additions & 16 deletions Examples/Dyes/dyes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ dyes ="
data {
int BATCHES;
int SAMPLES;
real y[BATCHES, SAMPLES];
//vector[SAMPLES] y[BATCHES];
matrix[BATCHES, SAMPLES] y;
}
parameters {
Expand Down Expand Up @@ -45,25 +44,21 @@ generated quantities {
}
"

dyesdata = Dict(:BATCHES => 6,
data = Dict(
:BATCHES => 6,
:SAMPLES => 5,
:y => reshape([
[1545, 1540, 1595, 1445, 1595];
[1520, 1440, 1555, 1550, 1440];
[1630, 1455, 1440, 1490, 1605];
[1595, 1515, 1450, 1520, 1560];
[1510, 1465, 1635, 1480, 1580];
[1495, 1560, 1545, 1625, 1445]
], 6, 5)
:y => [
1545 1540 1595 1445 1595;
1520 1440 1555 1550 1440;
1630 1455 1440 1490 1605;
1595 1515 1450 1520 1560;
1510 1465 1635 1480 1580;
1495 1560 1545 1625 1445]
)

#data = (BATCHES=6, SAMPLES=5, y=dyesdata[:y])
data = joinpath(ProjDir, "dyes.json")

tmpdir = joinpath(@__DIR__, "tmp")
sm = SampleModel("dyes", dyes, tmpdir);

#rc = stan_sample(sm; data)
rc = stan_sample(sm; data)

if success(rc)
Expand All @@ -79,7 +74,7 @@ if success(rc)
:internals => names(chns, [:internals])
)
)
show(chn)
chn |> display
println()
describe(chn, sections=[:mu]) |> display
println()
Expand Down
12 changes: 0 additions & 12 deletions Examples/Dyes/dyes.json

This file was deleted.

16 changes: 7 additions & 9 deletions Examples/Dyes/dyes_2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,20 @@ generated quantities {
dyesdata = Dict(:BATCHES => 6,
:SAMPLES => 5,
:y => [
1545 1520 1630 1595 1510 1495;
1540 1440 1455 1515 1465 1560;
1595 1555 1440 1450 1635 1545;
1445 1550 1490 1520 1480 1625;
1595 1440 1605 1560 1580 1445;
]
1545 1540 1595 1445 1595;
1520 1440 1555 1550 1440;
1630 1455 1440 1490 1605;
1595 1515 1450 1520 1560;
1510 1465 1635 1480 1580;
1495 1560 1545 1625 1445]
)

data = (BATCHES=6, SAMPLES=5, y=dyesdata[:y])
#data = joinpath(ProjDir, "dyes.json")

tmpdir = joinpath(@__DIR__, "tmp")
sm = SampleModel("dyes", dyes, tmpdir);

rc = stan_sample(sm; use_json=true, data)
#rc = stan_sample(sm; use_json=false, data)

if success(rc)
chns = read_samples(sm, :mcmcchains; include_internals=true)
Expand All @@ -78,7 +76,7 @@ if success(rc)
:internals => names(chns, [:internals])
)
)
show(chn)
chn |> display
println()
describe(chn, sections=[:mu]) |> display
println()
Expand Down
Binary file added Examples/RedCardsStudy/rjgoedman_covid2022.pdf
Binary file not shown.
12 changes: 9 additions & 3 deletions Examples_Stan_Methods/Generate_Quantities/generate_quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ gq = "
}
";

gq_data = Dict(
data = Dict(
"N" => 3,
"y" => [100, 950, 450]
);

stanmodel = SampleModel("Generate_quantities", gq);

rc = stan_sample(stanmodel; data=gq_data)
rc = stan_sample(stanmodel; data,
use_cpp_chains=true, check_num_chains=false,
num_cpp_chains=2, num_julia_chains=2)

if success(rc)
# Convert to an MCMCChains.Chains object
Expand All @@ -41,7 +43,11 @@ if success(rc)
display(df)
println()

StanSample.stan_generate_quantities(stanmodel, 1)
available_chains(stanmodel) |> display
println()

gq_df = stan_generate_quantities(stanmodel, 1, "2_3")
gq_df |> display
end


2 changes: 2 additions & 0 deletions LocalPreferences.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[CPUSummary]
hwloc = false
7 changes: 6 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ uuid = "682df890-35be-576f-97d0-3d8c8b33a550"
license = "MIT"
desc = "Illustrate StanJulia's interface options to cmdstan"
authors = ["Rob J Goedman <[email protected]>"]
version = "9.4.0"
version = "9.5.0"

[deps]
AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
CompatHelperLocal = "5224ae11-6099-4aaa-941d-3aab004bd678"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
MonteCarloMeasurements = "0987c9cc-fe09-11e8-30f0-b96dd679fdca"
NamedArrays = "86f7a689-2022-50b4-a561-43c23ac3c673"
NamedTupleTools = "d9ec5142-1e00-5aa0-9d6a-321866360f50"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StanBase = "d0ee94f6-a23d-54aa-bbe9-7f572d6da7f5"
Expand All @@ -35,12 +37,14 @@ AxisKeys = "0.1, 0.2.0"
CSV = "0.10"
CompatHelperLocal = "0.1.20"
DataFrames = "1.3"
DataStructures = "0.18.11"
DimensionalData = "0.20"
Distributions = "0.25"
DocStringExtensions = "0.8"
JSON = "0.21"
MCMCChains = "5.0"
MonteCarloMeasurements = "1.0"
NamedArrays = "0.9.6"
NamedTupleTools = "0.13, 0.14"
StanBase = "4"
StanDiagnose = "4"
Expand All @@ -54,6 +58,7 @@ Tables = "1.6"
julia = "1"

[extras]
CPUSummary = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

## Purpose

A collection of example Stan Language programs demonstrating the use Stan's cmdstan executable (as an external program) from Julia.
A collection of example Stan Language programs demonstrating all methods available in Stan's cmdstan executable (as an external program) from Julia.

For most applications one of the method packages is a better choice for day to day use and for executing a Stan Language program use the most important method (sample) in StanSample.jl.

## Background info

Expand Down
4 changes: 4 additions & 0 deletions docs/src/VERSIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ This version of the package has primarily been tested with GitHub workflows and

## Versions

### Version 9.4.1 (Not yet published)

1. Doc updates

### Version 9.4.0

1. Updated redcradsstudy results for cmdstan-2.29.0.
Expand Down
44 changes: 44 additions & 0 deletions test/Examples_Test_Cases/MatrixInput/input_array.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using StanSample, Random, Statistics

stan_chris = "
data {
int n_rows;
int<lower=1> n_cols;
matrix<lower=0>[n_rows,n_cols] x;
}
parameters {
real mu;
}
model {
mu ~ normal(0, 1);
}";

seed = 65445
Random.seed!(seed)

n_rows = 10
n_cols = 2
data = [(x = rand(n_rows, n_cols), n_rows = n_rows, n_cols = n_cols) for i in 1:4]

tmpdir = joinpath(@__DIR__, "tmp")
sm = SampleModel("chris", stan_chris, tmpdir);
rc = stan_sample(sm; data, seed);

if success(rc)
df = read_samples(sm , :dataframe)
#df |> display
[mean(df.mu), std(df.mu)] |> display
end

tmpdir = joinpath(@__DIR__, "tmp")
sm = SampleModel("chris", stan_chris, tmpdir);
rc = stan_sample(sm; data=data[1:3], seed);

if success(rc)
df = read_samples(sm , :dataframe)
#df |> display
[mean(df.mu), std(df.mu)] |> display
end

34 changes: 34 additions & 0 deletions test/Examples_Test_Cases/MatrixInput/input_dict_string.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using JSON, StanSample, Random, Statistics

stan_chris = "
data {
int n_rows;
int<lower=1> n_cols;
matrix<lower=0>[n_rows,n_cols] x;
}
parameters {
real mu;
}
model {
mu ~ normal(0, 1);
}";

seed = 65445;
Random.seed!(seed)

n_rows = 10
n_cols = 2
x = fill(0, n_rows, n_cols)
data = Dict("x" => x, "n_rows" => n_rows, "n_cols" => n_cols)

tmpdir = joinpath(@__DIR__, "tmp")
sm = SampleModel("chris", stan_chris, tmpdir);
rc = stan_sample(sm; data, seed);

if success(rc)
df = read_samples(sm , :dataframe)
#df |> display
[mean(df.mu), std(df.mu)] |> display
end
34 changes: 34 additions & 0 deletions test/Examples_Test_Cases/MatrixInput/input_dict_symbol.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using JSON, StanSample, Random, Statistics

stan_chris = "
data {
int n_rows;
int<lower=1> n_cols;
matrix<lower=0>[n_rows,n_cols] x;
}
parameters {
real mu;
}
model {
mu ~ normal(0, 1);
}";

seed = 65445;
Random.seed!(seed)

n_rows = 10
n_cols = 2
x = fill(0, n_rows, n_cols)
data = Dict(:x => x, :n_rows => n_rows, :n_cols => n_cols)

tmpdir = joinpath(@__DIR__, "tmp")
sm = SampleModel("chris", stan_chris, tmpdir);
rc = stan_sample(sm; data, seed);

if success(rc)
df = read_samples(sm , :dataframe)
#df |> display
[mean(df.mu), std(df.mu)] |> display
end
34 changes: 34 additions & 0 deletions test/Examples_Test_Cases/MatrixInput/input_nt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using StanSample, Random, Statistics

stan_chris = "
data {
int n_rows;
int<lower=1> n_cols;
matrix<lower=0>[n_rows,n_cols] x;
}
parameters {
real mu;
}
model {
mu ~ normal(0, 1);
}";

seed = 65445;
Random.seed!(seed)

n_rows = 10
n_cols = 2
x = fill(0, n_rows, n_cols)
data = (x = x, n_rows = n_rows, n_cols = n_cols)

tmpdir = joinpath(@__DIR__, "tmp")
sm = SampleModel("chris", stan_chris, tmpdir);
rc = stan_sample(sm; data, seed);

if success(rc)
df = read_samples(sm , :dataframe)
#df |> display
[mean(df.mu), std(df.mu)] |> display
end
Loading

0 comments on commit 68b8b07

Please sign in to comment.