Changeset 80723
 Timestamp:
 Sep 27, 2012, 4:19:08 PM (6 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/libs/math/doc/sf_and_dist/distributions/students_t_examples.qbk
r51455 r80723 6 6 Let's say you have a sample mean, you may wish to know what confidence intervals 7 7 you can place on that mean. Colloquially: "I want an interval that I can be 8 P% sure contains the true mean". (On a technical point, note that 9 the interval either contains the true mean or it does not: the 10 meaning of the confidence level is subtly 11 different from this colloquialism. More background information can be found on the 8 P% sure contains the true mean". (On a technical point, note that 9 the interval either contains the true mean or it does not: the 10 meaning of the confidence level is subtly 11 different from this colloquialism. More background information can be found on the 12 12 [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm NIST site]). 13 13 … … 16 16 [equation dist_tutorial4] 17 17 18 Where, ['Y[sub s]] is the sample mean, /s/ is the sample standard deviation, 19 /N/ is the sample size, /[alpha]/ is the desired significance level and 18 Where, ['Y[sub s]] is the sample mean, /s/ is the sample standard deviation, 19 /N/ is the sample size, /[alpha]/ is the desired significance level and 20 20 ['t[sub ([alpha]/2,N1)]] is the upper critical value of the Studentst 21 21 distribution with /N1/ degrees of freedom. … … 28 28 The confidence level of the test is defined as 1  [alpha], and often expressed 29 29 as a percentage. So for example a significance level of 0.05, is equivalent 30 to a 95% confidence level. Refer to 31 [@http://www.itl.nist.gov/div898/handbook/prc/section1/prc14.htm 30 to a 95% confidence level. Refer to 31 [@http://www.itl.nist.gov/div898/handbook/prc/section1/prc14.htm 32 32 "What are confidence intervals?"] in __handbook for more information. 33 33 ] [/ Note] … … 50 50 [@../../../example/students_t_single_sample.cpp students_t_single_sample.cpp]. 51 51 52 We'll begin by defining a procedure to calculate intervals for 52 We'll begin by defining a procedure to calculate intervals for 53 53 various confidence levels; the procedure will print these out 54 54 as a table: … … 60 60 // Bring everything into global namespace for ease of use: 61 61 using namespace boost::math; 62 using namespace std; 62 using namespace std; 63 63 64 64 void confidence_limits_on_mean( … … 71 71 72 72 // Print out general info: 73 cout << 73 cout << 74 74 "__________________________________\n" 75 75 "2Sided Confidence Limits For Mean\n" … … 83 83 84 84 double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 }; 85 85 86 86 Note that these are the complements of the confidence/probability levels: 0.5, 0.75, 0.9 .. 0.99999). 87 87 … … 98 98 99 99 double T = quantile(complement(dist, alpha[i] / 2)); 100 100 101 101 Note that alpha was divided by two, since we'll be calculating 102 102 both the upper and lower bounds: had we been interested in a single 103 103 sided interval then we would have omitted this step. 104 104 105 105 Now to complete the picture, we'll get the (onesided) width of the 106 106 interval from the tstatistic … … 116 116 Let's take a look at some sample output, first using the 117 117 [@http://www.itl.nist.gov/div898/handbook/eda/section4/eda428.htm 118 Heat flow data] from the NIST site. The data set was collected 119 by Bob Zarr of NIST in January, 1990 from a heat flow meter 118 Heat flow data] from the NIST site. The data set was collected 119 by Bob Zarr of NIST in January, 1990 from a heat flow meter 120 120 calibration and stability analysis. 121 121 The corresponding dataplot 122 output for this test can be found in 123 [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm 122 output for this test can be found in 123 [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm 124 124 section 3.5.2] of the __handbook. 125 125 … … 147 147 99.999 4.537 7.404e003 9.25406 9.26886 148 148 '''] 149 149 150 150 As you can see the large sample size (195) and small standard deviation (0.023) 151 151 have combined to give very small intervals, indeed we can be … … 156 156 and from Statistics for Analytical Chemistry, 3rd ed. (1994), pp 5455 157 157 J. C. Miller and J. N. Miller, Ellis Horwood ISBN 0 13 0309907.] 158 The values result from the determination of mercury by coldvapour 158 The values result from the determination of mercury by coldvapour 159 159 atomic absorption. 160 160 … … 191 191 [section:tut_mean_test Testing a sample mean for difference from a "true" mean] 192 192 193 When calibrating or comparing a scientific instrument or measurement method of some kind, 193 When calibrating or comparing a scientific instrument or measurement method of some kind, 194 194 we want to be answer the question "Does an observed sample mean differ from the 195 195 "true" mean in any significant way?". If it does, then we have evidence of 196 196 a systematic difference. This question can be answered with a Studentst test: 197 more information can be found 198 [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm 197 more information can be found 198 [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm 199 199 on the NIST site]. 200 200 … … 214 214 but statisticans eschew this to avoid implying that there is positive evidence of 'no difference'. 215 215 'Notrejected' here means there is *no evidence* of difference, but there still might well be a difference. 216 For example, see [@http://en.wikipedia.org/wiki/Argument_from_ignorance argument from ignorance] and 216 For example, see [@http://en.wikipedia.org/wiki/Argument_from_ignorance argument from ignorance] and 217 217 [@http://www.bmj.com/cgi/content/full/311/7003/485 Absence of evidence does not constitute evidence of absence.] 218 218 ] [/ note] … … 225 225 // Bring everything into global namespace for ease of use: 226 226 using namespace boost::math; 227 using namespace std; 227 using namespace std; 228 228 229 229 void single_sample_t_test(double M, double Sm, double Sd, unsigned Sn, double alpha) … … 235 235 // Sn = Sample Size. 236 236 // alpha = Significance Level. 237 237 238 238 Most of the procedure is prettyprinting, so let's just focus on the 239 239 calculation, we begin by calculating the tstatistic: … … 246 246 double t_stat = diff * sqrt(double(Sn)) / Sd; 247 247 248 Finally calculate the probability from the tstatistic. If we're interested 248 Finally calculate the probability from the tstatistic. If we're interested 249 249 in simply whether there is a difference (either less or greater) or not, 250 250 we don't care about the sign of the tstatistic, … … 254 254 students_t dist(v); 255 255 double q = cdf(complement(dist, fabs(t_stat))); 256 256 257 257 The procedure then prints out the results of the various tests 258 that can be done, these 258 that can be done, these 259 259 can be summarised in the following table: 260 260 … … 262 262 [[Hypothesis][Test]] 263 263 [[The Nullhypothesis: there is 264 *no difference* in means] 264 *no difference* in means] 265 265 [Reject if complement of CDF for t < significance level / 2: 266 266 … … 268 268 269 269 [[The Alternativehypothesis: there 270 *is difference* in means] 270 *is difference* in means] 271 271 [Reject if complement of CDF for t > significance level / 2: 272 272 … … 274 274 275 275 [[The Alternativehypothesis: the sample mean *is less* than 276 the true mean.] 276 the true mean.] 277 277 [Reject if CDF of t > significance level: 278 278 … … 280 280 281 281 [[The Alternativehypothesis: the sample mean *is greater* than 282 the true mean.] 282 the true mean.] 283 283 [Reject if complement of CDF of t > significance level: 284 284 … … 290 290 and against `alpha` for a onesided test] 291 291 292 Now that we have all the parts in place, let's take a look at some 292 Now that we have all the parts in place, let's take a look at some 293 293 sample output, first using the 294 294 [@http://www.itl.nist.gov/div898/handbook/eda/section4/eda428.htm 295 Heat flow data] from the NIST site. The data set was collected 296 by Bob Zarr of NIST in January, 1990 from a heat flow meter 295 Heat flow data] from the NIST site. The data set was collected 296 by Bob Zarr of NIST in January, 1990 from a heat flow meter 297 297 calibration and stability analysis. The corresponding dataplot 298 output for this test can be found in 299 [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm 298 output for this test can be found in 299 [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm 300 300 section 3.5.2] of the __handbook. 301 301 … … 335 335 and from Statistics for Analytical Chemistry, 3rd ed. (1994), pp 5455 336 336 J. C. Miller and J. N. Miller, Ellis Horwood ISBN 0 13 0309907.] 337 The values result from the determination of mercury by coldvapour 337 The values result from the determination of mercury by coldvapour 338 338 atomic absorption. 339 339 … … 364 364 in the location of the true mean. So even though there appears to be a difference 365 365 between the sample mean and the expected true mean, we conclude that there 366 is no significant difference, and are unable to reject the null hypothesis. 367 However, if we were to lower the bar for acceptance down to alpha = 0.1 366 is no significant difference, and are unable to reject the null hypothesis. 367 However, if we were to lower the bar for acceptance down to alpha = 0.1 368 368 (a 90% confidence level) we see a different output: 369 369 … … 407 407 can provide this information. 408 408 409 This section is based on the example code in 409 This section is based on the example code in 410 410 [@../../../example/students_t_single_sample.cpp students_t_single_sample.cpp] 411 411 and we begin by defining a procedure that will print out a table of … … 418 418 // Bring everything into global namespace for ease of use: 419 419 using namespace boost::math; 420 using namespace std; 420 using namespace std; 421 421 422 422 void single_sample_find_df( … … 448 448 The first argument is the difference between the means that you 449 449 wish to be able to detect, here it's the absolute value of the 450 difference between the sample mean, and the true mean. 450 difference between the sample mean, and the true mean. 451 451 452 452 Then come two probability values: alpha and beta. Alpha is the … … 454 454 in fact true. Beta is the maximum acceptable risk of failing to reject 455 455 the nullhypothesis when in fact it is false. 456 Also note that for a twosided test, alpha must be divided by 2. 456 Also note that for a twosided test, alpha must be divided by 2. 457 457 458 458 The final parameter of the function is the standard deviation of the sample. … … 488 488 and from Statistics for Analytical Chemistry, 3rd ed. (1994), pp 5455 489 489 J. C. Miller and J. N. Miller, Ellis Horwood ISBN 0 13 0309907.] 490 The values result from the determination of mercury by coldvapour 491 atomic absorption. 490 The values result from the determination of mercury by coldvapour 491 atomic absorption. 492 492 493 493 Only three measurements were made, and the Studentst test above … … 519 519 '''] 520 520 521 So in this case, many more measurements would have had to be made, 521 So in this case, many more measurements would have had to be made, 522 522 for example at the 95% level, 14 measurements in total for a twosided test. 523 523 … … 529 529 determining whether a new process or treatment is better than an old one. 530 530 531 In this example, we'll be using the 531 In this example, we'll be using the 532 532 [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda3531.htm 533 Car Mileage sample data] from the 533 Car Mileage sample data] from the 534 534 [@http://www.itl.nist.gov NIST website]. The data compares 535 535 miles per gallon of US cars with miles per gallon of Japanese cars. 536 536 537 The sample code is in 537 The sample code is in 538 538 [@../../../example/students_t_two_samples.cpp students_t_two_samples.cpp]. 539 539 … … 542 542 If the standard deviations are assumed to be equal, then the calculation 543 543 of the tstatistic is greatly simplified, so we'll examine that case first. 544 In real life we should verify whether this assumption is valid with a 544 In real life we should verify whether this assumption is valid with a 545 545 ChiSquared test for equal variances. 546 546 … … 565 565 double alpha) // alpha = Significance Level. 566 566 { 567 567 568 568 569 569 Our procedure will begin by calculating the tstatistic, assuming … … 572 572 [equation dist_tutorial1] 573 573 574 where Sp is the "pooled" standard deviation of the two samples, 574 where Sp is the "pooled" standard deviation of the two samples, 575 575 and /v/ is the number of degrees of freedom of the two combined 576 576 samples. We can now write the code to calculate the tstatistic: … … 581 581 // Pooled variance: 582 582 double sp = sqrt(((Sn11) * Sd1 * Sd1 + (Sn21) * Sd2 * Sd2) / v); 583 cout << setw(55) << left << "Pooled Standard Deviation" << "= " << v<< "\n";583 cout << setw(55) << left << "Pooled Standard Deviation" << "= " << sp << "\n"; 584 584 // tstatistic: 585 585 double t_stat = (Sm1  Sm2) / (sp * sqrt(1.0 / Sn1 + 1.0 / Sn2)); 586 586 cout << setw(55) << left << "T Statistic" << "= " << t_stat << "\n"; 587 587 588 588 The next step is to define our distribution object, and calculate the 589 589 complement of the probability: … … 591 591 students_t dist(v); 592 592 double q = cdf(complement(dist, fabs(t_stat))); 593 cout << setw(55) << left << "Probability that difference is due to chance" << "= " 593 cout << setw(55) << left << "Probability that difference is due to chance" << "= " 594 594 << setprecision(3) << scientific << 2 * q << "\n\n"; 595 595 … … 603 603 [[Hypothesis][Test]] 604 604 [[The Nullhypothesis: there is 605 *no difference* in means] 605 *no difference* in means] 606 606 [Reject if complement of CDF for t < significance level / 2: 607 607 … … 609 609 610 610 [[The Alternativehypothesis: there is a 611 *difference* in means] 611 *difference* in means] 612 612 [Reject if complement of CDF for t > significance level / 2: 613 613 … … 615 615 616 616 [[The Alternativehypothesis: Sample 1 Mean is *less* than 617 Sample 2 Mean.] 617 Sample 2 Mean.] 618 618 [Reject if CDF of t > significance level: 619 619 … … 621 621 622 622 [[The Alternativehypothesis: Sample 1 Mean is *greater* than 623 Sample 2 Mean.] 623 Sample 2 Mean.] 624 624 625 625 [Reject if complement of CDF of t > significance level: … … 634 634 skip over that, and take a look at the sample output for alpha=0.05 635 635 (a 95% probability level). For comparison the dataplot output 636 for the same data is in 636 for the same data is in 637 637 [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm 638 638 section 1.3.5.3] of the __handbook. … … 644 644 645 645 Number of Observations (Sample 1) = 249 646 Sample 1 Mean = 20.14 458647 Sample 1 Standard Deviation = 6.4147 0646 Sample 1 Mean = 20.145 647 Sample 1 Standard Deviation = 6.4147 648 648 Number of Observations (Sample 2) = 79 649 Sample 2 Mean = 30.481 01650 Sample 2 Standard Deviation = 6.1077 1651 Degrees of Freedom = 326 .00000652 Pooled Standard Deviation = 326.00000653 T Statistic = 12.62 059649 Sample 2 Mean = 30.481 650 Sample 2 Standard Deviation = 6.1077 651 Degrees of Freedom = 326 652 Pooled Standard Deviation = 6.3426 653 T Statistic = 12.621 654 654 Probability that difference is due to chance = 5.273e030 655 655 … … 667 667 The tests on the alternative hypothesis show that we must 668 668 also reject the hypothesis that Sample 1 Mean is 669 greater than that for Sample 2: in this case Sample 1 represents the 670 miles per gallon for Japanese cars, and Sample 2 the miles per gallon for 669 greater than that for Sample 2: in this case Sample 1 represents the 670 miles per gallon for Japanese cars, and Sample 2 the miles per gallon for 671 671 US cars, so we conclude that Japanese cars are on average more 672 672 fuel efficient. … … 678 678 [equation dist_tutorial2] 679 679 680 And for the combined degrees of freedom we use the 680 And for the combined degrees of freedom we use the 681 681 [@http://en.wikipedia.org/wiki/WelchSatterthwaite_equation WelchSatterthwaite] 682 682 approximation: … … 692 692 an integer value: this may be necessary if you are relying on lookup tables, 693 693 but since our code fully supports noninteger degrees of freedom there is no 694 need to truncate in this case. Also note that when the degrees of freedom 694 need to truncate in this case. Also note that when the degrees of freedom 695 695 is small then the WelchSatterthwaite approximation may be a significant 696 696 source of error.] … … 754 754 Instead we calculate the difference between before and after measurements 755 755 in each patient, and calculate the mean and standard deviation of the differences. 756 To test whether a significant change has taken place, we can then test 757 the nullhypothesis that the true mean is zero using the same procedure 756 To test whether a significant change has taken place, we can then test 757 the nullhypothesis that the true mean is zero using the same procedure 758 758 we used in the single sample cases previously discussed. 759 759 760 760 That means we can: 761 761 762 * [link math_toolkit.dist.stat_tut.weg.st_eg.tut_mean_intervals Calculate confidence intervals of the mean]. 763 If the endpoints of the interval differ in sign then we are unable to reject 762 * [link math_toolkit.dist.stat_tut.weg.st_eg.tut_mean_intervals Calculate confidence intervals of the mean]. 763 If the endpoints of the interval differ in sign then we are unable to reject 764 764 the nullhypothesis that there is no change. 765 765 * [link math_toolkit.dist.stat_tut.weg.st_eg.tut_mean_test Test whether the true mean is zero]. If the … … 773 773 [endsect][/section:st_eg Student's t] 774 774 775 [/ 776 Copyright 2006 John Maddock and Paul A. Bristow.775 [/ 776 Copyright 2006, 2012 John Maddock and Paul A. Bristow. 777 777 Distributed under the Boost Software License, Version 1.0. 778 778 (See accompanying file LICENSE_1_0.txt or copy at
Note: See TracChangeset
for help on using the changeset viewer.