truncated_normal_prb.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824
  1. # include <cstdlib>
  2. # include <iostream>
  3. # include <iomanip>
  4. # include <cmath>
  5. # include <ctime>
  6. using namespace std;
  7. # include "truncated_normal.hpp"
  8. int main ( );
  9. void i4_uniform_ab_test ( );
  10. void normal_01_cdf_test ( );
  11. void normal_01_cdf_inv_test ( );
  12. void normal_01_mean_test ( );
  13. void normal_01_moment_test ( );
  14. void normal_01_pdf_test ( );
  15. void normal_01_sample_test ( );
  16. void normal_01_variance_test ( );
  17. void normal_ms_cdf_test ( );
  18. void normal_ms_cdf_inv_test ( );
  19. void normal_ms_mean_test ( );
  20. void normal_ms_moment_test ( );
  21. void normal_ms_moment_central_test ( );
  22. void normal_ms_pdf_test ( );
  23. void normal_ms_sample_test ( );
  24. void normal_ms_variance_test ( );
  25. void r8_choose_test ( );
  26. void r8_factorial2_test ( );
  27. void r8_mop_test ( );
  28. void r8_uniform_01_test ( );
  29. void r8poly_print_test ( );
  30. void r8poly_value_horner_test ( );
  31. void r8vec_linspace_new_test ( );
  32. void r8vec_print_test ( );
  33. void truncated_normal_a_cdf_test ( );
  34. void truncated_normal_a_cdf_inv_test ( );
  35. void truncated_normal_a_mean_test ( );
  36. void truncated_normal_a_moment_test ( );
  37. void truncated_normal_a_pdf_test ( );
  38. void truncated_normal_a_sample_test ( );
  39. void truncated_normal_a_variance_test ( );
  40. void truncated_normal_ab_cdf_test ( );
  41. void truncated_normal_ab_cdf_inv_test ( );
  42. void truncated_normal_ab_mean_test ( );
  43. void truncated_normal_ab_moment_test ( );
  44. void truncated_normal_ab_pdf_test ( );
  45. void truncated_normal_ab_sample_test ( );
  46. void truncated_normal_ab_variance_test ( );
  47. void truncated_normal_b_cdf_test ( );
  48. void truncated_normal_b_cdf_inv_test ( );
  49. void truncated_normal_b_mean_test ( );
  50. void truncated_normal_b_moment_test ( );
  51. void truncated_normal_b_pdf_test ( );
  52. void truncated_normal_b_sample_test ( );
  53. void truncated_normal_b_variance_test ( );
  54. //****************************************************************************80
  55. int main ( )
  56. //****************************************************************************80
  57. //
  58. // Purpose:
  59. //
  60. // MAIN is the main program for TRUNCATED_NORMAL_PRB.
  61. //
  62. // Discussion:
  63. //
  64. // TRUNCATED_NORMAL_PRB tests the TRUNCATED_NORMAL library.
  65. //
  66. // Licensing:
  67. //
  68. // This code is distributed under the GNU LGPL license.
  69. //
  70. // Modified:
  71. //
  72. // 07 March 2015
  73. //
  74. // Author:
  75. //
  76. // John Burkardt
  77. //
  78. {
  79. timestamp ( );
  80. cout << "\n";
  81. cout << "TRUNCATED_NORMAL_PRB\n";
  82. cout << " C++ version.\n";
  83. cout << " Test the TRUNCATED_NORMAL library.\n";
  84. //
  85. // Utilities.
  86. //
  87. i4_uniform_ab_test ( );
  88. r8_choose_test ( );
  89. r8_factorial2_test ( );
  90. r8_mop_test ( );
  91. r8_uniform_01_test ( );
  92. r8poly_print_test ( );
  93. r8poly_value_horner_test ( );
  94. r8vec_linspace_new_test ( );
  95. r8vec_print_test ( );
  96. //
  97. // Library.
  98. //
  99. normal_01_cdf_test ( );
  100. normal_01_cdf_inv_test ( );
  101. normal_01_mean_test ( );
  102. normal_01_moment_test ( );
  103. normal_01_pdf_test ( );
  104. normal_01_sample_test ( );
  105. normal_01_variance_test ( );
  106. normal_ms_cdf_test ( );
  107. normal_ms_cdf_inv_test ( );
  108. normal_ms_mean_test ( );
  109. normal_ms_moment_test ( );
  110. normal_ms_moment_central_test ( );
  111. normal_ms_pdf_test ( );
  112. normal_ms_sample_test ( );
  113. normal_ms_variance_test ( );
  114. truncated_normal_a_cdf_test ( );
  115. truncated_normal_a_cdf_inv_test ( );
  116. truncated_normal_a_mean_test ( );
  117. truncated_normal_a_moment_test ( );
  118. truncated_normal_a_pdf_test ( );
  119. truncated_normal_a_sample_test ( );
  120. truncated_normal_a_variance_test ( );
  121. truncated_normal_ab_cdf_test ( );
  122. truncated_normal_ab_cdf_inv_test ( );
  123. truncated_normal_ab_mean_test ( );
  124. truncated_normal_ab_moment_test ( );
  125. truncated_normal_ab_pdf_test ( );
  126. truncated_normal_ab_sample_test ( );
  127. truncated_normal_ab_variance_test ( );
  128. truncated_normal_b_cdf_test ( );
  129. truncated_normal_b_cdf_inv_test ( );
  130. truncated_normal_b_mean_test ( );
  131. truncated_normal_b_moment_test ( );
  132. truncated_normal_b_pdf_test ( );
  133. truncated_normal_b_sample_test ( );
  134. truncated_normal_b_variance_test ( );
  135. //
  136. // Terminate.
  137. //
  138. cout << "\n";
  139. cout << "TRUNCATED_NORMAL_PRB\n";
  140. cout << " Normal end of execution.\n";
  141. cout << "\n";
  142. timestamp ( );
  143. return 0;
  144. }
  145. //****************************************************************************80
  146. void i4_uniform_ab_test ( )
  147. //****************************************************************************80
  148. //
  149. // Purpose:
  150. //
  151. // I4_UNIFORM_AB_TEST tests I4_UNIFORM_AB.
  152. //
  153. // Licensing:
  154. //
  155. // This code is distributed under the GNU LGPL license.
  156. //
  157. // Modified:
  158. //
  159. // 27 October 2014
  160. //
  161. // Author:
  162. //
  163. // John Burkardt
  164. //
  165. {
  166. int a = -100;
  167. int b = 200;
  168. int i;
  169. int j;
  170. int seed = 123456789;
  171. cout << "\n";
  172. cout << "I4_UNIFORM_AB_TEST\n";
  173. cout << " I4_UNIFORM_AB computes pseudorandom values\n";
  174. cout << " in an interval [A,B].\n";
  175. cout << "\n";
  176. cout << " The lower endpoint A = " << a << "\n";
  177. cout << " The upper endpoint B = " << b << "\n";
  178. cout << " The initial seed is " << seed << "\n";
  179. cout << "\n";
  180. for ( i = 1; i <= 20; i++ )
  181. {
  182. j = i4_uniform_ab ( a, b, seed );
  183. cout << " " << setw(8) << i
  184. << " " << setw(8) << j << "\n";
  185. }
  186. return;
  187. }
  188. //****************************************************************************80
  189. void normal_01_cdf_test ( )
  190. //****************************************************************************80
  191. //
  192. // Purpose:
  193. //
  194. // NORMAL_01_CDF_TEST tests NORMAL_01_CDF.
  195. //
  196. // Licensing:
  197. //
  198. // This code is distributed under the GNU LGPL license.
  199. //
  200. // Modified:
  201. //
  202. // 27 February 2015
  203. //
  204. // Author:
  205. //
  206. // John Burkardt
  207. //
  208. {
  209. double cdf1;
  210. double cdf2;
  211. int n_data;
  212. double x;
  213. cout << "\n";
  214. cout << "NORMAL_01_CDF_TEST\n";
  215. cout << " NORMAL_01_CDF evaluates the Normal 01 CDF;\n";
  216. cout << "\n";
  217. cout << " X CDF CDF\n";
  218. cout << " (exact) (computed)\n";
  219. cout << "\n";
  220. n_data = 0;
  221. for ( ; ; )
  222. {
  223. normal_01_cdf_values ( n_data, x, cdf1 );
  224. if ( n_data == 0 )
  225. {
  226. break;
  227. }
  228. cdf2 = normal_01_cdf ( x );
  229. cout << " " << setw(14) << x
  230. << " " << setw(24) << cdf1
  231. << " " << setw(24) << cdf2 << "\n";
  232. }
  233. return;
  234. }
  235. //****************************************************************************80
  236. void normal_01_cdf_inv_test ( )
  237. //****************************************************************************80
  238. //
  239. // Purpose:
  240. //
  241. // NORMAL_01_CDF_INV_TEST tests NORMAL_01_CDF_INV.
  242. //
  243. // Licensing:
  244. //
  245. // This code is distributed under the GNU LGPL license.
  246. //
  247. // Modified:
  248. //
  249. // 28 February 2015
  250. //
  251. // Author:
  252. //
  253. // John Burkardt
  254. //
  255. {
  256. double cdf;
  257. int n_data;
  258. double x1;
  259. double x2;
  260. cout << "\n";
  261. cout << "NORMAL_01_CDF_INV_TEST\n";
  262. cout << " NORMAL_01_CDF_INV inverts the Normal 01 CDF;\n";
  263. cout << "\n";
  264. cout << " CDF X X\n";
  265. cout << " (exact) (computed)\n";
  266. cout << "\n";
  267. n_data = 0;
  268. for ( ; ; )
  269. {
  270. normal_01_cdf_values ( n_data, x1, cdf );
  271. if ( n_data == 0 )
  272. {
  273. break;
  274. }
  275. x2 = normal_01_cdf_inv ( cdf );
  276. cout << " " << setw(14) << cdf
  277. << " " << setw(24) << x1
  278. << " " << setw(24) << x2 << "\n";
  279. }
  280. return;
  281. }
  282. //****************************************************************************80
  283. void normal_01_mean_test ( )
  284. //****************************************************************************80
  285. //
  286. // Purpose:
  287. //
  288. // NORMAL_01_MEAN_TEST tests NORMAL_01_MEAN.
  289. //
  290. // Licensing:
  291. //
  292. // This code is distributed under the GNU LGPL license.
  293. //
  294. // Modified:
  295. //
  296. // 01 March 2015
  297. //
  298. // Author:
  299. //
  300. // John Burkardt
  301. //
  302. {
  303. int i;
  304. double mean;
  305. int sample_num;
  306. int seed = 123456789;
  307. double *x;
  308. double xmax;
  309. double xmin;
  310. cout << "\n";
  311. cout << "NORMAL_01_MEAN_TEST\n";
  312. cout << " NORMAL_01_MEAN computes the Normal 01 mean;\n";
  313. mean = normal_01_mean ( );
  314. cout << "\n";
  315. cout << " PDF mean = " << mean << "\n";
  316. sample_num = 1000;
  317. x = new double[sample_num];
  318. for ( i = 0; i < sample_num; i++ )
  319. {
  320. x[i] = normal_01_sample ( seed );
  321. }
  322. mean = r8vec_mean ( sample_num, x );
  323. xmax = r8vec_max ( sample_num, x );
  324. xmin = r8vec_min ( sample_num, x );
  325. cout << "\n";
  326. cout << " Sample size = " << sample_num << "\n";
  327. cout << " Sample mean = " << mean << "\n";
  328. cout << " Sample maximum = " << xmax << "\n";
  329. cout << " Sample minimum = " << xmin << "\n";
  330. delete [] x;
  331. return;
  332. }
  333. //****************************************************************************80
  334. void normal_01_moment_test ( )
  335. //****************************************************************************80
  336. //
  337. // Purpose:
  338. //
  339. // NORMAL_01_MOMENT_TEST tests NORMAL_01_MOMENT.
  340. //
  341. // Licensing:
  342. //
  343. // This code is distributed under the GNU LGPL license.
  344. //
  345. // Modified:
  346. //
  347. // 01 March 2015
  348. //
  349. // Author:
  350. //
  351. // John Burkardt
  352. //
  353. {
  354. double moment;
  355. int order;
  356. cout << "\n";
  357. cout << "NORMAL_01_MOMENT_TEST\n";
  358. cout << " NORMAL_01_MOMENT evaluates Normal 01 moments;\n";
  359. cout << "\n";
  360. cout << " Order Moment\n";
  361. cout << "\n";
  362. for ( order = 0; order <= 10; order++ )
  363. {
  364. moment = normal_01_moment ( order );
  365. cout << " " << setw(14) << order
  366. << " " << setw(14) << moment << "\n";
  367. }
  368. return;
  369. }
  370. //****************************************************************************80
  371. void normal_01_pdf_test ( )
  372. //****************************************************************************80
  373. //
  374. // Purpose:
  375. //
  376. // NORMAL_01_PDF_TEST tests NORMAL_01_PDF.
  377. //
  378. // Licensing:
  379. //
  380. // This code is distributed under the GNU LGPL license.
  381. //
  382. // Modified:
  383. //
  384. // 01 March 2015
  385. //
  386. // Author:
  387. //
  388. // John Burkardt
  389. //
  390. {
  391. int i;
  392. double pdf;
  393. double x;
  394. cout << "\n";
  395. cout << "NORMAL_01_PDF_TEST\n";
  396. cout << " NORMAL_01_PDF evaluates the Normal 01 PDF;\n";
  397. cout << "\n";
  398. cout << " X PDF\n";
  399. cout << "\n";
  400. for ( i = - 20; i <= 20; i++ )
  401. {
  402. x = ( double ) ( i ) / 10.0;
  403. pdf = normal_01_pdf ( x );
  404. cout << " " << setw(14) << x
  405. << " " << setw(14) << pdf << "\n";
  406. }
  407. return;
  408. }
  409. //****************************************************************************80
  410. void normal_01_sample_test ( )
  411. //****************************************************************************80
  412. //
  413. // Purpose:
  414. //
  415. // NORMAL_01_SAMPLE_TEST tests NORMAL_01_SAMPLE.
  416. //
  417. // Licensing:
  418. //
  419. // This code is distributed under the GNU LGPL license.
  420. //
  421. // Modified:
  422. //
  423. // 26 February 2015
  424. //
  425. // Author:
  426. //
  427. // John Burkardt
  428. //
  429. {
  430. int i;
  431. int seed;
  432. double x;
  433. cout << "\n";
  434. cout << "NORMAL_01_SAMPLE_TEST\n";
  435. cout << " NORMAL_01_SAMPLE returns samples from the normal\n";
  436. cout << " distribution with mean 0 and standard deviation 1.\n";
  437. cout << "\n";
  438. seed = 123456789;
  439. for ( i = 1; i <= 10; i++ )
  440. {
  441. x = normal_01_sample ( seed );
  442. cout << " " << setw(4) << i
  443. << " " << setw(14) << x << "\n";
  444. }
  445. return;
  446. }
  447. //****************************************************************************80
  448. void normal_01_variance_test ( )
  449. //****************************************************************************80
  450. //
  451. // Purpose:
  452. //
  453. // NORMAL_01_VARIANCE_TEST tests NORMAL_01_VARIANCE;
  454. //
  455. // Licensing:
  456. //
  457. // This code is distributed under the GNU LGPL license.
  458. //
  459. // Modified:
  460. //
  461. // 02 March 2015
  462. //
  463. // Author:
  464. //
  465. // John Burkardt
  466. //
  467. {
  468. int i;
  469. int sample_num;
  470. int seed = 123456789;
  471. double variance;
  472. double *x;
  473. cout << "\n";
  474. cout << "NORMAL_01_VARIANCE_TEST\n";
  475. cout << " NORMAL_01_VARIANCE computes the Normal 01 variance;\n";
  476. variance = normal_01_variance ( );
  477. cout << "\n";
  478. cout << " PDF variance = " << variance << "\n";
  479. sample_num = 1000;
  480. x = new double[sample_num];
  481. for ( i = 0; i < sample_num; i++ )
  482. {
  483. x[i] = normal_01_sample ( seed );
  484. }
  485. variance = r8vec_variance ( sample_num, x );
  486. cout << "\n";
  487. cout << " Sample size = " << sample_num << "\n";
  488. cout << " Sample variance = " << variance << "\n";
  489. delete [] x;
  490. return;
  491. }
  492. //****************************************************************************80
  493. void normal_ms_cdf_test ( )
  494. //****************************************************************************80
  495. //
  496. // Purpose:
  497. //
  498. // NORMAL_MS_CDF_TEST tests NORMAL_MS_CDF.
  499. //
  500. // Licensing:
  501. //
  502. // This code is distributed under the GNU LGPL license.
  503. //
  504. // Modified:
  505. //
  506. // 05 March 2015
  507. //
  508. // Author:
  509. //
  510. // John Burkardt
  511. //
  512. {
  513. double cdf;
  514. int i;
  515. double mu;
  516. double sigma;
  517. double x;
  518. cout << "\n";
  519. cout << "NORMAL_MS_CDF_TEST\n";
  520. cout << " NORMAL_MS_CDF evaluates the Normal MS CDF;\n";
  521. mu = 100.0;
  522. sigma = 15.0;
  523. cout << "\n";
  524. cout << " Parameter MU = " << mu << "\n";
  525. cout << " Parameteter SIGMA = " << sigma << "\n";
  526. cout << "\n";
  527. cout << " X CDF\n";
  528. cout << "\n";
  529. for ( i = - 20; i <= +20; i++ )
  530. {
  531. x = mu + sigma * ( double ) ( i ) / 10.0;
  532. cdf = normal_ms_cdf ( x, mu, sigma );
  533. cout << " " << setw(14) << x
  534. << " " << setw(24) << cdf << "\n";
  535. }
  536. return;
  537. }
  538. //****************************************************************************80
  539. void normal_ms_cdf_inv_test ( )
  540. //****************************************************************************80
  541. //
  542. // Purpose:
  543. //
  544. // NORMAL_MS_CDF_INV_TEST tests NORMAL_MS_CDF_INV.
  545. //
  546. // Licensing:
  547. //
  548. // This code is distributed under the GNU LGPL license.
  549. //
  550. // Modified:
  551. //
  552. // 04 March 2015
  553. //
  554. // Author:
  555. //
  556. // John Burkardt
  557. //
  558. {
  559. double cdf;
  560. int i;
  561. double mu;
  562. double sigma;
  563. double x;
  564. double x2;
  565. cout << "\n";
  566. cout << "NORMAL_MS_CDF_INV_TEST\n";
  567. cout << " NORMAL_MS_CDF_INV inverts the Normal MS CDF;\n";
  568. mu = 100.0;
  569. sigma = 15.0;
  570. cout << "\n";
  571. cout << " Parameter MU = " << mu << "\n";
  572. cout << " Parameteter SIGMA = " << sigma << "\n";
  573. cout << "\n";
  574. cout << " X CDF CDF_INV\n";
  575. cout << "\n";
  576. for ( i = - 20; i <= +20; i++ )
  577. {
  578. x = mu + sigma * ( double ) ( i ) / 10.0;
  579. cdf = normal_ms_cdf ( x, mu, sigma );
  580. x2 = normal_ms_cdf_inv ( cdf, mu, sigma );
  581. cout << " " << setw(14) << x
  582. << " " << setw(14) << cdf
  583. << " " << setw(14) << x2 << "\n";
  584. }
  585. return;
  586. }
  587. //****************************************************************************80
  588. void normal_ms_mean_test ( )
  589. //****************************************************************************80
  590. //
  591. // Purpose:
  592. //
  593. // NORMAL_MS_MEAN_TEST tests NORMAL_MS_MEAN.
  594. //
  595. // Licensing:
  596. //
  597. // This code is distributed under the GNU LGPL license.
  598. //
  599. // Modified:
  600. //
  601. // 04 March 2015
  602. //
  603. // Author:
  604. //
  605. // John Burkardt
  606. //
  607. {
  608. int i;
  609. double mean;
  610. double mu;
  611. int sample_num;
  612. int seed = 123456789;
  613. double sigma;
  614. double *x;
  615. double xmax;
  616. double xmin;
  617. cout << "\n";
  618. cout << "NORMAL_MS_MEAN_TEST\n";
  619. cout << " NORMAL_MS_MEAN computes the Normal MS mean.\n";
  620. mu = 100.0;
  621. sigma = 15.0;
  622. cout << "\n";
  623. cout << " Parameter MU = " << mu << "\n";
  624. cout << " Parameteter SIGMA = " << sigma << "\n";
  625. mean = normal_ms_mean ( mu, sigma );
  626. cout << "\n";
  627. cout << " PDF mean = " << mean << "\n";
  628. sample_num = 1000;
  629. x = new double[sample_num];
  630. for ( i = 0; i < sample_num; i++ )
  631. {
  632. x[i] = normal_ms_sample ( mu, sigma, seed );
  633. }
  634. mean = r8vec_mean ( sample_num, x );
  635. xmax = r8vec_max ( sample_num, x );
  636. xmin = r8vec_min ( sample_num, x );
  637. cout << "\n";
  638. cout << " Sample size = " << sample_num << "\n";
  639. cout << " Sample mean = " << mean << "\n";
  640. cout << " Sample maximum = " << xmax << "\n";
  641. cout << " Sample minimum = " << xmin << "\n";
  642. delete [] x;
  643. return;
  644. }
  645. //****************************************************************************80
  646. void normal_ms_moment_test ( )
  647. //****************************************************************************80
  648. //
  649. // Purpose:
  650. //
  651. // NORMAL_MOMENT_MS_TEST tests NORMAL_MS_MOMENT.
  652. //
  653. // Licensing:
  654. //
  655. // This code is distributed under the GNU LGPL license.
  656. //
  657. // Modified:
  658. //
  659. // 31 August 2013
  660. //
  661. // Author:
  662. //
  663. // John Burkardt
  664. //
  665. {
  666. double moment1;
  667. double moment2;
  668. double mu;
  669. double mu_test[4] = { 0.0, 2.0, 10.0, 0.0 };
  670. int order;
  671. double sigma;
  672. double sigma_test[4] = { 1.0, 1.0, 2.0, 2.0 };
  673. int test;
  674. int test_num = 4;
  675. cout << "\n";
  676. cout << "NORMAL_MOMENT_MS_TEST\n";
  677. cout << " NORMAL_MS_MOMENT evaluates the moments of the Normal MS distribution.\n";
  678. for ( test = 0; test < test_num; test++ )
  679. {
  680. mu = mu_test[test];
  681. sigma = sigma_test[test];
  682. cout << "\n";
  683. cout << " Mu = " << mu
  684. << " Sigma = " << sigma << "\n";
  685. cout << " Order Moment\n";
  686. cout << "\n";
  687. for ( order = 0; order <= 8; order++ )
  688. {
  689. moment1 = normal_ms_moment ( order, mu, sigma );
  690. moment2 = normal_ms_moment_values ( order, mu, sigma );
  691. cout << " " << setw(2) << order
  692. << " " << setw(14) << moment1
  693. << " " << setw(14) << moment2 << "\n";
  694. }
  695. }
  696. return;
  697. }
  698. //****************************************************************************80
  699. void normal_ms_moment_central_test ( )
  700. //****************************************************************************80
  701. //
  702. // Purpose:
  703. //
  704. // NORMAL_MS_MOMENT_CENTRAL_TEST tests NORMAL_MS_MOMENT_CENTRAL.
  705. //
  706. // Licensing:
  707. //
  708. // This code is distributed under the GNU LGPL license.
  709. //
  710. // Modified:
  711. //
  712. // 31 August 2013
  713. //
  714. // Author:
  715. //
  716. // John Burkardt
  717. //
  718. {
  719. double moment1;
  720. double moment2;
  721. double mu;
  722. double mu_test[4] = { 0.0, 2.0, 10.0, 0.0 };
  723. int order;
  724. double sigma;
  725. double sigma_test[4] = { 1.0, 1.0, 2.0, 2.0 };
  726. int test;
  727. int test_num = 4;
  728. cout << "\n";
  729. cout << "NORMAL_MS_MOMENT_CENTRAL_TEST\n";
  730. cout << " NORMAL_MS_MOMENT_CENTRAL evaluates the central moments of the\n";
  731. cout << " Normal MS distribution.\n";
  732. for ( test = 0; test < test_num; test++ )
  733. {
  734. mu = mu_test[test];
  735. sigma = sigma_test[test];
  736. cout << "\n";
  737. cout << " Mu = " << mu
  738. << " Sigma = " << sigma << "\n";
  739. cout << " Order Moment\n";
  740. cout << "\n";
  741. for ( order = 0; order <= 8; order++ )
  742. {
  743. moment1 = normal_ms_moment_central ( order, mu, sigma );
  744. moment2 = normal_ms_moment_central_values ( order, mu, sigma );
  745. cout << " " << setw(2) << order
  746. << " " << setw(14) << moment1
  747. << " " << setw(14) << moment2 << "\n";
  748. }
  749. }
  750. return;
  751. }
  752. //****************************************************************************80
  753. void normal_ms_pdf_test ( )
  754. //****************************************************************************80
  755. //
  756. // Purpose:
  757. //
  758. // NORMAL_MS_PDF_TEST tests NORMAL_MS_PDF.
  759. //
  760. // Licensing:
  761. //
  762. // This code is distributed under the GNU LGPL license.
  763. //
  764. // Modified:
  765. //
  766. // 04 March 2015
  767. //
  768. // Author:
  769. //
  770. // John Burkardt
  771. //
  772. {
  773. int i;
  774. double mu;
  775. double pdf;
  776. double sigma;
  777. double x;
  778. cout << "\n";
  779. cout << "NORMAL_MS_PDF_TEST\n";
  780. cout << " NORMAL_MS_PDF evaluates the Normal MS PDF;\n";
  781. mu = 100.0;
  782. sigma = 15.0;
  783. cout << "\n";
  784. cout << " Parameter MU = " << mu << "\n";
  785. cout << " Parameteter SIGMA = " << sigma << "\n";
  786. cout << "\n";
  787. cout << " X PDF\n";
  788. cout << "\n";
  789. for ( i = - 20; i <= +20; i++ )
  790. {
  791. x = mu + sigma * ( double ) ( i ) / 10.0;
  792. pdf = normal_ms_pdf ( mu, sigma, x );
  793. cout << " " << setw(14) << x
  794. << " " << setw(24) << pdf << "\n";
  795. }
  796. return;
  797. }
  798. //****************************************************************************80
  799. void normal_ms_sample_test ( )
  800. //****************************************************************************80
  801. //
  802. // Purpose:
  803. //
  804. // NORMAL_MS_SAMPLE_TEST tests NORMAL_MS_SAMPLE.
  805. //
  806. // Licensing:
  807. //
  808. // This code is distributed under the GNU LGPL license.
  809. //
  810. // Modified:
  811. //
  812. // 04 March 2015
  813. //
  814. // Author:
  815. //
  816. // John Burkardt
  817. //
  818. {
  819. int i;
  820. double mu;
  821. int seed;
  822. double sigma;
  823. double x;
  824. cout << "\n";
  825. cout << "NORMAL_MS_SAMPLE_TEST\n";
  826. cout << " NORMAL_MS_SAMPLE returns samples from the Normal MS PDF.\n";
  827. mu = 100.0;
  828. sigma = 15.0;
  829. cout << "\n";
  830. cout << " Parameter MU = " << mu << "\n";
  831. cout << " Parameteter SIGMA = " << sigma << "\n";
  832. cout << "\n";
  833. seed = 123456789;
  834. for ( i = 1; i <= 10; i++ )
  835. {
  836. x = normal_ms_sample ( mu, sigma, seed );
  837. cout << " " << setw(4) << i
  838. << " " << setw(14) << x << "\n";
  839. }
  840. return;
  841. }
  842. //****************************************************************************80
  843. void normal_ms_variance_test ( )
  844. //****************************************************************************80
  845. //
  846. // Purpose:
  847. //
  848. // NORMAL_MS_VARIANCE_TEST tests NORMAL_MS_VARIANCE;
  849. //
  850. // Licensing:
  851. //
  852. // This code is distributed under the GNU LGPL license.
  853. //
  854. // Modified:
  855. //
  856. // 04 March 2015
  857. //
  858. // Author:
  859. //
  860. // John Burkardt
  861. //
  862. {
  863. int i;
  864. double mu;
  865. int sample_num;
  866. int seed = 123456789;
  867. double sigma;
  868. double variance;
  869. double *x;
  870. cout << "\n";
  871. cout << "NORMAL_MS_VARIANCE_TEST\n";
  872. cout << " NORMAL_MS_VARIANCE computes the Normal MS variance;\n";
  873. mu = 100.0;
  874. sigma = 15.0;
  875. cout << "\n";
  876. cout << " Parameter MU = " << mu << "\n";
  877. cout << " Parameteter SIGMA = " << sigma << "\n";
  878. variance = normal_ms_variance ( mu, sigma );
  879. cout << "\n";
  880. cout << " PDF variance = " << variance << "\n";
  881. sample_num = 1000;
  882. x = new double[sample_num];
  883. for ( i = 0; i < sample_num; i++ )
  884. {
  885. x[i] = normal_ms_sample ( mu, sigma, seed );
  886. }
  887. variance = r8vec_variance ( sample_num, x );
  888. cout << "\n";
  889. cout << " Sample size = " << sample_num << "\n";
  890. cout << " Sample variance = " << variance << "\n";
  891. delete [] x;
  892. return;
  893. }
  894. //****************************************************************************80
  895. void r8_choose_test ( )
  896. //****************************************************************************80
  897. //
  898. // Purpose:
  899. //
  900. // R8_CHOOSE_TEST tests R8_CHOOSE.
  901. //
  902. // Licensing:
  903. //
  904. // This code is distributed under the GNU LGPL license.
  905. //
  906. // Modified:
  907. //
  908. // 26 July 2014
  909. //
  910. // Author:
  911. //
  912. // John Burkardt
  913. //
  914. {
  915. double cnk;
  916. int k;
  917. int n;
  918. cout << "\n";
  919. cout << "R8_CHOOSE_TEST\n";
  920. cout << " R8_CHOOSE evaluates C(N,K).\n";
  921. cout << "\n";
  922. cout << " N K CNK\n";
  923. for ( n = 0; n <= 5; n++ )
  924. {
  925. cout << "\n";
  926. for ( k = 0; k <= n; k++ )
  927. {
  928. cnk = r8_choose ( n, k );
  929. cout << setw(10) << n << " "
  930. << setw(8) << k << " "
  931. << setw(14) << cnk << "\n";
  932. }
  933. }
  934. return;
  935. }
  936. //****************************************************************************80
  937. void r8_factorial2_test ( )
  938. //****************************************************************************80
  939. //
  940. // Purpose:
  941. //
  942. // R8_FACTORIAL2_TEST tests R8_FACTORIAL2.
  943. //
  944. // Licensing:
  945. //
  946. // This code is distributed under the GNU LGPL license.
  947. //
  948. // Modified:
  949. //
  950. // 08 February 2015
  951. //
  952. // Author:
  953. //
  954. // John Burkardt
  955. //
  956. {
  957. double f1;
  958. double f2;
  959. int n;
  960. int n_data;
  961. streamsize ss;
  962. //
  963. // Save the current precision.
  964. //
  965. ss = cout.precision ( );
  966. cout << "\n";
  967. cout << "R8_FACTORIAL2_TEST\n";
  968. cout << " R8_FACTORIAL2 evaluates the double factorial function.\n";
  969. cout << "\n";
  970. cout << " N Exact Computed\n";
  971. cout << "\n";
  972. n_data = 0;
  973. for ( ; ; )
  974. {
  975. r8_factorial2_values ( n_data, n, f1 );
  976. if ( n_data == 0 )
  977. {
  978. break;
  979. }
  980. f2 = r8_factorial2 ( n );
  981. cout << " "
  982. << setw(4) << n << " "
  983. << setprecision(16) << setw(24) << f1 << " "
  984. << setprecision(16) << setw(24) << f2 << "\n";
  985. }
  986. //
  987. // Restore the default precision.
  988. //
  989. cout.precision ( ss );
  990. return;
  991. }
  992. //****************************************************************************80
  993. void r8_mop_test ( )
  994. //****************************************************************************80
  995. //
  996. // Purpose:
  997. //
  998. // R8_MOP_TEST tests R8_MOP.
  999. //
  1000. // Licensing:
  1001. //
  1002. // This code is distributed under the GNU LGPL license.
  1003. //
  1004. // Modified:
  1005. //
  1006. // 06 December 2014
  1007. //
  1008. // Author:
  1009. //
  1010. // John Burkardt
  1011. //
  1012. {
  1013. int i4;
  1014. int i4_max;
  1015. int i4_min;
  1016. double r8;
  1017. int seed = 123456789;
  1018. int test;
  1019. cout << "\n";
  1020. cout << "R8_MOP_TEST\n";
  1021. cout << " R8_MOP evaluates (-1.0)^I4 as an R8.\n";
  1022. cout << "\n";
  1023. cout << " I4 R8_MOP(I4)\n";
  1024. cout << "\n";
  1025. i4_min = -100;
  1026. i4_max = +100;
  1027. for ( test = 1; test <= 10; test++ )
  1028. {
  1029. i4 = i4_uniform_ab ( i4_min, i4_max, seed );
  1030. r8 = r8_mop ( i4 );
  1031. cout << " "
  1032. << setw(4) << i4 << " "
  1033. << setw(4) <<r8 << "\n";
  1034. }
  1035. return;
  1036. }
  1037. //****************************************************************************80
  1038. void r8_uniform_01_test ( )
  1039. //****************************************************************************80
  1040. //
  1041. // Purpose:
  1042. //
  1043. // R8_UNIFORM_01_TEST tests R8_UNIFORM_01.
  1044. //
  1045. // Licensing:
  1046. //
  1047. // This code is distributed under the GNU LGPL license.
  1048. //
  1049. // Modified:
  1050. //
  1051. // 12 September 2005
  1052. //
  1053. // Author:
  1054. //
  1055. // John Burkardt
  1056. //
  1057. {
  1058. # define N 1000
  1059. int i;
  1060. double max;
  1061. double mean;
  1062. double min;
  1063. int n;
  1064. int seed = 123456789;
  1065. double x[N];
  1066. double variance;
  1067. cout << "\n";
  1068. cout << "R8_UNIFORM_01_TEST\n";
  1069. cout << " R8_UNIFORM_01 samples a uniform random distribution in [0,1].\n";
  1070. cout << " distributed random numbers.\n";
  1071. cout << " Using initial random number seed = " << seed << "\n";
  1072. for ( i = 0; i < N; i++ )
  1073. {
  1074. x[i] = r8_uniform_01 ( seed );
  1075. }
  1076. cout << "\n";
  1077. cout << " First few values:\n";
  1078. cout << "\n";
  1079. for ( i = 0; i < 10; i++ )
  1080. {
  1081. cout << " " << setw(6) << i
  1082. << " " << setw(14) << x[i] << "\n";
  1083. }
  1084. min = r8vec_min ( N, x );
  1085. max = r8vec_max ( N, x );
  1086. mean = r8vec_mean ( N, x );
  1087. variance = r8vec_variance ( N, x );
  1088. cout << "\n";
  1089. cout << " Number of samples was " << N << "\n";
  1090. cout << " Minimum value was " << min << "\n";
  1091. cout << " Maximum value was " << max << "\n";
  1092. cout << " Average value was " << mean << "\n";
  1093. cout << " Variance was " << variance << "\n";
  1094. return;
  1095. # undef N
  1096. }
  1097. //****************************************************************************80
  1098. void r8poly_print_test ( )
  1099. //****************************************************************************80
  1100. //
  1101. // Purpose:
  1102. //
  1103. // R8POLY_PRINT_TEST tests R8POLY_PRINT.
  1104. //
  1105. // Licensing:
  1106. //
  1107. // This code is distributed under the GNU LGPL license.
  1108. //
  1109. // Modified:
  1110. //
  1111. // 03 January 2015
  1112. //
  1113. // Author:
  1114. //
  1115. // John Burkardt
  1116. //
  1117. {
  1118. double c[6] = { 2.0, -3.4, 56.0, 0.0, 0.78, 9.0 };
  1119. int m = 5;
  1120. cout << "\n";
  1121. cout << "R8POLY_PRINT_TEST\n";
  1122. cout << " R8POLY_PRINT prints an R8POLY.\n";
  1123. r8poly_print ( m, c, " The R8POLY:" );
  1124. return;
  1125. }
  1126. //****************************************************************************80
  1127. void r8poly_value_horner_test ( )
  1128. //****************************************************************************80
  1129. //
  1130. // Purpose:
  1131. //
  1132. // R8POLY_VALUE_HORNER_TEST tests R8POLY_VALUE_HORNER.
  1133. //
  1134. // Licensing:
  1135. //
  1136. // This code is distributed under the GNU LGPL license.
  1137. //
  1138. // Modified:
  1139. //
  1140. // 02 January 2015
  1141. //
  1142. // Author:
  1143. //
  1144. // John Burkardt
  1145. //
  1146. {
  1147. double c[5] = { 24.0, -50.0, +35.0, -10.0, 1.0 };
  1148. int i;
  1149. int m = 4;
  1150. int n = 16;
  1151. double p;
  1152. double *x;
  1153. double x_hi;
  1154. double x_lo;
  1155. cout << "\n";
  1156. cout << "R8POLY_VALUE_HORNER_TEST\n";
  1157. cout << " R8POLY_VALUE_HORNER evaluates a polynomial at\n";
  1158. cout << " one point, using Horner's method.\n";
  1159. r8poly_print ( m, c, " The polynomial coefficients:" );
  1160. x_lo = 0.0;
  1161. x_hi = 5.0;
  1162. x = r8vec_linspace_new ( n, x_lo, x_hi );
  1163. cout << "\n";
  1164. cout << " I X P(X)\n";
  1165. cout << "\n";
  1166. for ( i = 0; i < n; i++ )
  1167. {
  1168. p = r8poly_value_horner ( m, c, x[i] );
  1169. cout << " " << setw(2) << i
  1170. << " " << setw(8) << x[i]
  1171. << " " << setw(14) << p << "\n";
  1172. }
  1173. delete [] x;
  1174. return;
  1175. }
  1176. //****************************************************************************80
  1177. void r8vec_linspace_new_test ( )
  1178. //****************************************************************************80
  1179. //
  1180. // Purpose:
  1181. //
  1182. // R8VEC_LINSPACE_NEW_TEST tests R8VEC_LINSPACE_NEW.
  1183. //
  1184. // Licensing:
  1185. //
  1186. // This code is distributed under the GNU LGPL license.
  1187. //
  1188. // Modified:
  1189. //
  1190. // 03 June 2012
  1191. //
  1192. // Author:
  1193. //
  1194. // John Burkardt
  1195. //
  1196. {
  1197. double a;
  1198. double b;
  1199. int n = 5;
  1200. double *x;
  1201. cout << "\n";
  1202. cout << "R8VEC_LINSPACE_NEW_TEST\n";
  1203. cout << " For a R8VEC:\n";
  1204. cout << " R8VEC_LINSPACE_NEW: evenly spaced points between A and B;\n";
  1205. a = 10.0;
  1206. b = 20.0;
  1207. x = r8vec_linspace_new ( n, a, b );
  1208. r8vec_print ( n, x, " r8vec_linspace ( 5, 10, 20 )" );
  1209. delete [] x;
  1210. return;
  1211. }
  1212. //****************************************************************************80
  1213. void r8vec_print_test ( )
  1214. //****************************************************************************80
  1215. //
  1216. // Purpose:
  1217. //
  1218. // R8VEC_PRINT_TEST tests R8VEC_PRINT.
  1219. //
  1220. // Licensing:
  1221. //
  1222. // This code is distributed under the GNU LGPL license.
  1223. //
  1224. // Modified:
  1225. //
  1226. // 31 August 2014
  1227. //
  1228. // Author:
  1229. //
  1230. // John Burkardt
  1231. //
  1232. {
  1233. double a[4] = { 123.456, 0.000005, -1.0E+06, 3.14159265 };
  1234. int n = 4;
  1235. cout << "\n";
  1236. cout << "TEST1335\n";
  1237. cout << " R8VEC_PRINT prints an R8VEC.\n";
  1238. r8vec_print ( n, a, " The R8VEC:" );
  1239. return;
  1240. }
  1241. //****************************************************************************80
  1242. void truncated_normal_a_cdf_test ( )
  1243. //****************************************************************************80
  1244. //
  1245. // Purpose:
  1246. //
  1247. // TRUNCATED_NORMAL_A_CDF_TEST tests TRUNCATED_NORMAL_A_CDF.
  1248. //
  1249. // Licensing:
  1250. //
  1251. // This code is distributed under the GNU LGPL license.
  1252. //
  1253. // Modified:
  1254. //
  1255. // 14 September 2013
  1256. //
  1257. // Author:
  1258. //
  1259. // John Burkardt
  1260. //
  1261. {
  1262. double a;
  1263. double cdf1;
  1264. double cdf2;
  1265. double mu;
  1266. int n_data;
  1267. double sigma;
  1268. double x;
  1269. cout << "\n";
  1270. cout << "TRUNCATED_NORMAL_A_CDF_TEST:\n";
  1271. cout << " TRUNCATED_NORMAL_A_CDF evaluates\n";
  1272. cout << " the lower Truncated Normal Cumulative Density Function.\n";
  1273. cout << "\n";
  1274. cout << " MU S A X CDF1 CDF2\n";
  1275. cout << "\n";
  1276. n_data = 0;
  1277. for ( ; ; )
  1278. {
  1279. truncated_normal_a_cdf_values ( n_data, mu, sigma, a, x, cdf1 );
  1280. if ( n_data == 0 )
  1281. {
  1282. break;
  1283. }
  1284. cdf2 = truncated_normal_a_cdf ( x, mu, sigma, a );
  1285. cout << " " << setw(8) << mu
  1286. << " " << setw(8) << sigma
  1287. << " " << setw(8) << a
  1288. << " " << setw(8) << x
  1289. << " " << setprecision(16) << setw(24) << cdf1
  1290. << " " << setprecision(16) << setw(24) << cdf2 << "\n";
  1291. }
  1292. return;
  1293. }
  1294. //****************************************************************************80
  1295. void truncated_normal_a_cdf_inv_test ( )
  1296. //****************************************************************************80
  1297. //
  1298. // Purpose:
  1299. //
  1300. // TRUNCATED_NORMAL_A_CDF_INV_TEST tests TRUNCATED_NORMAL_A_CDF_INV.
  1301. //
  1302. // Licensing:
  1303. //
  1304. // This code is distributed under the GNU LGPL license.
  1305. //
  1306. // Modified:
  1307. //
  1308. // 07 March 2015
  1309. //
  1310. // Author:
  1311. //
  1312. // John Burkardt
  1313. //
  1314. {
  1315. double a;
  1316. double cdf;
  1317. int i;
  1318. double mu;
  1319. int sample_num = 10;
  1320. int seed;
  1321. double sigma;
  1322. double x;
  1323. double x2;
  1324. a = 50.0;
  1325. mu = 100.0;
  1326. sigma = 25.0;
  1327. seed = 123456789;
  1328. cout << "\n";
  1329. cout << "TRUNCATED_NORMAL_A_CDF_INV_TEST:\n";
  1330. cout << " TRUNCATED_NORMAL_A_CDF_INV inverts the CDF of\n";
  1331. cout << " the lower Truncated Normal Distribution.\n";
  1332. cout << "\n";
  1333. cout << " The parent normal distribution has\n";
  1334. cout << " mean = " << mu << "\n";
  1335. cout << " standard deviation = " << sigma << "\n";
  1336. cout << " The parent distribution is truncated to\n";
  1337. cout << " the interval [" << a << ",+oo)\n";
  1338. cout << "\n";
  1339. cout << " X CDF CDF_INV\n";
  1340. cout << "\n";
  1341. for ( i = 0; i < sample_num; i++ )
  1342. {
  1343. x = truncated_normal_a_sample ( mu, sigma, a, seed );
  1344. cdf = truncated_normal_a_cdf ( x, mu, sigma, a );
  1345. x2 = truncated_normal_a_cdf_inv ( cdf, mu, sigma, a );
  1346. cout << " " << setw(2) << i
  1347. << " " << setw(14) << x
  1348. << " " << setw(14) << cdf
  1349. << " " << setw(14) << x2 << "\n";
  1350. }
  1351. return;
  1352. }
  1353. //****************************************************************************80
  1354. void truncated_normal_a_mean_test ( )
  1355. //****************************************************************************80
  1356. //
  1357. // Purpose:
  1358. //
  1359. // TRUNCATED_NORMAL_A_MEAN_TEST tests TRUNCATED_NORMAL_A_MEAN.
  1360. //
  1361. // Licensing:
  1362. //
  1363. // This code is distributed under the GNU LGPL license.
  1364. //
  1365. // Modified:
  1366. //
  1367. // 07 March 2015
  1368. //
  1369. // Author:
  1370. //
  1371. // John Burkardt
  1372. //
  1373. {
  1374. double a;
  1375. int i;
  1376. double mean;
  1377. double mu;
  1378. int sample_num = 1000;
  1379. int seed;
  1380. double sigma;
  1381. double *x;
  1382. double xmax;
  1383. double xmin;
  1384. a = 50.0;
  1385. mu = 100.0;
  1386. sigma = 25.0;
  1387. seed = 123456789;
  1388. cout << "\n";
  1389. cout << "TRUNCATED_NORMAL_A_MEAN_TEST\n";
  1390. cout << " TRUNCATED_NORMAL_A_MEAN computes the mean\n";
  1391. cout << " of the lower Truncated Normal Distribution.\n";
  1392. cout << "\n";
  1393. cout << " The parent normal distribution has\n";
  1394. cout << " mean = " << mu << "\n";
  1395. cout << " standard deviation = " << sigma << "\n";
  1396. cout << " The parent distribution is truncated to\n";
  1397. cout << " the interval [" << a << ",+oo)\n";
  1398. mean = truncated_normal_a_mean ( mu, sigma, a );
  1399. cout << "\n";
  1400. cout << " PDF mean = " << mean << "\n";
  1401. x = new double[sample_num];
  1402. for ( i = 0; i < sample_num; i++ )
  1403. {
  1404. x[i] = truncated_normal_a_sample ( mu, sigma, a, seed );
  1405. }
  1406. mean = r8vec_mean ( sample_num, x );
  1407. xmax = r8vec_max ( sample_num, x );
  1408. xmin = r8vec_min ( sample_num, x );
  1409. cout << "\n";
  1410. cout << " Sample size = " << sample_num << "\n";
  1411. cout << " Sample mean = " << mean << "\n";
  1412. cout << " Sample maximum = " << xmax << "\n";
  1413. cout << " Sample minimum = " << xmin << "\n";
  1414. delete [] x;
  1415. return;
  1416. }
  1417. //****************************************************************************80
  1418. void truncated_normal_a_moment_test ( )
  1419. //****************************************************************************80
  1420. //
  1421. // Purpose:
  1422. //
  1423. // TRUNCATED_NORMAL_A_MOMENT_TEST tests TRUNCATED_NORMAL_A_MOMENT.
  1424. //
  1425. // Licensing:
  1426. //
  1427. // This code is distributed under the GNU LGPL license.
  1428. //
  1429. // Modified:
  1430. //
  1431. // 11 September 2013
  1432. //
  1433. // Author:
  1434. //
  1435. // John Burkardt
  1436. //
  1437. {
  1438. double a;
  1439. double a_test[6] = {
  1440. 0.0, -10.0, 10.0, -10.0, 10.0, -10.0 };
  1441. double moment;
  1442. double mu;
  1443. double mu_test[6] = {
  1444. 0.0, 0.0, 0.0, 0.0, 0.0, -5.0 };
  1445. int order;
  1446. double sigma;
  1447. double sigma_test[6] = {
  1448. 1.0, 1.0, 1.0, 2.0, 2.0, 1.0 };
  1449. int test;
  1450. int test_num;
  1451. test_num = 6;
  1452. cout << "\n";
  1453. cout << "TRUNCATED_NORMAL_A_MOMENT_TEST\n";
  1454. cout << " TRUNCATED_NORMAL_A_MOMENT evaluates the moments\n";
  1455. cout << " of the Lower Truncated Normal Distribution.\n";
  1456. for ( test = 0; test < test_num; test++ )
  1457. {
  1458. mu = mu_test[test];
  1459. sigma = sigma_test[test];
  1460. a = a_test[test];
  1461. cout << "\n";
  1462. cout << " Test = " << test
  1463. << ", Mu = " << mu
  1464. << ", Sigma = " << sigma
  1465. << ", A = " << a << "\n";
  1466. cout << " Order Moment\n";
  1467. cout << "\n";
  1468. for ( order = 0; order <= 8; order++ )
  1469. {
  1470. moment = truncated_normal_a_moment ( order, mu, sigma, a );
  1471. cout << " " << setw(2) << order
  1472. << " " << setw(14) << moment << "\n";
  1473. }
  1474. }
  1475. return;
  1476. }
  1477. //****************************************************************************80
  1478. void truncated_normal_a_pdf_test ( )
  1479. //****************************************************************************80
  1480. //
  1481. // Purpose:
  1482. //
  1483. // TRUNCATED_NORMAL_A_PDF_TEST tests TRUNCATED_NORMAL_A_PDF.
  1484. //
  1485. // Licensing:
  1486. //
  1487. // This code is distributed under the GNU LGPL license.
  1488. //
  1489. // Modified:
  1490. //
  1491. // 14 September 2013
  1492. //
  1493. // Author:
  1494. //
  1495. // John Burkardt
  1496. //
  1497. {
  1498. double a;
  1499. double mu;
  1500. int n_data;
  1501. double pdf1;
  1502. double pdf2;
  1503. double sigma;
  1504. double x;
  1505. cout << "\n";
  1506. cout << "TRUNCATED_NORMAL_A_PDF_TEST:\n";
  1507. cout << " TRUNCATED_NORMAL_A_PDF evaluates the PDF of\n";
  1508. cout << " the lower Truncated Normal Distribution.\n";
  1509. cout << "\n";
  1510. cout << " MU S A X PDF1 PDF2\n";
  1511. cout << "\n";
  1512. n_data = 0;
  1513. for ( ; ; )
  1514. {
  1515. truncated_normal_a_pdf_values ( n_data, mu, sigma, a, x, pdf1 );
  1516. if ( n_data == 0 )
  1517. {
  1518. break;
  1519. }
  1520. pdf2 = truncated_normal_a_pdf ( x, mu, sigma, a );
  1521. cout << " " << setw(8) << mu
  1522. << " " << setw(8) << sigma
  1523. << " " << setw(8) << a
  1524. << " " << setw(8) << x
  1525. << " " << setprecision(16) << setw(24) << pdf1
  1526. << " " << setprecision(16)<< setw(24) << pdf2 << "\n";
  1527. }
  1528. return;
  1529. }
  1530. //****************************************************************************80
  1531. void truncated_normal_a_sample_test ( )
  1532. //****************************************************************************80
  1533. //
  1534. // Purpose:
  1535. //
  1536. // TRUNCATED_NORMAL_A_SAMPLE_TEST tests TRUNCATED_NORMAL_A_SAMPLE.
  1537. //
  1538. // Licensing:
  1539. //
  1540. // This code is distributed under the GNU LGPL license.
  1541. //
  1542. // Modified:
  1543. //
  1544. // 07 March 2015
  1545. //
  1546. // Author:
  1547. //
  1548. // John Burkardt
  1549. //
  1550. {
  1551. double a;
  1552. int i;
  1553. double mu;
  1554. int sample_num = 10;
  1555. int seed;
  1556. double sigma;
  1557. double x;
  1558. a = 50.0;
  1559. mu = 100.0;
  1560. sigma = 25.0;
  1561. seed = 123456789;
  1562. cout << "\n";
  1563. cout << "TRUNCATED_NORMAL_A_SAMPLE_TEST:\n";
  1564. cout << " TRUNCATED_NORMAL_A_SAMPLE samples\n";
  1565. cout << " the lower Truncated Normal Distribution.\n";
  1566. cout << "\n";
  1567. cout << " The parent normal distribution has\n";
  1568. cout << " mean = " << mu << "\n";
  1569. cout << " standard deviation = " << sigma << "\n";
  1570. cout << " The parent distribution is truncated to\n";
  1571. cout << " the interval [" << a << ",+oo)\n";
  1572. cout << "\n";
  1573. for ( i = 0; i < sample_num; i++ )
  1574. {
  1575. x = truncated_normal_a_sample ( mu, sigma, a, seed );
  1576. cout << " " << setw(2) << i
  1577. << " " << setw(14) << x << "\n";
  1578. }
  1579. return;
  1580. }
  1581. //****************************************************************************80
  1582. void truncated_normal_a_variance_test ( )
  1583. //****************************************************************************80
  1584. //
  1585. // Purpose:
  1586. //
  1587. // TRUNCATED_NORMAL_A_VARIANCE_TEST tests TRUNCATED_NORMAL_A_VARIANCE.
  1588. //
  1589. // Licensing:
  1590. //
  1591. // This code is distributed under the GNU LGPL license.
  1592. //
  1593. // Modified:
  1594. //
  1595. // 07 March 2015
  1596. //
  1597. // Author:
  1598. //
  1599. // John Burkardt
  1600. //
  1601. {
  1602. double a;
  1603. int i;
  1604. double mu;
  1605. int sample_num = 1000;
  1606. int seed;
  1607. double sigma;
  1608. double variance;
  1609. double *x;
  1610. a = 50.0;
  1611. mu = 100.0;
  1612. sigma = 25.0;
  1613. seed = 123456789;
  1614. cout << "\n";
  1615. cout << "TRUNCATED_NORMAL_A_VARIANCE_TEST\n";
  1616. cout << " TRUNCATED_NORMAL_A_VARIANCE computes the variance\n";
  1617. cout << " of the lower Truncated Normal Distribution.\n";
  1618. cout << "\n";
  1619. cout << " The parent normal distribution has\n";
  1620. cout << " mean = " << mu << "\n";
  1621. cout << " standard deviation = " << sigma << "\n";
  1622. cout << " The parent distribution is truncated to\n";
  1623. cout << " the interval [" << a << ",+oo)\n";
  1624. variance = truncated_normal_a_variance ( mu, sigma, a );
  1625. cout << "\n";
  1626. cout << " PDF variance = " << variance << "\n";
  1627. x = new double[sample_num];
  1628. for ( i = 0; i < sample_num; i++ )
  1629. {
  1630. x[i] = truncated_normal_a_sample ( mu, sigma, a, seed );
  1631. }
  1632. variance = r8vec_variance ( sample_num, x );
  1633. cout << "\n";
  1634. cout << " Sample size = " << sample_num << "\n";
  1635. cout << " Sample variance = " << variance << "\n";
  1636. delete [] x;
  1637. return;
  1638. }
  1639. //****************************************************************************80
  1640. void truncated_normal_ab_cdf_test ( )
  1641. //****************************************************************************80
  1642. //
  1643. // Purpose:
  1644. //
  1645. // TRUNCATED_NORMAL_AB_CDF_TEST tests TRUNCATED_NORMAL_AB_CDF.
  1646. //
  1647. // Licensing:
  1648. //
  1649. // This code is distributed under the GNU LGPL license.
  1650. //
  1651. // Modified:
  1652. //
  1653. // 13 September 2013
  1654. //
  1655. // Author:
  1656. //
  1657. // John Burkardt
  1658. //
  1659. {
  1660. double a;
  1661. double b;
  1662. double cdf1;
  1663. double cdf2;
  1664. double mu;
  1665. int n_data;
  1666. double sigma;
  1667. double x;
  1668. cout << "\n";
  1669. cout << "TRUNCATED_NORMAL_AB_CDF_TEST:\n";
  1670. cout << " TRUNCATED_NORMAL_AB_CDF evaluates\n";
  1671. cout << " the Truncated Normal Cumulative Density Function.\n";
  1672. cout << "\n";
  1673. cout << " MU S A B X CDF1 CDF2\n";
  1674. cout << "\n";
  1675. n_data = 0;
  1676. for ( ; ; )
  1677. {
  1678. truncated_normal_ab_cdf_values ( n_data, mu, sigma, a, b, x, cdf1 );
  1679. if ( n_data == 0 )
  1680. {
  1681. break;
  1682. }
  1683. cdf2 = truncated_normal_ab_cdf ( x, mu, sigma, a, b );
  1684. cout << " " << setw(8) << mu
  1685. << " " << setw(8) << sigma
  1686. << " " << setw(8) << a
  1687. << " " << setw(8) << b
  1688. << " " << setw(8) << x
  1689. << " " << setprecision(16) << setw(24) << cdf1
  1690. << " " << setprecision(16) << setw(24) << cdf2 << "\n";
  1691. }
  1692. return;
  1693. }
  1694. //****************************************************************************80
  1695. void truncated_normal_ab_cdf_inv_test ( )
  1696. //****************************************************************************80
  1697. //
  1698. // Purpose:
  1699. //
  1700. // TRUNCATED_NORMAL_AB_CDF_INV_TEST tests TRUNCATED_NORMAL_AB_CDF_INV.
  1701. //
  1702. // Licensing:
  1703. //
  1704. // This code is distributed under the GNU LGPL license.
  1705. //
  1706. // Modified:
  1707. //
  1708. // 07 March 2015
  1709. //
  1710. // Author:
  1711. //
  1712. // John Burkardt
  1713. //
  1714. {
  1715. double a;
  1716. double b;
  1717. double cdf;
  1718. int i;
  1719. double mu;
  1720. int sample_num = 10;
  1721. int seed;
  1722. double sigma;
  1723. double x;
  1724. double x2;
  1725. a = 50.0;
  1726. b = 150.0;
  1727. mu = 100.0;
  1728. sigma = 25.0;
  1729. seed = 123456789;
  1730. cout << "\n";
  1731. cout << "TRUNCATED_NORMAL_AB_CDF_INV_TEST:\n";
  1732. cout << " TRUNCATED_NORMAL_AB_CDF_INV inverts the CDF of\n";
  1733. cout << " the Truncated Normal Distribution.\n";
  1734. cout << "\n";
  1735. cout << " The parent normal distribution has\n";
  1736. cout << " mean = " << mu << "\n";
  1737. cout << " standard deviation = " << sigma << "\n";
  1738. cout << " The parent distribution is truncated to\n";
  1739. cout << " the interval [" << a << "," << b << "]\n";
  1740. cout << "\n";
  1741. cout << " X CDF CDF_INV\n";
  1742. cout << "\n";
  1743. for ( i = 0; i < sample_num; i++ )
  1744. {
  1745. x = truncated_normal_ab_sample ( mu, sigma, a, b, seed );
  1746. cdf = truncated_normal_ab_cdf ( x, mu, sigma, a, b );
  1747. x2 = truncated_normal_ab_cdf_inv ( cdf, mu, sigma, a, b );
  1748. cout << " " << setw(2) << i
  1749. << " " << setw(14) << x
  1750. << " " << setw(14) << cdf
  1751. << " " << setw(14) << x2 << "\n";
  1752. }
  1753. return;
  1754. }
  1755. //****************************************************************************80
  1756. void truncated_normal_ab_mean_test ( )
  1757. //****************************************************************************80
  1758. //
  1759. // Purpose:
  1760. //
  1761. // TRUNCATED_NORMAL_AB_MEAN_TEST tests TRUNCATED_NORMAL_AB_MEAN.
  1762. //
  1763. // Licensing:
  1764. //
  1765. // This code is distributed under the GNU LGPL license.
  1766. //
  1767. // Modified:
  1768. //
  1769. // 07 March 2015
  1770. //
  1771. // Author:
  1772. //
  1773. // John Burkardt
  1774. //
  1775. {
  1776. double a;
  1777. double b;
  1778. int i;
  1779. double mean;
  1780. double mu;
  1781. int sample_num = 1000;
  1782. int seed;
  1783. double sigma;
  1784. double *x;
  1785. double xmax;
  1786. double xmin;
  1787. a = 50.0;
  1788. b = 150.0;
  1789. mu = 100.0;
  1790. sigma = 25.0;
  1791. seed = 123456789;
  1792. cout << "\n";
  1793. cout << "TRUNCATED_NORMAL_AB_MEAN_TEST\n";
  1794. cout << " TRUNCATED_NORMAL_AB_MEAN computes the mean\n";
  1795. cout << " of the Truncated Normal Distribution.\n";
  1796. cout << "\n";
  1797. cout << " The parent normal distribution has\n";
  1798. cout << " mean = " << mu << "\n";
  1799. cout << " standard deviation = " << sigma << "\n";
  1800. cout << " The parent distribution is truncated to\n";
  1801. cout << " the interval [" << a << "," << b << "]\n";
  1802. mean = truncated_normal_ab_mean ( mu, sigma, a, b );
  1803. cout << "\n";
  1804. cout << " PDF mean = " << mean << "\n";
  1805. x = new double[sample_num];
  1806. for ( i = 0; i < sample_num; i++ )
  1807. {
  1808. x[i] = truncated_normal_ab_sample ( mu, sigma, a, b, seed );
  1809. }
  1810. mean = r8vec_mean ( sample_num, x );
  1811. xmax = r8vec_max ( sample_num, x );
  1812. xmin = r8vec_min ( sample_num, x );
  1813. cout << "\n";
  1814. cout << " Sample size = " << sample_num << "\n";
  1815. cout << " Sample mean = " << mean << "\n";
  1816. cout << " Sample maximum = " << xmax << "\n";
  1817. cout << " Sample minimum = " << xmin << "\n";
  1818. delete [] x;
  1819. return;
  1820. }
  1821. //****************************************************************************80
  1822. void truncated_normal_ab_moment_test ( )
  1823. //****************************************************************************80
  1824. //
  1825. // Purpose:
  1826. //
  1827. // TRUNCATED_NORMAL_AB_MOMENT_TEST tests TRUNCATED_NORMAL_AB_MOMENT.
  1828. //
  1829. // Licensing:
  1830. //
  1831. // This code is distributed under the GNU LGPL license.
  1832. //
  1833. // Modified:
  1834. //
  1835. // 11 September 2013
  1836. //
  1837. // Author:
  1838. //
  1839. // John Burkardt
  1840. //
  1841. {
  1842. double a;
  1843. double a_test[9] = {
  1844. -1.0, 0.0, -1.0, -1.0, 0.0, 0.5, -2.0, -4.0, 4.0 };
  1845. double b;
  1846. double b_test[9] = {
  1847. 1.0, 1.0, 0.0, 1.0, 2.0, 2.0, 2.0, 4.0, 7.0 };
  1848. double moment;
  1849. double mu;
  1850. double mu_test[9] = {
  1851. 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 5.0 };
  1852. int order;
  1853. double sigma;
  1854. double sigma_test[9] = {
  1855. 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 0.5 };
  1856. int test;
  1857. int test_num;
  1858. test_num = 9;
  1859. cout << "\n";
  1860. cout << "TRUNCATED_NORMAL_AB_MOMENT_TEST\n";
  1861. cout << " TRUNCATED_NORMAL_AB_MOMENT evaluates the moments\n";
  1862. cout << " of the Truncated Normal PDF:\n";
  1863. for ( test = 0; test < test_num; test++ )
  1864. {
  1865. mu = mu_test[test];
  1866. sigma = sigma_test[test];
  1867. a = a_test[test];
  1868. b = b_test[test];
  1869. cout << "\n";
  1870. cout << " Test = " << test
  1871. << ", Mu = " << mu
  1872. << ", Sigma = " << sigma
  1873. << ", A = " << a
  1874. << ", B = " << b << "\n";
  1875. cout << " Order Moment\n";
  1876. cout << "\n";
  1877. for ( order = 0; order <= 8; order++ )
  1878. {
  1879. moment = truncated_normal_ab_moment ( order, mu, sigma, a, b );
  1880. cout << " " << setw(2) << order
  1881. << " " << setw(14) << moment << "\n";
  1882. }
  1883. }
  1884. return;
  1885. }
  1886. //****************************************************************************80
  1887. void truncated_normal_ab_pdf_test ( )
  1888. //****************************************************************************80
  1889. //
  1890. // Purpose:
  1891. //
  1892. // TRUNCATED_NORMAL_AB_PDF_TEST tests TRUNCATED_NORMAL_AB_PDF.
  1893. //
  1894. // Licensing:
  1895. //
  1896. // This code is distributed under the GNU LGPL license.
  1897. //
  1898. // Modified:
  1899. //
  1900. // 13 September 2013
  1901. //
  1902. // Author:
  1903. //
  1904. // John Burkardt
  1905. //
  1906. {
  1907. double a;
  1908. double b;
  1909. double mu;
  1910. int n_data;
  1911. double pdf1;
  1912. double pdf2;
  1913. double sigma;
  1914. double x;
  1915. cout << "\n";
  1916. cout << "TRUNCATED_NORMAL_AB_PDF_TEST:\n";
  1917. cout << " TRUNCATED_NORMAL_AB_PDF evaluates\n";
  1918. cout << " the Truncated Normal Probability Density Function.\n";
  1919. cout << "\n";
  1920. cout << " MU S A B X PDF1 PDF2\n";
  1921. cout << "\n";
  1922. n_data = 0;
  1923. for ( ; ; )
  1924. {
  1925. truncated_normal_ab_pdf_values ( n_data, mu, sigma, a, b, x, pdf1 );
  1926. if ( n_data == 0 )
  1927. {
  1928. break;
  1929. }
  1930. pdf2 = truncated_normal_ab_pdf ( x, mu, sigma, a, b );
  1931. cout << " " << setw(8) << mu
  1932. << " " << setw(8) << sigma
  1933. << " " << setw(8) << a
  1934. << " " << setw(8) << b
  1935. << " " << setw(8) << x
  1936. << " " << setprecision(16) << setw(24) << pdf1
  1937. << " " << setprecision(16)<< setw(24) << pdf2 << "\n";
  1938. }
  1939. return;
  1940. }
  1941. //****************************************************************************80
  1942. void truncated_normal_ab_sample_test ( )
  1943. //****************************************************************************80
  1944. //
  1945. // Purpose:
  1946. //
  1947. // TRUNCATED_NORMAL_AB_SAMPLE_TEST tests TRUNCATED_NORMAL_AB_SAMPLE.
  1948. //
  1949. // Licensing:
  1950. //
  1951. // This code is distributed under the GNU LGPL license.
  1952. //
  1953. // Modified:
  1954. //
  1955. // 07 March 2015
  1956. //
  1957. // Author:
  1958. //
  1959. // John Burkardt
  1960. //
  1961. {
  1962. double a;
  1963. double b;
  1964. int i;
  1965. double mu;
  1966. int sample_num = 10;
  1967. int seed;
  1968. double sigma;
  1969. double x;
  1970. a = 50.0;
  1971. b = 150.0;
  1972. mu = 100.0;
  1973. sigma = 25.0;
  1974. seed = 123456789;
  1975. cout << "\n";
  1976. cout << "TRUNCATED_NORMAL_AB_SAMPLE_TEST:\n";
  1977. cout << " TRUNCATED_NORMAL_AB_SAMPLE samples\n";
  1978. cout << " the Truncated Normal Distribution.\n";
  1979. cout << "\n";
  1980. cout << " The parent normal distribution has\n";
  1981. cout << " mean = " << mu << "\n";
  1982. cout << " standard deviation = " << sigma << "\n";
  1983. cout << " The parent distribution is truncated to\n";
  1984. cout << " the interval [" << a << "," << b << "]\n";
  1985. cout << "\n";
  1986. for ( i = 0; i < sample_num; i++ )
  1987. {
  1988. x = truncated_normal_ab_sample ( mu, sigma, a, b, seed );
  1989. cout << " " << setw(2) << i
  1990. << " " << setw(14) << x << "\n";
  1991. }
  1992. return;
  1993. }
  1994. //****************************************************************************80
  1995. void truncated_normal_ab_variance_test ( )
  1996. //****************************************************************************80
  1997. //
  1998. // Purpose:
  1999. //
  2000. // TRUNCATED_NORMAL_AB_VARIANCE_TEST tests TRUNCATED_NORMAL_AB_VARIANCE.
  2001. //
  2002. // Licensing:
  2003. //
  2004. // This code is distributed under the GNU LGPL license.
  2005. //
  2006. // Modified:
  2007. //
  2008. // 07 March 2015
  2009. //
  2010. // Author:
  2011. //
  2012. // John Burkardt
  2013. //
  2014. {
  2015. double a;
  2016. double b;
  2017. int i;
  2018. double mu;
  2019. int sample_num = 1000;
  2020. int seed;
  2021. double sigma;
  2022. double variance;
  2023. double *x;
  2024. a = 50.0;
  2025. b = 150.0;
  2026. mu = 100.0;
  2027. sigma = 25.0;
  2028. seed = 123456789;
  2029. cout << "\n";
  2030. cout << "TRUNCATED_NORMAL_AB_VARIANCE_TEST\n";
  2031. cout << " TRUNCATED_NORMAL_AB_VARIANCE computes the variance\n";
  2032. cout << " of the Truncated Normal Distribution.\n";
  2033. cout << "\n";
  2034. cout << " The parent normal distribution has\n";
  2035. cout << " mean = " << mu << "\n";
  2036. cout << " standard deviation = " << sigma << "\n";
  2037. cout << " The parent distribution is truncated to\n";
  2038. cout << " the interval [" << a << "," << b << "]\n";
  2039. variance = truncated_normal_ab_variance ( mu, sigma, a, b );
  2040. cout << "\n";
  2041. cout << " PDF variance = " << variance << "\n";
  2042. x = new double[sample_num];
  2043. for ( i = 0; i < sample_num; i++ )
  2044. {
  2045. x[i] = truncated_normal_ab_sample ( mu, sigma, a, b, seed );
  2046. }
  2047. variance = r8vec_variance ( sample_num, x );
  2048. cout << "\n";
  2049. cout << " Sample size = " << sample_num << "\n";
  2050. cout << " Sample variance = " << variance << "\n";
  2051. delete [] x;
  2052. return;
  2053. }
  2054. //****************************************************************************80
  2055. void truncated_normal_b_cdf_test ( )
  2056. //****************************************************************************80
  2057. //
  2058. // Purpose:
  2059. //
  2060. // TRUNCATED_NORMAL_B_CDF_TEST tests TRUNCATED_NORMAL_B_CDF.
  2061. //
  2062. // Licensing:
  2063. //
  2064. // This code is distributed under the GNU LGPL license.
  2065. //
  2066. // Modified:
  2067. //
  2068. // 14 September 2013
  2069. //
  2070. // Author:
  2071. //
  2072. // John Burkardt
  2073. //
  2074. {
  2075. double b;
  2076. double cdf1;
  2077. double cdf2;
  2078. double mu;
  2079. int n_data;
  2080. double sigma;
  2081. double x;
  2082. cout << "\n";
  2083. cout << "TRUNCATED_NORMAL_B_CDF_TEST:\n";
  2084. cout << " TRUNCATED_NORMAL_B_CDF evaluates\n";
  2085. cout << " the upper Truncated Normal Cumulative Density Function.\n";
  2086. cout << "\n";
  2087. cout << " MU S B X CDF1 CDF2\n";
  2088. cout << "\n";
  2089. n_data = 0;
  2090. for ( ; ; )
  2091. {
  2092. truncated_normal_b_cdf_values ( n_data, mu, sigma, b, x, cdf1 );
  2093. if ( n_data == 0 )
  2094. {
  2095. break;
  2096. }
  2097. cdf2 = truncated_normal_b_cdf ( x, mu, sigma, b );
  2098. cout << " " << setw(8) << mu
  2099. << " " << setw(8) << sigma
  2100. << " " << setw(8) << b
  2101. << " " << setw(8) << x
  2102. << " " << setprecision(16) << setw(24) << cdf1
  2103. << " " << setprecision(16) << setw(24) << cdf2 << "\n";
  2104. }
  2105. return;
  2106. }
  2107. //****************************************************************************80
  2108. void truncated_normal_b_cdf_inv_test ( )
  2109. //****************************************************************************80
  2110. //
  2111. // Purpose:
  2112. //
  2113. // TRUNCATED_NORMAL_B_CDF_INV_TEST tests TRUNCATED_NORMAL_B_CDF_INV.
  2114. //
  2115. // Licensing:
  2116. //
  2117. // This code is distributed under the GNU LGPL license.
  2118. //
  2119. // Modified:
  2120. //
  2121. // 07 March 2015
  2122. //
  2123. // Author:
  2124. //
  2125. // John Burkardt
  2126. //
  2127. {
  2128. double b;
  2129. double cdf;
  2130. int i;
  2131. double mu;
  2132. int sample_num = 10;
  2133. int seed;
  2134. double sigma;
  2135. double x;
  2136. double x2;
  2137. b = 150.0;
  2138. mu = 100.0;
  2139. sigma = 25.0;
  2140. seed = 123456789;
  2141. cout << "\n";
  2142. cout << "TRUNCATED_NORMAL_B_CDF_INV_TEST:\n";
  2143. cout << " TRUNCATED_NORMAL_B_CDF_INV inverts the CDF of\n";
  2144. cout << " the upper Truncated Normal Distribution.\n";
  2145. cout << "\n";
  2146. cout << " The parent normal distribution has\n";
  2147. cout << " mean = " << mu << "\n";
  2148. cout << " standard deviation = " << sigma << "\n";
  2149. cout << " The parent distribution is truncated to\n";
  2150. cout << " the interval (-oo," << b << "]\n";
  2151. cout << "\n";
  2152. cout << " X CDF CDF_INV\n";
  2153. cout << "\n";
  2154. for ( i = 0; i < sample_num; i++ )
  2155. {
  2156. x = truncated_normal_b_sample ( mu, sigma, b, seed );
  2157. cdf = truncated_normal_b_cdf ( x, mu, sigma, b );
  2158. x2 = truncated_normal_b_cdf_inv ( cdf, mu, sigma, b );
  2159. cout << " " << setw(2) << i
  2160. << " " << setw(14) << x
  2161. << " " << setw(14) << cdf
  2162. << " " << setw(14) << x2 << "\n";
  2163. }
  2164. return;
  2165. }
  2166. //****************************************************************************80
  2167. void truncated_normal_b_mean_test ( )
  2168. //****************************************************************************80
  2169. //
  2170. // Purpose:
  2171. //
  2172. // TRUNCATED_NORMAL_B_MEAN_TEST tests TRUNCATED_NORMAL_B_MEAN.
  2173. //
  2174. // Licensing:
  2175. //
  2176. // This code is distributed under the GNU LGPL license.
  2177. //
  2178. // Modified:
  2179. //
  2180. // 07 March 2015
  2181. //
  2182. // Author:
  2183. //
  2184. // John Burkardt
  2185. //
  2186. {
  2187. double b;
  2188. int i;
  2189. double mean;
  2190. double mu;
  2191. int sample_num = 1000;
  2192. int seed;
  2193. double sigma;
  2194. double *x;
  2195. double xmax;
  2196. double xmin;
  2197. b = 150.0;
  2198. mu = 100.0;
  2199. sigma = 25.0;
  2200. seed = 123456789;
  2201. cout << "\n";
  2202. cout << "TRUNCATED_NORMAL_B_MEAN_TEST\n";
  2203. cout << " TRUNCATED_NORMAL_B_MEAN computes the mean\n";
  2204. cout << " of the upper Truncated Normal Distribution.\n";
  2205. cout << "\n";
  2206. cout << " The parent normal distribution has\n";
  2207. cout << " mean = " << mu << "\n";
  2208. cout << " standard deviation = " << sigma << "\n";
  2209. cout << " The parent distribution is truncated to\n";
  2210. cout << " the interval (-oo," << b << "]\n";
  2211. mean = truncated_normal_b_mean ( mu, sigma, b );
  2212. cout << "\n";
  2213. cout << " PDF mean = " << mean << "\n";
  2214. x = new double[sample_num];
  2215. for ( i = 0; i < sample_num; i++ )
  2216. {
  2217. x[i] = truncated_normal_b_sample ( mu, sigma, b, seed );
  2218. }
  2219. mean = r8vec_mean ( sample_num, x );
  2220. xmax = r8vec_max ( sample_num, x );
  2221. xmin = r8vec_min ( sample_num, x );
  2222. cout << "\n";
  2223. cout << " Sample size = " << sample_num << "\n";
  2224. cout << " Sample mean = " << mean << "\n";
  2225. cout << " Sample maximum = " << xmax << "\n";
  2226. cout << " Sample minimum = " << xmin << "\n";
  2227. delete [] x;
  2228. return;
  2229. }
  2230. //****************************************************************************80
  2231. void truncated_normal_b_moment_test ( )
  2232. //****************************************************************************80
  2233. //
  2234. // Purpose:
  2235. //
  2236. // TRUNCATED_NORMAL_B_MOMENT_TEST tests TRUNCATED_NORMAL_B_MOMENT.
  2237. //
  2238. // Licensing:
  2239. //
  2240. // This code is distributed under the GNU LGPL license.
  2241. //
  2242. // Modified:
  2243. //
  2244. // 03 September 2013
  2245. //
  2246. // Author:
  2247. //
  2248. // John Burkardt
  2249. //
  2250. {
  2251. double b;
  2252. double b_test[6] = {
  2253. 0.0, 10.0, -10.0, 10.0, -10.0, 10.0 };
  2254. double moment;
  2255. double mu;
  2256. double mu_test[6] = {
  2257. 0.0, 0.0, 0.0, 0.0, 0.0, 5.0 };
  2258. int order;
  2259. double sigma;
  2260. double sigma_test[6] = {
  2261. 1.0, 1.0, 1.0, 2.0, 2.0, 1.0 };
  2262. int test;
  2263. int test_num;
  2264. test_num = 6;
  2265. cout << "\n";
  2266. cout << "TRUNCATED_NORMAL_B_MOMENT_TEST\n";
  2267. cout << " For the Upper Truncated Normal PDF:\n";
  2268. cout << " TRUNCATED_NORMAL_B_MOMENT evaluates the moments.\n";
  2269. for ( test = 0; test < test_num; test++ )
  2270. {
  2271. mu = mu_test[test];
  2272. sigma = sigma_test[test];
  2273. b = b_test[test];
  2274. cout << "\n";
  2275. cout << " Test = " << test
  2276. << ", Mu = " << mu
  2277. << ", Sigma = " << sigma
  2278. << ", B = " << b << "\n";
  2279. cout << " Order Moment\n";
  2280. cout << "\n";
  2281. for ( order = 0; order <= 8; order++ )
  2282. {
  2283. moment = truncated_normal_b_moment ( order, mu, sigma, b );
  2284. cout << " " << setw(2) << order
  2285. << " " << setw(14) << moment << "\n";
  2286. }
  2287. }
  2288. return;
  2289. }
  2290. //****************************************************************************80
  2291. void truncated_normal_b_pdf_test ( )
  2292. //****************************************************************************80
  2293. //
  2294. // Purpose:
  2295. //
  2296. // TRUNCATED_NORMAL_B_PDF_TEST tests TRUNCATED_NORMAL_B_PDF.
  2297. //
  2298. // Licensing:
  2299. //
  2300. // This code is distributed under the GNU LGPL license.
  2301. //
  2302. // Modified:
  2303. //
  2304. // 14 September 2013
  2305. //
  2306. // Author:
  2307. //
  2308. // John Burkardt
  2309. //
  2310. {
  2311. double b;
  2312. double mu;
  2313. int n_data;
  2314. double pdf1;
  2315. double pdf2;
  2316. double sigma;
  2317. double x;
  2318. cout << "\n";
  2319. cout << "TRUNCATED_NORMAL_B_PDF_TEST:\n";
  2320. cout << " TRUNCATED_NORMAL_B_PDF evaluates\n";
  2321. cout << " the upper Truncated Normal Distribution.\n";
  2322. cout << "\n";
  2323. cout << " MU S B X PDF1 PDF2\n";
  2324. cout << "\n";
  2325. n_data = 0;
  2326. for ( ; ; )
  2327. {
  2328. truncated_normal_b_pdf_values ( n_data, mu, sigma, b, x, pdf1 );
  2329. if ( n_data == 0 )
  2330. {
  2331. break;
  2332. }
  2333. pdf2 = truncated_normal_b_pdf ( x, mu, sigma, b );
  2334. cout << " " << setw(8) << mu
  2335. << " " << setw(8) << sigma
  2336. << " " << setw(8) << b
  2337. << " " << setw(8) << x
  2338. << " " << setprecision(16) << setw(24) << pdf1
  2339. << " " << setprecision(16)<< setw(24) << pdf2 << "\n";
  2340. }
  2341. return;
  2342. }
  2343. //****************************************************************************80
  2344. void truncated_normal_b_sample_test ( )
  2345. //****************************************************************************80
  2346. //
  2347. // Purpose:
  2348. //
  2349. // TRUNCATED_NORMAL_B_SAMPLE_TEST tests TRUNCATED_NORMAL_B_SAMPLE.
  2350. //
  2351. // Licensing:
  2352. //
  2353. // This code is distributed under the GNU LGPL license.
  2354. //
  2355. // Modified:
  2356. //
  2357. // 07 March 2015
  2358. //
  2359. // Author:
  2360. //
  2361. // John Burkardt
  2362. //
  2363. {
  2364. double b;
  2365. int i;
  2366. double mu;
  2367. int sample_num = 10;
  2368. int seed;
  2369. double sigma;
  2370. double x;
  2371. b = 150.0;
  2372. mu = 100.0;
  2373. sigma = 25.0;
  2374. seed = 123456789;
  2375. cout << "\n";
  2376. cout << "TRUNCATED_NORMAL_B_SAMPLE_TEST:\n";
  2377. cout << " TRUNCATED_NORMAL_B_SAMPLE samples\n";
  2378. cout << " the upper Truncated Normal Distribution.\n";
  2379. cout << "\n";
  2380. cout << " The parent normal distribution has\n";
  2381. cout << " mean = " << mu << "\n";
  2382. cout << " standard deviation = " << sigma << "\n";
  2383. cout << " The parent distribution is truncated to\n";
  2384. cout << " the interval (-oo," << b << "]\n";
  2385. cout << "\n";
  2386. for ( i = 0; i < sample_num; i++ )
  2387. {
  2388. x = truncated_normal_b_sample ( mu, sigma, b, seed );
  2389. cout << " " << setw(2) << i
  2390. << " " << setw(14) << x << "\n";
  2391. }
  2392. return;
  2393. }
  2394. //****************************************************************************80
  2395. void truncated_normal_b_variance_test ( )
  2396. //****************************************************************************80
  2397. //
  2398. // Purpose:
  2399. //
  2400. // TRUNCATED_NORMAL_B_VARIANCE_TEST tests TRUNCATED_NORMAL_B_VARIANCE.
  2401. //
  2402. // Licensing:
  2403. //
  2404. // This code is distributed under the GNU LGPL license.
  2405. //
  2406. // Modified:
  2407. //
  2408. // 07 March 2015
  2409. //
  2410. // Author:
  2411. //
  2412. // John Burkardt
  2413. //
  2414. {
  2415. double b;
  2416. int i;
  2417. double mu;
  2418. int sample_num = 1000;
  2419. int seed;
  2420. double sigma;
  2421. double variance;
  2422. double *x;
  2423. b = 150.0;
  2424. mu = 100.0;
  2425. sigma = 25.0;
  2426. seed = 123456789;
  2427. cout << "\n";
  2428. cout << "TRUNCATED_NORMAL_B_VARIANCE_TEST\n";
  2429. cout << " TRUNCATED_NORMAL_B_VARIANCE computes the variance\n";
  2430. cout << " of the upper Truncated Normal Distribution.\n";
  2431. cout << "\n";
  2432. cout << " The parent normal distribution has\n";
  2433. cout << " mean = " << mu << "\n";
  2434. cout << " standard deviation = " << sigma << "\n";
  2435. cout << " The parent distribution is truncated to\n";
  2436. cout << " the interval (-oo," << b << "]\n";
  2437. variance = truncated_normal_b_variance ( mu, sigma, b );
  2438. cout << "\n";
  2439. cout << " PDF variance = " << variance << "\n";
  2440. x = new double[sample_num];
  2441. for ( i = 0; i < sample_num; i++ )
  2442. {
  2443. x[i] = truncated_normal_b_sample ( mu, sigma, b, seed );
  2444. }
  2445. variance = r8vec_variance ( sample_num, x );
  2446. cout << "\n";
  2447. cout << " Sample size = " << sample_num << "\n";
  2448. cout << " Sample variance = " << variance << "\n";
  2449. delete [] x;
  2450. return;
  2451. }