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

generalized uniform #2133

Merged
merged 17 commits into from
Nov 5, 2020
Merged

generalized uniform #2133

merged 17 commits into from
Nov 5, 2020

Conversation

t4c1
Copy link
Contributor

@t4c1 t4c1 commented Oct 6, 2020

Summary

Generalizes uniform distribution related functions to accept Eigen expressions.

Tests

Tested using expression testing framework.

Side Effects

None.

Release notes

Generalized uniform distribution related functions to accept Eigen expressions.

Checklist

  • Math issue Generalize matrix function signatures #1470

  • Copyright holder: Tadej Ciglarič

    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)
    • dependencies checks pass, (make test-math-dependencies)
    • 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

@t4c1
Copy link
Contributor Author

t4c1 commented Oct 9, 2020

Hmm, this is only failing due to tight tolerances (in test_repeat_as_vector second order gradients differ by 2.2e-14). @bbbales2 Is it possible to increase tolerances just for one function?

@bbbales2
Copy link
Member

bbbales2 commented Oct 9, 2020

Yeah this seems a bit weird. I want some time to look at it.

The error went away when I changed the first lcdf test case to:

    param[0] = 1.1;   // y                                                                                                                                                   
    param[1] = -0.1;  // alpha                                                                                                                                               
    param[2] = 5.0;   // beta                                                                                                                                                
    parameters.push_back(param);
    cdf_log.push_back(std::log(1.2 / 5.1));  // expected cdf_log 

from:

    param[0] = 0.1;   // y                                                                                                                                                   
    param[1] = -0.1;  // alpha                                                                                                                                               
    param[2] = 5.0;   // beta                                                                                                                                                
    parameters.push_back(param);
    cdf_log.push_back(std::log(0.2 / 5.1));  // expected cdf_log 

It seems weird that that would have made so much of a difference for such simple functions. Anyway I will get to this after the release is sorted out.

@t4c1
Copy link
Contributor Author

t4c1 commented Oct 27, 2020

@bbbales2 Do you have time to look into this now?

@bbbales2
Copy link
Member

@t4c1 yeah. I guess with release out no excuse to procrastinate

@bbbales2
Copy link
Member

Alright I am confused. I was trying to simplify the computations a bit in the hopes that this went away and I am stuck.

The test I'm running is:

./runTests.py test/prob/uniform/uniform_cdf_log_00001_generated_v_test.cpp

If I replace:

ops_partials.edge1_.partials_ = inv(b_minus_a * cdf_log_n);

with:

ops_partials.edge1_.partials_ = inv(b_minus_a * (y_val - alpha_val) / b_minus_a);

Things work. If I replace it with:

ops_partials.edge1_.partials_ = inv(y_val - alpha_val);

The test fails. I have tried evaluating this expression to a temporary.

Anyway I'm not sure what is going on. I will come back to this tomorrow with a fresh head and try again.

@bbbales2
Copy link
Member

Alright got it, but Github didn't let me push to your branch. This code passes for me:

  const auto& b_minus_a
      = to_ref_if<!is_constant_all<T_low, T_high>::value>(beta_val
                                                          - alpha_val);
  const auto& y_minus_alpha
      = to_ref_if<!is_constant_all<T_y, T_low>::value>(y_val - alpha_val);
  const auto& cdf_log_n = y_minus_alpha / b_minus_a;
  T_partials_return cdf_log = sum(log(cdf_log_n));

  if (!is_constant_all<T_y>::value) {
    if(!is_vector<T_y>::value &&
       is_vector<T_high>::value &&
       !is_vector<T_low>::value) {
      ops_partials.edge1_.partials_ = size(beta) * inv(y_minus_alpha);
    } else {
      ops_partials.edge1_.partials_ = inv(y_minus_alpha);
    }
  }
  if (!is_constant_all<T_low>::value) {
    ops_partials.edge2_.partials_
        = (y_val - beta_val) / (b_minus_a * y_minus_alpha);
  }
  if (!is_constant_all<T_high>::value) {
    if (is_vector<T_y>::value && !is_vector<T_low>::value
        && !is_vector<T_high>::value) {
      ops_partials.edge3_.partials_
          = inv(-b_minus_a) * size(y);
    } else {
      ops_partials.edge3_.partials_ = inv(-b_minus_a);
    }
  }

It replaces cdf_log_n * b_minus_a with y_minus_alpha (which is y_val - alpha_val).

t4c1 and others added 13 commits October 28, 2020 08:54
# Conflicts:
#	test/expressions/stan_math_sigs_exceptions.expected
# Conflicts:
#	test/expressions/generateExpressionTests.py
#	test/expressions/stan_math_sigs_exceptions.expected
# Conflicts:
#	test/expressions/stan_math_sigs_exceptions.expected
# Conflicts:
#	test/expressions/generateExpressionTests.py
#	test/expressions/stan_math_sigs_exceptions.expected
@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.15 3.14 1.0 0.17% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 1.0 -0.13% slower
eight_schools/eight_schools.stan 0.12 0.11 1.04 3.55% faster
gp_regr/gp_regr.stan 0.17 0.17 0.98 -1.59% slower
irt_2pl/irt_2pl.stan 5.67 5.68 1.0 -0.05% slower
performance.compilation 89.59 87.65 1.02 2.17% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.47 8.48 1.0 -0.16% slower
pkpd/one_comp_mm_elim_abs.stan 29.34 29.81 0.98 -1.61% slower
sir/sir.stan 137.92 140.48 0.98 -1.86% slower
gp_regr/gen_gp_data.stan 0.05 0.04 1.06 5.7% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.97 2.95 1.0 0.48% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.38 0.37 1.01 0.8% faster
arK/arK.stan 1.78 1.78 1.0 -0.04% slower
arma/arma.stan 0.6 0.61 0.99 -0.76% slower
garch/garch.stan 0.75 0.74 1.0 0.46% faster
Mean result: 1.00517276737

Jenkins Console Log
Blue Ocean
Commit hash: 86b293a


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

Copy link
Member

@bbbales2 bbbales2 left a comment

Choose a reason for hiding this comment

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

Good!

@bbbales2 bbbales2 merged commit f299764 into stan-dev:develop Nov 5, 2020
@t4c1 t4c1 deleted the generalize_uniform branch November 30, 2020 09:24
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.

4 participants