# include # include # include # include # include "discrete_pdf_sample_2d.h" int main ( int argc, char *argv[] ); void test01 ( int n ); void test02 ( int n ); void timestamp ( ); /******************************************************************************/ int main ( int argc, char *argv[] ) /******************************************************************************/ /* Purpose: discrete_pdf_sample_2d_test() tests discrete_pdf_sample_2d(). Discussion: This program is an example of how discrete sample or density data can be used to define a PDF (probability density function). In this function and the functions it calls, we assume that we have data for an array of 20 by 20 square subcells of the unit square. We wish to derive a PDF that will allow us to sample an arbitrary number of points from this region. In particular, we intend to use the discrete data to generate a PDF which we will then use to generate sample points. Roughly speaking, we have kept track of how many fish we caught in each part of a lake, and now we want to simulate catching N fish under the same conditions. The statistics for each simulation should be governed by the discrete PDF, but with random variation. In other words, the actual number of points taken from each subregion is random, and the actual location of each point in a subregion is random, but over many simulations, the statistics of the sample points should reproduce the statistics of the original discrete sample that defined the PDF. Licensing: This code is distributed under the MIT license. Modified: 07 August 2025 Author: John Burkardt */ { int n; timestamp ( ); printf ( "\n" ); printf ( "discrete_pdf_sample_2d_test():\n" ); printf ( " C version\n" ); printf ( " Test discrete_pdf_sample_2d(),\n" ); printf ( " which generates sample data using a discrete PDF.\n" ); n = 1000; test01 ( n ); test02 ( n ); /* Terminate. */ printf ( "\n" ); printf ( "discrete_pdf_sample_2d_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void test01 ( int n ) /******************************************************************************/ /* Purpose: TEST01 looks at a 20x20 region. Licensing: This code is distributed under the MIT license. Modified: 27 June 2013 Author: John Burkardt Input: int N, the number of sample points to be generated. */ { double *cdf; char filename[] = "test01.txt"; int n1; int n2; double *pdf; int seed; double *u; double *xy; printf ( "\n" ); printf ( "TEST01\n" ); printf ( " Consider data skewed toward the upper left corner of the unit square.\n" ); printf ( " Generate %d samples\n", n ); /* Get the dimensions of the PDF data. */ get_discrete_pdf_size1 ( &n1, &n2 ); printf ( " PDF data is on a %d by %d grid.\n", n1, n2 ); /* Construct a PDF from the data. */ pdf = get_discrete_pdf_data1 ( n1, n2 ); /* "Integrate" the data over rows and columns of the region to get the CDF. */ cdf = set_discrete_cdf ( n1, n2, pdf ); /* Choose N CDF values at random. */ seed = 123456789; u = r8vec_uniform_01_new ( n, &seed ); /* Find the cell corresponding to each CDF value, and choose a random point in that cell. */ xy = discrete_cdf_to_xy ( n1, n2, cdf, n, u, &seed ); /* Write data to a file for examination, plotting, or analysis. */ r8mat_write ( filename, 2, n, xy ); printf ( "\n" ); printf ( " Wrote sample data to file \"%s\".\n", filename ); /* Free memory. */ free ( cdf ); free ( pdf ); free ( u ); free ( xy ); return; } /******************************************************************************/ void test02 ( int n ) /******************************************************************************/ /* Purpose: TEST02 looks at a 12x8 region. Licensing: This code is distributed under the MIT license. Modified: 27 June 2013 Author: John Burkardt Input: int N, the number of sample points to be generated. */ { double *cdf; char filename[] = "test02.txt"; int n1; int n2; double *pdf; int seed; double *u; double *xy; printf ( "\n" ); printf ( "TEST02\n" ); printf ( " Consider data suggested by the shape and density of Iowa.\n" ); printf ( " Generate %d samples\n", n ); /* Get the dimensions of the PDF data. */ get_discrete_pdf_size2 ( &n1, &n2 ); printf ( " PDF data is on a %d by %d grid.\n", n1, n2 ); /* Construct a PDF from the data. */ pdf = get_discrete_pdf_data2 ( n1, n2 ); /* "Integrate" the data over rows and columns of the region to get the CDF. */ cdf = set_discrete_cdf ( n1, n2, pdf ); /* Choose N CDF values at random. */ seed = 123456789; u = r8vec_uniform_01_new ( n, &seed ); /* Find the cell corresponding to each CDF value, and choose a random point in that cell. */ xy = discrete_cdf_to_xy ( n1, n2, cdf, n, u, &seed ); /* Write data to a file for examination, plotting, or analysis. */ r8mat_write ( filename, 2, n, xy ); printf ( "\n" ); printf ( " Wrote sample data to file \"%s\".\n", filename ); /* Free memory. */ free ( cdf ); free ( pdf ); free ( u ); free ( xy ); return; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }