<commit_before>#include <stan/math/rev.hpp>
#include <stan/math/prim.hpp>
#include <gtest/gtest.h>
#include <vector>
#include <cmath>

using Eigen::Dynamic;
using Eigen::Matrix;
using stan::math::var;

//  We check that the values of the new regression match those of one built
//  from existing primitives.
TEST(ProbDistributionsNormalIdGLM, glm_matches_normal_id_doubles) {
  Matrix<double, Dynamic, 1> y(3, 1);
  y << 51, 32, 12;
  Matrix<double, Dynamic, Dynamic> x(3, 2);
  x << -12, 46, -42, 24, 25, 27;
  Matrix<double, Dynamic, 1> beta(2, 1);
  beta << 0.3, 2;
  double alpha = 0.3;
  Matrix<double, Dynamic, 1> alphavec = alpha * Matrix<double, 3, 1>::Ones();
  Matrix<double, Dynamic, 1> theta(3, 1);
  theta = x * beta + alphavec;
  double sigma = 10;
  EXPECT_FLOAT_EQ((stan::math::normal_lpdf(y, theta, sigma)),
                  (stan::math::normal_id_glm_lpdf(y, x, alpha, beta, sigma)));
  EXPECT_FLOAT_EQ(
      (stan::math::normal_lpdf<true>(y, theta, sigma)),
      (stan::math::normal_id_glm_lpdf<true>(y, x, alpha, beta, sigma)));
}
//  We check that the values of the new regression match those of one built
//  from existing primitives.
TEST(ProbDistributionsNormalIdGLM, glm_matches_normal_id_doubles_rand) {
  for (size_t ii = 0; ii < 200; ii++) {
    Matrix<double, Dynamic, 1> y(3, 1);
    for (size_t i = 0; i < 3; i++) {
      y[i] = Matrix<unsigned int, 1, 1>::Random(1, 1)[0] % 200;
    }
    Matrix<double, Dynamic, Dynamic> x
        = Matrix<double, Dynamic, Dynamic>::Random(3, 2);
    Matrix<double, Dynamic, 1> beta
        = Matrix<double, Dynamic, Dynamic>::Random(2, 1);
    Matrix<double, 1, 1> alphamat = Matrix<double, 1, 1>::Random(1, 1);
    double alpha = alphamat[0];
    Matrix<double, Dynamic, 1> alphavec = alpha * Matrix<double, 3, 1>::Ones();
    Matrix<double, Dynamic, 1> theta(3, 1);
    theta = x * beta + alphavec;
    Matrix<double, Dynamic, 1> phi
        = Matrix<double, Dynamic, Dynamic>::Random(1, 1)
          + Matrix<double, 1, 1>::Ones();
    EXPECT_FLOAT_EQ(
        (stan::math::normal_lpdf(y, theta, phi[0])),
        (stan::math::normal_id_glm_lpdf(y, x, alpha, beta, phi[0])));
    EXPECT_FLOAT_EQ(
        (stan::math::normal_lpdf<true>(y, theta, phi[0])),
        (stan::math::normal_id_glm_lpdf<true>(y, x, alpha, beta, phi[0])));
  }
}
//  We check that the gradients of the new regression match those of one built
//  from existing primitives.
TEST(ProbDistributionsNormalIdGLM, glm_matches_normal_id_vars) {
  Matrix<var, Dynamic, 1> y(3, 1);
  y << 14, 32, 21;
  Matrix<var, Dynamic, Dynamic> x(3, 2);
  x << -12, 46, -42, 24, 25, 27;
  Matrix<var, Dynamic, 1> beta(2, 1);
  beta << 0.3, 2;
  var alpha = 0.3;
  var sigma = 10;
  Matrix<var, Dynamic, 1> alphavec = alpha * Matrix<double, 3, 1>::Ones();
  Matrix<var, Dynamic, 1> theta(3, 1);
  theta = x * beta + alphavec;
  var lp = stan::math::normal_lpdf(y, theta, sigma);
  lp.grad();

  double lp_val = lp.val();
  double alpha_adj = alpha.adj();
  Matrix<double, Dynamic, Dynamic> x_adj(3, 2);
  Matrix<double, Dynamic, 1> beta_adj(2, 1);
  Matrix<double, Dynamic, 1> y_adj(3, 1);
  for (size_t i = 0; i < 2; i++) {
    beta_adj[i] = beta[i].adj();
    for (size_t j = 0; j < 3; j++) {
      x_adj(j, i) = x(j, i).adj();
    }
  }
  double sigma_adj = sigma.adj();
  for (size_t j = 0; j < 3; j++) {
    y_adj[j] = y[j].adj();
  }

  stan::math::recover_memory();

  Matrix<var, Dynamic, 1> y2(3, 1);
  y2 << 14, 32, 21;
  Matrix<var, Dynamic, Dynamic> x2(3, 2);
  x2 << -12, 46, -42, 24, 25, 27;
  Matrix<var, Dynamic, 1> beta2(2, 1);
  beta2 << 0.3, 2;
  var alpha2 = 0.3;
  var sigma2 = 10;
  var lp2 = stan::math::normal_id_glm_lpdf(y2, x2, alpha2, beta2, sigma2);
  lp2.grad();
  EXPECT_FLOAT_EQ(lp_val, lp2.val());
  for (size_t i = 0; i < 2; i++) {
    EXPECT_FLOAT_EQ(beta_adj[i], beta2[i].adj());
  }
  EXPECT_FLOAT_EQ(alpha_adj, alpha2.adj());
  EXPECT_FLOAT_EQ(sigma_adj, sigma2.adj());
  for (size_t j = 0; j < 3; j++) {
    EXPECT_FLOAT_EQ(y_adj[j], y2[j].adj());
    for (size_t i = 0; i < 2; i++) {
      EXPECT_FLOAT_EQ(x_adj(j, i), x2(j, i).adj());
    }
  }
}

TEST(ProbDistributionsNormalIdGLM, glm_matches_normal_id_simple) {
  double eps = 1e-9;
  Matrix<double, Dynamic, 1> y(3, 1);
  y << 14, 32, 21;
  Matrix<double, Dynamic, Dynamic> x(3, 2);
  x << -12, 46, -42, 24, 25, 27;
  Matrix<double, Dynamic, 1> beta(2, 1);
  beta << 0.3, 2;
  Matrix<var, Dynamic, 1> beta1 = beta;
  Matrix<var, Dynamic, 1> beta2 = beta;
  var alpha1 = 0.3;
  var alpha2 = 0.3;
  double sigma = 10;

  var lp1 = stan::math::normal_id_glm_lpdf(y, x, alpha1, beta1, sigma);
  var lp2 = stan::math::normal_lpdf(y, ((x * beta2).array() +  alpha2).matrix().eval(), sigma);

  EXPECT_NEAR(lp1.val(), lp2.val(), eps);

  (lp1 + lp2).grad();

  for (int i = 0; i < 2; i++) {
    EXPECT_NEAR(beta1[i].adj(), beta2[i].adj(), eps);
  }
  EXPECT_NEAR(alpha1.adj(), alpha2.adj(), eps);
}

TEST(ProbDistributionsNormalIdGLM, broadcast_x) {<commit_msg>[Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/stable/2017-11-14)<commit_after>#include <stan/math/rev.hpp>
#include <stan/math/prim.hpp>
#include <gtest/gtest.h>
#include <vector>
#include <cmath>

using Eigen::Dynamic;
using Eigen::Matrix;
using stan::math::var;

//  We check that the values of the new regression match those of one built
//  from existing primitives.
TEST(ProbDistributionsNormalIdGLM, glm_matches_normal_id_doubles) {
  Matrix<double, Dynamic, 1> y(3, 1);
  y << 51, 32, 12;
  Matrix<double, Dynamic, Dynamic> x(3, 2);
  x << -12, 46, -42, 24, 25, 27;
  Matrix<double, Dynamic, 1> beta(2, 1);
  beta << 0.3, 2;
  double alpha = 0.3;
  Matrix<double, Dynamic, 1> alphavec = alpha * Matrix<double, 3, 1>::Ones();
  Matrix<double, Dynamic, 1> theta(3, 1);
  theta = x * beta + alphavec;
  double sigma = 10;
  EXPECT_FLOAT_EQ((stan::math::normal_lpdf(y, theta, sigma)),
                  (stan::math::normal_id_glm_lpdf(y, x, alpha, beta, sigma)));
  EXPECT_FLOAT_EQ(
      (stan::math::normal_lpdf<true>(y, theta, sigma)),
      (stan::math::normal_id_glm_lpdf<true>(y, x, alpha, beta, sigma)));
}
//  We check that the values of the new regression match those of one built
//  from existing primitives.
TEST(ProbDistributionsNormalIdGLM, glm_matches_normal_id_doubles_rand) {
  for (size_t ii = 0; ii < 200; ii++) {
    Matrix<double, Dynamic, 1> y(3, 1);
    for (size_t i = 0; i < 3; i++) {
      y[i] = Matrix<unsigned int, 1, 1>::Random(1, 1)[0] % 200;
    }
    Matrix<double, Dynamic, Dynamic> x
        = Matrix<double, Dynamic, Dynamic>::Random(3, 2);
    Matrix<double, Dynamic, 1> beta
        = Matrix<double, Dynamic, Dynamic>::Random(2, 1);
    Matrix<double, 1, 1> alphamat = Matrix<double, 1, 1>::Random(1, 1);
    double alpha = alphamat[0];
    Matrix<double, Dynamic, 1> alphavec = alpha * Matrix<double, 3, 1>::Ones();
    Matrix<double, Dynamic, 1> theta(3, 1);
    theta = x * beta + alphavec;
    Matrix<double, Dynamic, 1> phi
        = Matrix<double, Dynamic, Dynamic>::Random(1, 1)
          + Matrix<double, 1, 1>::Ones();
    EXPECT_FLOAT_EQ(
        (stan::math::normal_lpdf(y, theta, phi[0])),
        (stan::math::normal_id_glm_lpdf(y, x, alpha, beta, phi[0])));
    EXPECT_FLOAT_EQ(
        (stan::math::normal_lpdf<true>(y, theta, phi[0])),
        (stan::math::normal_id_glm_lpdf<true>(y, x, alpha, beta, phi[0])));
  }
}
//  We check that the gradients of the new regression match those of one built
//  from existing primitives.
TEST(ProbDistributionsNormalIdGLM, glm_matches_normal_id_vars) {
  Matrix<var, Dynamic, 1> y(3, 1);
  y << 14, 32, 21;
  Matrix<var, Dynamic, Dynamic> x(3, 2);
  x << -12, 46, -42, 24, 25, 27;
  Matrix<var, Dynamic, 1> beta(2, 1);
  beta << 0.3, 2;
  var alpha = 0.3;
  var sigma = 10;
  Matrix<var, Dynamic, 1> alphavec = alpha * Matrix<double, 3, 1>::Ones();
  Matrix<var, Dynamic, 1> theta(3, 1);
  theta = x * beta + alphavec;
  var lp = stan::math::normal_lpdf(y, theta, sigma);
  lp.grad();

  double lp_val = lp.val();
  double alpha_adj = alpha.adj();
  Matrix<double, Dynamic, Dynamic> x_adj(3, 2);
  Matrix<double, Dynamic, 1> beta_adj(2, 1);
  Matrix<double, Dynamic, 1> y_adj(3, 1);
  for (size_t i = 0; i < 2; i++) {
    beta_adj[i] = beta[i].adj();
    for (size_t j = 0; j < 3; j++) {
      x_adj(j, i) = x(j, i).adj();
    }
  }
  double sigma_adj = sigma.adj();
  for (size_t j = 0; j < 3; j++) {
    y_adj[j] = y[j].adj();
  }

  stan::math::recover_memory();

  Matrix<var, Dynamic, 1> y2(3, 1);
  y2 << 14, 32, 21;
  Matrix<var, Dynamic, Dynamic> x2(3, 2);
  x2 << -12, 46, -42, 24, 25, 27;
  Matrix<var, Dynamic, 1> beta2(2, 1);
  beta2 << 0.3, 2;
  var alpha2 = 0.3;
  var sigma2 = 10;
  var lp2 = stan::math::normal_id_glm_lpdf(y2, x2, alpha2, beta2, sigma2);
  lp2.grad();
  EXPECT_FLOAT_EQ(lp_val, lp2.val());
  for (size_t i = 0; i < 2; i++) {
    EXPECT_FLOAT_EQ(beta_adj[i], beta2[i].adj());
  }
  EXPECT_FLOAT_EQ(alpha_adj, alpha2.adj());
  EXPECT_FLOAT_EQ(sigma_adj, sigma2.adj());
  for (size_t j = 0; j < 3; j++) {
    EXPECT_FLOAT_EQ(y_adj[j], y2[j].adj());
    for (size_t i = 0; i < 2; i++) {
      EXPECT_FLOAT_EQ(x_adj(j, i), x2(j, i).adj());
    }
  }
}

TEST(ProbDistributionsNormalIdGLM, glm_matches_normal_id_simple) {
  double eps = 1e-9;
  Matrix<double, Dynamic, 1> y(3, 1);
  y << 14, 32, 21;
  Matrix<double, Dynamic, Dynamic> x(3, 2);
  x << -12, 46, -42, 24, 25, 27;
  Matrix<double, Dynamic, 1> beta(2, 1);
  beta << 0.3, 2;
  Matrix<var, Dynamic, 1> beta1 = beta;
  Matrix<var, Dynamic, 1> beta2 = beta;
  var alpha1 = 0.3;
  var alpha2 = 0.3;
  double sigma = 10;

  var lp1 = stan::math::normal_id_glm_lpdf(y, x, alpha1, beta1, sigma);
  var lp2 = stan::math::normal_lpdf(
      y, ((x * beta2).array() + alpha2).matrix().eval(), sigma);

  EXPECT_NEAR(lp1.val(), lp2.val(), eps);

  (lp1 + lp2).grad();

  for (int i = 0; i < 2; i++) {
    EXPECT_NEAR(beta1[i].adj(), beta2[i].adj(), eps);
  }
  EXPECT_NEAR(alpha1.adj(), alpha2.adj(), eps);
}

TEST(ProbDistributionsNormalIdGLM, broadcast_x) {
{'fraction_non_alphanumeric': 0.18478260869565216, 'fraction_numerical': 0.05195452732349422, 'mean_word_length': 3.6689944134078214, 'pattern_counts': {'":': 0, '<': 161, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1080982', 'n_tokens_mistral': 4540, 'n_tokens_neox': 4052, 'n_words': 1043}