TEST(ProbDistributionsNegBinomial, chiSquareGoodnessFitTest3) {
  boost::random::mt19937 rng;
  double p = 0.2;
  double alpha = 0.4;
  double beta = p / (1 - p);
  int N = 1000;
  int K = boost::math::round(2 * std::pow(N, (1-p)));
  boost::math::chi_squared mydist(K-1);

  int loc[K - 1];
  for(int i = 1; i < K; i++)
    loc[i - 1] = i - 1;

  int count = 0;
  double bin [K];
  double expect [K];

  for(int i = 0 ; i < K; i++)
    bin[i] = 0;
  expected_bin_sizes(expect, K, N, alpha, beta);

  while (count < N) {
    int a = stan::math::neg_binomial_rng(alpha, beta, rng);
    int i = 0;
    while (i < K-1 && a > loc[i])
      ++i;
    ++bin[i];
    count++;
   }

  double chi = 0;

  for(int j = 0; j < K; j++)
    chi += ((bin[j] - expect[j]) * (bin[j] - expect[j]) / expect[j]);

  EXPECT_TRUE(chi < boost::math::quantile(boost::math::complement(mydist, 1e-6)));
}
Beispiel #2
0
TEST(ProbDistributionsBinomial, chiSquareGoodnessFitTest) {
  boost::random::mt19937 rng;
  int N = 10000;
  int K = boost::math::round(2 * std::pow(N, 0.4));
  boost::math::binomial_distribution<>dist (100,0.6);
  boost::math::chi_squared mydist(K-1);

  int loc[K - 1];
  for(int i = 1; i < K; i++)
    loc[i - 1] = i - 1;

  int count = 0;
  int bin [K];
  double expect [K];
  for(int i = 0 ; i < K; i++) {
    bin[i] = 0;
    expect[i] = N * pdf(dist, i);
  }
  expect[K-1] = N * (1 - cdf(dist, K-2));

  while (count < N) {
    int a = stan::math::binomial_rng(100,0.6,rng);
    int i = 0;
    while (i < K-1 && a > loc[i]) 
      ++i;
    ++bin[i];
    count++;
   }

  double chi = 0;

  for(int j = 0; j < K; j++)
    chi += ((bin[j] - expect[j]) * (bin[j] - expect[j]) / expect[j]);

  EXPECT_TRUE(chi < quantile(complement(mydist, 1e-6)));
}
Beispiel #3
0
TEST(ProbDistributionsHypergeometric, chiSquareGoodnessFitTest) {
  boost::random::mt19937 rng;
  int N = 10000;
  int num_draws = 10;
  int K = num_draws;
  boost::math::hypergeometric_distribution<>dist (15, num_draws, 25);
  boost::math::chi_squared mydist(K-1);

  std::vector<int> loc(K - 1);
  for(int i = 1; i < K; i++)
    loc[i - 1] = i - 1;

  int count = 0;
  std::vector<int> bin(K);
  std::vector<double> expect(K);
  for(int i = 0 ; i < K; i++) {
    bin[i] = 0;
    expect[i] = N * pdf(dist, i);
  }

  while (count < N) {
    int a = stan::math::hypergeometric_rng(num_draws, 10, 15,rng);
    int i = 0;
    while (i < K-1 && a > loc[i]) 
      ++i;
    ++bin[i];
    count++;
   }

  double chi = 0;

  for(int j = 0; j < K; j++)
    chi += ((bin[j] - expect[j]) * (bin[j] - expect[j]) / expect[j]);

  EXPECT_TRUE(chi < quantile(complement(mydist, 1e-6)));
}
Beispiel #4
0
TEST(ProbDistributionGamma, chiSquareGoodnessFitTest) {
  boost::random::mt19937 rng;
  int N = 10000;
  int K = boost::math::round(2 * std::pow(N, 0.4));
  boost::math::gamma_distribution<>dist (2.0,1.0);
  boost::math::chi_squared mydist(K-1);

  double loc[K - 1];
  for(int i = 1; i < K; i++)
    loc[i - 1] = quantile(dist, i * std::pow(K, -1.0));

  int count = 0;
  int bin [K];
  double expect [K];
  for(int i = 0 ; i < K; i++)
  {
    bin[i] = 0;
    expect[i] = N / K;
  }

  while (count < N) {
    double a = stan::prob::gamma_rng(2.0,1.0,rng);
    int i = 0;
    while (i < K-1 && a > loc[i]) 
  ++i;
    ++bin[i];
    count++;
   }

  double chi = 0;

  for(int j = 0; j < K; j++)
    chi += ((bin[j] - expect[j]) * (bin[j] - expect[j]) / expect[j]);

  EXPECT_TRUE(chi < quantile(complement(mydist, 1e-6)));
}
void idlefunc(void)
{
   int j , k , kmin ;
   //
   double d , dmin ;
   //
   for( j = 0 ; j < N ; j++ )
   {
      arr[j].x += ( arr[j].v * cos( arr[j].t ) * DT ) ;
      arr[j].y += ( arr[j].v * sin( arr[j].t ) * DT ) ;
      //
      // bounce
      //
      if( arr[j].y - R < 0.0 )
      {
         arr[j].y = R ;
         //
         arr[j].t *= -1.0        ;
         arr[j].t +=  2.0 * M_PI ;
      }
      else if( arr[j].y + R > 1.0 )
      {
         arr[j].y = 1.0 - R ;
         //
         arr[j].t *= -1.0        ;
         arr[j].t +=  2.0 * M_PI ;
      }
      //
      if( arr[j].x - R < 0.0 )
      {
         arr[j].x = R ;
         //
         arr[j].t *= -1.0        ;
         arr[j].t +=  1.0 * M_PI ;
      }
      else if( arr[j].x + R > 1.0 )
      {
         arr[j].x = 1.0 - R ;
         //
         arr[j].t *= -1.0        ;
         arr[j].t +=  1.0 * M_PI ;
      }
   }
   //
   //
   //
   for( j = 0 ; j < N ; j++ )
   {
      kmin = j ;
      //
      for( k = 0 ; k < N ; k++ )
      {
         if( k != j )
         {
            d = mydist( arr[j].x , arr[j].y , arr[k].x , arr[k].y ) ;
            //
            if( kmin == j || d < dmin )
            {
               kmin = k ;
               dmin = d ;
            }
         }
      }
      //
      nbr[j] = kmin ;
   }
   //
   //
   //===================================================================================================================
   for( j = 0 ; j < N ; j++ )
   {
      k = nbr[j] ;
      double alpha = 0.05;
      //
      if( k == j ) // no nbr
      {
         arr[j].dv = 0.0 ;
         arr[j].dt = 0.0 ;
      }
      else
      {
         // ======================= Velocity =================================
         printf("t1: %f t2: %f tv: %f\n",arr[j].t, arr[k].t, arr[j].t - arr[k].t);
         arr[j].dv = (arr[k].v - arr[j].v)*alpha;
         //
         // =======================   Angle  =================================
         if( abs(arr[j].t - arr[k].t) > M_PI )
         {
            printf("GREAT\n");
            double a1 = arr[j].t;
            double a2 = arr[k].t;
            double change = 0;
            if( arr[j].t > M_PI )
            {
                change = abs( arr[k].t - (M_PI - arr[j].t) );
            }
            if( arr[k].t > M_PI )
            {
                change = -abs( arr[j].t - (M_PI - arr[k].t) );
            }
            arr[j].dt = change*alpha;
         }
         else
         {
             arr[j].dt = (arr[k].t - arr[j].t)*alpha;
         }
         //
      }
   }
   //===================================================================================================================
   for( j = 0 ; j < N ; j++ )
   {
      arr[j].v += arr[j].dv ;
      if( arr[j].v > 0.15 ) arr[j].v = 0.15 ;
      if( arr[j].v < 0.05 ) arr[j].v = 0.05 ;
      //
      arr[j].t += arr[j].dt ;
      if( arr[j].t > 2.0 * M_PI ) arr[j].t -= ( 2.0 * M_PI ) ;
      if( arr[j].t < 0.0        ) arr[j].t += ( 2.0 * M_PI ) ;
   }
   //
   glutPostRedisplay() ;
}
Beispiel #6
0
TEST(ProbDistributionsFrechet, chiSquareGoodnessFitTest_2) {
  boost::random::mt19937 rng;
  int N = 10000;
  int K = boost::math::round(2 * std::pow(N, 0.4));
  boost::math::chi_squared mydist(K-1);

  // compare rng to values generated in R
  stan::math::vector_d loc(K - 1);
  loc << 0.0955415954270533857029, 0.1041316533397634719327, 0.1103740935559852087700,
    0.1155522740053754326972, 0.1201122408786449852203, 0.1242675441002982356098,
    0.1281388937447018039339, 0.1318020457964521607863, 0.1353081473284461211382,
    0.1386936692085097289073, 0.1419857581568354232271, 0.1452053426999067309300,
    0.1483690435024691001153, 0.1514904048409720094259, 0.1545807200109403090060,
    0.1576496031786457641122, 0.1607053971093497513056, 0.1637554713779679482766,
    0.1668064455827929504217, 0.1698643600576038303895, 0.1729348091403954956746,
    0.1760230473253571181758, 0.1791340755387774430485, 0.1822727127227262711173,
    0.1854436565133893710655, 0.1886515358368690276070, 0.1919009575713397086627,
    0.1951965489490360139424, 0.1985429970347641626116, 0.2019450863797647710562,
    0.2054077357842012396816, 0.2089360349903569558094, 0.2125352820597541936287,
    0.2162110221529071896196, 0.2199690884253143186022, 0.2238156457748539107655,
    0.2277572382230414749227, 0.2318008407862954534107, 0.2359539167955838900870,
    0.2402244817572899704405, 0.2446211750202796908482, 0.2491533407314772130547,
    0.2538311198348010000458, 0.2586655552093837240335, 0.2636687124701073692279,
    0.2688538194897237554315, 0.2742354283777497792052, 0.2798296045075444049566,
    0.2856541482738663706442, 0.2917288566621027978698, 0.2980758335169671480180,
    0.3047198597496216798675, 0.3116888378074926801986, 0.3190143288159938972370,
    0.3267322062618903677489, 0.3348834574628689608744, 0.3435151741391668234193,
    0.3526817873222083399298, 0.3624466213226111843682, 0.3728838691486778267326,
    0.3840811316557718457787, 0.3961427211814618765118, 0.4091940177230903863403,
    0.4233872987100369411628, 0.4389096706055364283117, 0.4559940614596232899558,
    0.4749347769469325353242, 0.4961100434630218436460, 0.5200155800775090320087,
    0.5473162043798307507814, 0.5789281712257871026495, 0.6161565249522205078847,
    0.6609369681056525003271, 0.7162914824634227795030, 0.7872641783601075360366,
    0.8830792885403591085947, 1.0230050174489953018764, 1.2569469392970760157624,
    1.7832436936202971100585;


  int count = 0;
  int bin [K];
  double expect [K];
  for(int i = 0 ; i < K; i++) {
    bin[i] = 0;
    expect[i] = N / K;
  }

  while (count < N) {
    double a = stan::math::frechet_rng(2.0,0.2,rng);
    int i = 0;
    while (i < K-1 && a > loc[i]) 
      ++i;
    ++bin[i];
    count++;
   }

  double chi = 0;

  for(int j = 0; j < K; j++)
    chi += ((bin[j] - expect[j]) * (bin[j] - expect[j]) / expect[j]);

  EXPECT_TRUE(chi < quantile(complement(mydist, 1e-6)));
}