Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vector construction utilities #1620

Merged
merged 7 commits into from
Jan 21, 2020

Conversation

mcol
Copy link
Contributor

@mcol mcol commented Jan 15, 2020

Summary

This adds the following function signatures (well the underlying code to implement them):

  • vector constant_vector(int K, real c)
  • vector one_hot_vector(int K, int k)
  • vector ones_vector(int K)
  • vector set_spaced_vector(int K, real low, real high)
  • vector uniform_simplex(int K)
  • vector zeros_vector(int K)
  • matrix identity_matrix(int K)

This departs slightly from the signatures mentioned in #692 for constant_vector, one_hot_vector and set_spaced_vector, in that K is always the first parameter, which is also what Eigen uses.

Tests

Added.

Side Effects

None.

Checklist

  • Math issue Container construction utilities #692

  • Copyright holder: Marco Colombo

    The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
    - Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
    - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

  • the basic tests are passing

    • unit tests pass (to run, use: ./runTests.py test/unit)
    • header checks pass, (make test-headers)
    • docs build, (make doxygen)
    • code passes the built in C++ standards checks (make cpplint)
  • the code is written in idiomatic C++ and changes are documented in the doxygen

  • the new changes are tested

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 4.97 4.95 1.0 0.44% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 0.98 -1.87% slower
eight_schools/eight_schools.stan 0.09 0.09 1.0 -0.43% slower
gp_regr/gp_regr.stan 0.22 0.22 0.99 -0.82% slower
irt_2pl/irt_2pl.stan 6.08 6.09 1.0 -0.11% slower
performance.compilation 89.22 86.69 1.03 2.83% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 7.34 7.32 1.0 0.27% faster
pkpd/one_comp_mm_elim_abs.stan 20.33 20.47 0.99 -0.7% slower
sir/sir.stan 104.48 105.7 0.99 -1.17% slower
gp_regr/gen_gp_data.stan 0.04 0.05 0.98 -2.23% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.96 2.96 1.0 0.02% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.32 0.32 1.01 1.02% faster
arK/arK.stan 1.76 1.75 1.01 0.65% faster
arma/arma.stan 0.79 0.81 0.97 -2.9% slower
garch/garch.stan 0.59 0.59 0.99 -0.59% slower
Mean result: 0.996473621875

Jenkins Console Log
Blue Ocean
Commit hash: f699da1


Machine information ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010

CPU:
Intel(R) Xeon(R) CPU E5-1680 v2 @ 3.00GHz

G++:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

Clang:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

@bob-carpenter
Copy link
Contributor

I'll review this one.

Copy link
Contributor

@bob-carpenter bob-carpenter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nicely documented and tested overall. I requested a bunch of simple changes on documenting exceptions and testing boundary conditions. The only real substantive change is that creating a simplex of size 0 should throw an exception and explaining what happens to linearly spaced with K = 0 and K = 1.

stan/math/prim/fun/constant_vector.hpp Show resolved Hide resolved
stan/math/prim/fun/constant_vector.hpp Outdated Show resolved Hide resolved
stan/math/prim/fun/identity_matrix.hpp Show resolved Hide resolved
stan/math/prim/fun/one_hot_vector.hpp Outdated Show resolved Hide resolved
stan/math/prim/fun/one_hot_vector.hpp Show resolved Hide resolved
test/unit/math/prim/fun/ones_vector_test.cpp Outdated Show resolved Hide resolved
test/unit/math/prim/fun/uniform_simplex_test.cpp Outdated Show resolved Hide resolved
test/unit/math/prim/fun/uniform_simplex_test.cpp Outdated Show resolved Hide resolved
test/unit/math/prim/fun/zeros_vector_test.cpp Show resolved Hide resolved
@mcol
Copy link
Contributor Author

mcol commented Jan 17, 2020

Thanks Bob! I've addressed almost everything, I've answered you for things I was not sure.

As suggested I replaced most tests with loops, even if they used the same functions used in the headers being tested, but I left the tests for set_spaced_vector unchanged as they convey more clearly what linearly spaced means.

I've also changed the check in one_hot_vector not to allow 0-size vectors: this is because the function would throw anyway because on a size 0 vector there's no way we can specify an index of at least 1. In either case we throw, but now it's at least more obvious.

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 4.89 4.94 0.99 -1.11% slower
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 1.0 0.17% faster
eight_schools/eight_schools.stan 0.09 0.09 1.0 -0.12% slower
gp_regr/gp_regr.stan 0.22 0.23 0.99 -0.78% slower
irt_2pl/irt_2pl.stan 6.09 6.07 1.0 0.21% faster
performance.compilation 87.92 86.75 1.01 1.33% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 7.3 7.32 1.0 -0.26% slower
pkpd/one_comp_mm_elim_abs.stan 20.34 20.63 0.99 -1.43% slower
sir/sir.stan 95.55 98.79 0.97 -3.4% slower
gp_regr/gen_gp_data.stan 0.05 0.05 0.96 -3.9% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.95 2.96 1.0 -0.37% slower
pkpd/sim_one_comp_mm_elim_abs.stan 0.32 0.32 1.01 0.67% faster
arK/arK.stan 1.75 1.74 1.0 0.27% faster
arma/arma.stan 0.81 0.79 1.02 2.26% faster
garch/garch.stan 0.63 0.63 0.99 -1.05% slower
Mean result: 0.995244270132

Jenkins Console Log
Blue Ocean
Commit hash: 3732d4d


Machine information ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010

CPU:
Intel(R) Xeon(R) CPU E5-1680 v2 @ 3.00GHz

G++:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

Clang:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 4.95 4.91 1.01 0.78% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 1.0 -0.38% slower
eight_schools/eight_schools.stan 0.09 0.09 1.01 0.71% faster
gp_regr/gp_regr.stan 0.22 0.23 0.99 -1.02% slower
irt_2pl/irt_2pl.stan 6.1 6.1 1.0 0.06% faster
performance.compilation 88.79 87.74 1.01 1.19% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 7.29 7.32 1.0 -0.38% slower
pkpd/one_comp_mm_elim_abs.stan 20.65 20.36 1.01 1.39% faster
sir/sir.stan 93.53 93.38 1.0 0.16% faster
gp_regr/gen_gp_data.stan 0.04 0.05 0.99 -0.66% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.96 2.96 1.0 0.03% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.32 0.32 1.01 1.27% faster
arK/arK.stan 1.76 1.75 1.0 0.47% faster
arma/arma.stan 0.81 0.77 1.04 4.11% faster
garch/garch.stan 0.63 0.63 0.99 -0.71% slower
Mean result: 1.00486294415

Jenkins Console Log
Blue Ocean
Commit hash: 3732d4d


Machine information ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010

CPU:
Intel(R) Xeon(R) CPU E5-1680 v2 @ 3.00GHz

G++:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

Clang:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

@bob-carpenter
Copy link
Contributor

bob-carpenter commented Jan 19, 2020 via email

@bob-carpenter bob-carpenter merged commit 12cbb63 into develop Jan 21, 2020
@mcol mcol deleted the feature/692-container-construction-utilities branch January 21, 2020 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants