Numerical Recipes Forum  

Go Back   Numerical Recipes Forum > Numerical Recipes Third Edition Forum > Methods: All Chapters in NR3

Reply
 
Thread Tools Rating: Thread Rating: 6 votes, 5.00 average. Display Modes
  #1  
Old 05-16-2011, 03:24 PM
firex firex is offline
Registered User
 
Join Date: May 2011
Posts: 2
KS test help

I'm a university student trying to write a C++ Kolmogorov - Smirnov Test program with the programs I downloaded for the website. The problem is, I don't have much experience in programming. I'll display my code below (it won't compile). I don't really know how to make this work and I think I have all of the #include. Any help would be appreciated.

#include <math.h>
#include <iostream>
#include "nr3.h"
#include "sort.h"
#include "moment.h"
#include "gamma.h"
#include "erf.h"
#include "incgammabeta.h"
#include "stattests.h"
#include "quadvl.h"
#include "ksdist.h"
#include "kstests_2d.h"

using namespace std;

int main()
{
int i, j;
cout << "Enter first variable: ";
cin >> i;
cout << "Enter second variable: ";
cin >> j;

ks2d1s(i);
ks2d2s(j);

cout << "Result: " << i << ", " << j << endl;
return 0;
}
Reply With Quote
  #2  
Old 05-17-2011, 09:18 AM
MPD78 MPD78 is offline
Registered User
 
Join Date: Dec 2008
Location: Pittsburgh, PA
Posts: 196
ksdist is a struct.

You have to instance the struct first.

Here is an example:

ksdist myksdist;

To call the functions in ksdist you do the following

Doub result = myksdist.pks(your integer value)

To get the results from ksdist you do the following

cout << "Result " << result << endl;

Also, the only #include that you need for ksdist is nr3.h

Hope that helps.

Thanks
Reply With Quote
  #3  
Old 05-17-2011, 09:56 AM
davekw7x davekw7x is offline
Registered User
 
Join Date: Jan 2008
Posts: 453
One way to get things to compile: Put the code in a directory at the same level as the nr3 stuff, and use relative paths in the include directives.

So, on my workstation the nr3 stuff is in a directory /home/dave/nr3/code, and the application program is /home/dave/nr3/xks2d1s/xks2d1s.cpp

Then...

Code:
// Example for testing ks2d1s
//
// davekw7x
//
#include "../code/nr3.h"
#include "../code/gamma.h"
#include "../code/sort.h"
#include "../code/moment.h"
#include "../code/incgammabeta.h"
#include "../code/erf.h"
#include "../code/stattests.h"
#include "../code/quadvl.h"
#include "../code/ksdist.h"
#include "../code/kstests_2d.h"
#include "../code/ran.h"

int main() 
{
    Int num_points, num_trials;
    Doub factor;

    // For debugging, you might want to use the same
    // seed each time so that you can evaluate the
    // effects of program changes
    Int seed = 12345678;

    //
    // For repeated testing, you might want to use
    // a different seed each time
    //
    //Int seed = time(0);
    Ran ran(seed);

    cout << "Enter the number of points (Must be a greater than 2): ";
    //
    // Terminate the program if the user enters something non-numeric
    // or if the value is less than three
    //
    while ((cin >> num_points) && (num_points > 2)) {

        //
        // Low values of the factor makes the point distribution
        // be jointly uniform. As factor gets larger, the
        // distribution gets "less and less" jointly uniform
        //

        // Terminate the loop and exit the program if the user
        // makes a non-numeric entry, otherwise make sure an
        // appropriate value is entered, then proceed.
        //
        cout << "Enter the non-linearity factor (0.0 to 1.0)          : ";
        while ((cin >> factor) && ((factor < 0) || (factor > 1.0))) {

            if (factor < 0.0) {
                cout << "Factor can not be less than 0." << endl;
            }
            if (factor > 1.0) {
                cout << "Factor can not be greater than 1." << endl;
            }
            cout << endl;
            cout << "Enter the non-linearity factor (0.0 to 1.0)          : ";
        }
        if (!cin) {
            break;
        }

        //
        // Terminate the loop and exit the program if the user
        // makes a non-numeric entry, otherwise make sure an
        // appropriate value is entered, then proceed.
        //
        cout << "Enter the number of trials                           : ";
        while ((cin >> num_trials) && (num_trials <= 0)) {

            cout << "Must be a positive integer."
                 << endl << endl;
            cout << "Enter the number of trials                           : ";
        }
        if (!cin) {
            break;
        }

        cout << endl << endl
             << "The null hypothesis is that the distribution of the"
             << endl
             << "points (x,y) is jointly uniform for x in [-1,1] and"
             << endl
             << "and y in [-1,1]"
             << endl << endl
             << "If the probability number in the table is small, then"
             << endl
             << "we reject the null hypothesis, and we believe that the"
             << endl
             << "distribution is not jointly uniform in the given region."
             << endl << endl;

        cout << "                 Probability that D is greater"
             << endl
             << "K-S D value           than the K-S D value"
             << endl
             << "---------------------------------------------------"
             << endl;

        VecDoub x(num_points), y(num_points);
        for (Int i  = 0; i < num_trials; i++) {
            for (Int j = 0; j < num_points; j++) {

                Doub rand_temp = ran.doub();

                rand_temp = rand_temp * ((1.0 - factor) + rand_temp * factor);
                x[j] = 2.0 * rand_temp - 1.0;

                rand_temp = ran.doub();
                rand_temp = rand_temp * ((1.0 - factor) + rand_temp * factor);
                y[j] = 2.0 * rand_temp - 1.0;

            }

            Doub d, prob;
            ks2d1s(x, y, quadvl, d, prob);
            cout << fixed      << setprecision(6) << setw(9)  << d
                 << scientific << setprecision(2) << setw(24) << prob
                 << endl;
        }
        cout << endl << endl
             << "========================================================="
             << endl << endl;

        cout << "Enter the number of points (Must be a greater than 2): ";
    }
    if (cin) {
        cout << "Goodbye for now." << endl;
    }
    else {
        cout << "Invalid entry. Program is ending." << endl;
    }

    return 0;
}
With GNU compilers, the command line is

g++ -Wall -W xks2d1s.cpp -o xks2d1s

Here's a run:
Code:
Enter the number of points (Must be a greater than 2): 100
Enter the non-linearity factor (0.0 to 1.0)          : 0.1
Enter the number of trials                           : 10


The null hypothesis is that the distribution of the
points (x,y) is jointly uniform for x in [-1,1] and
and y in [-1,1]

If the probability number in the table is small, then
we reject the null hypothesis, and we believe that the
distribution is not jointly uniform in the given region.

                 Probability that D is greater
K-S D value           than the K-S D value
---------------------------------------------------
 0.140249                1.14e-01
 0.122477                2.27e-01
 0.110910                3.33e-01
 0.074685                8.14e-01
 0.121303                2.37e-01
 0.087340                6.37e-01
 0.129655                1.75e-01
 0.165833                3.65e-02
 0.100479                4.53e-01
 0.143230                1.01e-01


=========================================================

Enter the number of points (Must be a greater than 2): 100
Enter the non-linearity factor (0.0 to 1.0)          : .9
Enter the number of trials                           : 10


The null hypothesis is that the distribution of the
points (x,y) is jointly uniform for x in [-1,1] and
and y in [-1,1]

If the probability number in the table is small, then
we reject the null hypothesis, and we believe that the
distribution is not jointly uniform in the given region.

                 Probability that D is greater
K-S D value           than the K-S D value
---------------------------------------------------
 0.348430                4.35e-08
 0.360526                1.22e-08
 0.338268                1.18e-07
 0.374735                2.93e-09
 0.407322                7.10e-11
 0.296730                5.77e-06
 0.289877                1.02e-05
 0.359207                1.45e-08
 0.298212                5.00e-06
 0.292582                8.11e-06


=========================================================

Enter the number of points (Must be a greater than 2): 0
Goodbye for now.

Regards,

Dave

Footnote:
This example was derived from the Numerical Recipes legacy C++ version 2 program test program xks2d1s.cpp

Can also be compiled from a command line with recent Microsoft compilers:

cl xk2d1s.cpp /EHsc

or Borland:

bcc32 xk2d1s.cpp
Reply With Quote
  #4  
Old 05-17-2011, 02:29 PM
firex firex is offline
Registered User
 
Join Date: May 2011
Posts: 2
Thank you dave, that really helps! It compiled and runs perfectly, thanks again!
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 12:27 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.