truncated_normal.cpp 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985
  1. #include <cmath>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <iomanip>
  5. #include <iostream>
  6. #include <string>
  7. using namespace std;
  8. #include "truncated_normal.hpp"
  9. //****************************************************************************80
  10. int i4_uniform_ab(int a, int b, int &seed)
  11. //****************************************************************************80
  12. //
  13. // Purpose:
  14. //
  15. // I4_UNIFORM_AB returns a scaled pseudorandom I4 between A and B.
  16. //
  17. // Discussion:
  18. //
  19. // The pseudorandom number should be uniformly distributed
  20. // between A and B.
  21. //
  22. // Licensing:
  23. //
  24. // This code is distributed under the GNU LGPL license.
  25. //
  26. // Modified:
  27. //
  28. // 02 October 2012
  29. //
  30. // Author:
  31. //
  32. // John Burkardt
  33. //
  34. // Reference:
  35. //
  36. // Paul Bratley, Bennett Fox, Linus Schrage,
  37. // A Guide to Simulation,
  38. // Second Edition,
  39. // Springer, 1987,
  40. // ISBN: 0387964673,
  41. // LC: QA76.9.C65.B73.
  42. //
  43. // Bennett Fox,
  44. // Algorithm 647:
  45. // Implementation and Relative Efficiency of Quasirandom
  46. // Sequence Generators,
  47. // ACM Transactions on Mathematical Software,
  48. // Volume 12, Number 4, December 1986, pages 362-376.
  49. //
  50. // Pierre L'Ecuyer,
  51. // Random Number Generation,
  52. // in Handbook of Simulation,
  53. // edited by Jerry Banks,
  54. // Wiley, 1998,
  55. // ISBN: 0471134031,
  56. // LC: T57.62.H37.
  57. //
  58. // Peter Lewis, Allen Goodman, James Miller,
  59. // A Pseudo-Random Number Generator for the System/360,
  60. // IBM Systems Journal,
  61. // Volume 8, Number 2, 1969, pages 136-143.
  62. //
  63. // Parameters:
  64. //
  65. // Input, int A, B, the limits of the interval.
  66. //
  67. // Input/output, int &SEED, the "seed" value, which should NOT be 0.
  68. // On output, SEED has been updated.
  69. //
  70. // Output, int I4_UNIFORM_AB, a number between A and B.
  71. //
  72. {
  73. int c;
  74. const int i4_huge = 2147483647;
  75. int k;
  76. float r;
  77. int value;
  78. if (seed == 0) {
  79. cerr << "\n";
  80. cerr << "I4_UNIFORM_AB - Fatal error!\n";
  81. cerr << " Input value of SEED = 0.\n";
  82. exit(1);
  83. }
  84. //
  85. // Guarantee A <= B.
  86. //
  87. if (b < a) {
  88. c = a;
  89. a = b;
  90. b = c;
  91. }
  92. k = seed / 127773;
  93. seed = 16807 * (seed - k * 127773) - k * 2836;
  94. if (seed < 0) {
  95. seed = seed + i4_huge;
  96. }
  97. r = (float)(seed)*4.656612875E-10;
  98. //
  99. // Scale R to lie between A-0.5 and B+0.5.
  100. //
  101. r = (1.0 - r) * ((float)a - 0.5) + r * ((float)b + 0.5);
  102. //
  103. // Use rounding to convert R to an integer between A and B.
  104. //
  105. value = round(r);
  106. //
  107. // Guarantee A <= VALUE <= B.
  108. //
  109. if (value < a) {
  110. value = a;
  111. }
  112. if (b < value) {
  113. value = b;
  114. }
  115. return value;
  116. }
  117. //****************************************************************************80
  118. double normal_01_cdf(double x)
  119. //****************************************************************************80
  120. //
  121. // Purpose:
  122. //
  123. // NORMAL_01_CDF evaluates the Normal 01 CDF.
  124. //
  125. // Licensing:
  126. //
  127. // This code is distributed under the GNU LGPL license.
  128. //
  129. // Modified:
  130. //
  131. // 10 February 1999
  132. //
  133. // Author:
  134. //
  135. // John Burkardt
  136. //
  137. // Reference:
  138. //
  139. // A G Adams,
  140. // Areas Under the Normal Curve,
  141. // Algorithm 39,
  142. // Computer j.,
  143. // Volume 12, pages 197-198, 1969.
  144. //
  145. // Parameters:
  146. //
  147. // Input, double X, the argument of the CDF.
  148. //
  149. // Output, double CDF, the value of the CDF.
  150. //
  151. {
  152. double a1 = 0.398942280444;
  153. double a2 = 0.399903438504;
  154. double a3 = 5.75885480458;
  155. double a4 = 29.8213557808;
  156. double a5 = 2.62433121679;
  157. double a6 = 48.6959930692;
  158. double a7 = 5.92885724438;
  159. double b0 = 0.398942280385;
  160. double b1 = 3.8052E-08;
  161. double b2 = 1.00000615302;
  162. double b3 = 3.98064794E-04;
  163. double b4 = 1.98615381364;
  164. double b5 = 0.151679116635;
  165. double b6 = 5.29330324926;
  166. double b7 = 4.8385912808;
  167. double b8 = 15.1508972451;
  168. double b9 = 0.742380924027;
  169. double b10 = 30.789933034;
  170. double b11 = 3.99019417011;
  171. double cdf;
  172. double q;
  173. double y;
  174. //
  175. // |X| <= 1.28.
  176. //
  177. if (fabs(x) <= 1.28) {
  178. y = 0.5 * x * x;
  179. q = 0.5 -
  180. fabs(x) * (a1 - a2 * y / (y + a3 - a4 / (y + a5 + a6 / (y + a7))));
  181. //
  182. // 1.28 < |X| <= 12.7
  183. //
  184. } else if (fabs(x) <= 12.7) {
  185. y = 0.5 * x * x;
  186. q = exp(-y) * b0 /
  187. (fabs(x) - b1 +
  188. b2 / (fabs(x) + b3 +
  189. b4 / (fabs(x) - b5 +
  190. b6 / (fabs(x) + b7 -
  191. b8 / (fabs(x) + b9 + b10 / (fabs(x) + b11))))));
  192. //
  193. // 12.7 < |X|
  194. //
  195. } else {
  196. q = 0.0;
  197. }
  198. //
  199. // Take account of negative X.
  200. //
  201. if (x < 0.0) {
  202. cdf = q;
  203. } else {
  204. cdf = 1.0 - q;
  205. }
  206. return cdf;
  207. }
  208. //****************************************************************************80
  209. double normal_01_cdf_inv(double p)
  210. //****************************************************************************80
  211. //
  212. // Purpose:
  213. //
  214. // NORMAL_01_CDF_INV inverts the standard normal CDF.
  215. //
  216. // Discussion:
  217. //
  218. // The result is accurate to about 1 part in 10**16.
  219. //
  220. // Licensing:
  221. //
  222. // This code is distributed under the GNU LGPL license.
  223. //
  224. // Modified:
  225. //
  226. // 27 December 2004
  227. //
  228. // Author:
  229. //
  230. // Original FORTRAN77 version by Michael Wichura.
  231. // C++ version by John Burkardt.
  232. //
  233. // Reference:
  234. //
  235. // Michael Wichura,
  236. // The Percentage Points of the Normal Distribution,
  237. // Algorithm AS 241,
  238. // Applied Statistics,
  239. // Volume 37, Number 3, pages 477-484, 1988.
  240. //
  241. // Parameters:
  242. //
  243. // Input, double P, the value of the cumulative probability
  244. // densitity function. 0 < P < 1. If P is outside this range, an
  245. // "infinite" value is returned.
  246. //
  247. // Output, double NORMAL_01_CDF_INV, the normal deviate value
  248. // with the property that the probability of a standard normal deviate being
  249. // less than or equal to this value is P.
  250. //
  251. {
  252. double a[8] = {3.3871328727963666080, 1.3314166789178437745E+2,
  253. 1.9715909503065514427E+3, 1.3731693765509461125E+4,
  254. 4.5921953931549871457E+4, 6.7265770927008700853E+4,
  255. 3.3430575583588128105E+4, 2.5090809287301226727E+3};
  256. double b[8] = {1.0,
  257. 4.2313330701600911252E+1,
  258. 6.8718700749205790830E+2,
  259. 5.3941960214247511077E+3,
  260. 2.1213794301586595867E+4,
  261. 3.9307895800092710610E+4,
  262. 2.8729085735721942674E+4,
  263. 5.2264952788528545610E+3};
  264. double c[8] = {1.42343711074968357734, 4.63033784615654529590,
  265. 5.76949722146069140550, 3.64784832476320460504,
  266. 1.27045825245236838258, 2.41780725177450611770E-1,
  267. 2.27238449892691845833E-2, 7.74545014278341407640E-4};
  268. double const1 = 0.180625;
  269. double const2 = 1.6;
  270. double d[8] = {1.0,
  271. 2.05319162663775882187,
  272. 1.67638483018380384940,
  273. 6.89767334985100004550E-1,
  274. 1.48103976427480074590E-1,
  275. 1.51986665636164571966E-2,
  276. 5.47593808499534494600E-4,
  277. 1.05075007164441684324E-9};
  278. double e[8] = {6.65790464350110377720, 5.46378491116411436990,
  279. 1.78482653991729133580, 2.96560571828504891230E-1,
  280. 2.65321895265761230930E-2, 1.24266094738807843860E-3,
  281. 2.71155556874348757815E-5, 2.01033439929228813265E-7};
  282. double f[8] = {1.0,
  283. 5.99832206555887937690E-1,
  284. 1.36929880922735805310E-1,
  285. 1.48753612908506148525E-2,
  286. 7.86869131145613259100E-4,
  287. 1.84631831751005468180E-5,
  288. 1.42151175831644588870E-7,
  289. 2.04426310338993978564E-15};
  290. double q;
  291. double r;
  292. double split1 = 0.425;
  293. double split2 = 5.0;
  294. double value;
  295. if (p <= 0.0) {
  296. value = -r8_huge();
  297. return value;
  298. }
  299. if (1.0 <= p) {
  300. value = r8_huge();
  301. return value;
  302. }
  303. q = p - 0.5;
  304. if (fabs(q) <= split1) {
  305. r = const1 - q * q;
  306. value = q * r8poly_value_horner(7, a, r) / r8poly_value_horner(7, b, r);
  307. } else {
  308. if (q < 0.0) {
  309. r = p;
  310. } else {
  311. r = 1.0 - p;
  312. }
  313. if (r <= 0.0) {
  314. value = r8_huge();
  315. } else {
  316. r = sqrt(-log(r));
  317. if (r <= split2) {
  318. r = r - const2;
  319. value = r8poly_value_horner(7, c, r) / r8poly_value_horner(7, d, r);
  320. } else {
  321. r = r - split2;
  322. value = r8poly_value_horner(7, e, r) / r8poly_value_horner(7, f, r);
  323. }
  324. }
  325. if (q < 0.0) {
  326. value = -value;
  327. }
  328. }
  329. return value;
  330. }
  331. //****************************************************************************80
  332. void normal_01_cdf_values(int &n_data, double &x, double &fx)
  333. //****************************************************************************80
  334. //
  335. // Purpose:
  336. //
  337. // NORMAL_01_CDF_VALUES returns some values of the Normal 01 CDF.
  338. //
  339. // Discussion:
  340. //
  341. // In Mathematica, the function can be evaluated by:
  342. //
  343. // Needs["Statistics`ContinuousDistributions`"]
  344. // dist = NormalDistribution [ 0, 1 ]
  345. // CDF [ dist, x ]
  346. //
  347. // Licensing:
  348. //
  349. // This code is distributed under the GNU LGPL license.
  350. //
  351. // Modified:
  352. //
  353. // 28 August 2004
  354. //
  355. // Author:
  356. //
  357. // John Burkardt
  358. //
  359. // Reference:
  360. //
  361. // Milton Abramowitz, Irene Stegun,
  362. // Handbook of Mathematical Functions,
  363. // National Bureau of Standards, 1964,
  364. // ISBN: 0-486-61272-4,
  365. // LC: QA47.A34.
  366. //
  367. // Stephen Wolfram,
  368. // The Mathematica Book,
  369. // Fourth Edition,
  370. // Cambridge University Press, 1999,
  371. // ISBN: 0-521-64314-7,
  372. // LC: QA76.95.W65.
  373. //
  374. // Parameters:
  375. //
  376. // Input/output, int &N_DATA. The user sets N_DATA to 0 before the
  377. // first call. On each call, the routine increments N_DATA by 1, and
  378. // returns the corresponding data; when there is no more data, the
  379. // output value of N_DATA will be 0 again.
  380. //
  381. // Output, double &X, the argument of the function.
  382. //
  383. // Output, double &FX, the value of the function.
  384. //
  385. {
  386. #define N_MAX 17
  387. static double fx_vec[N_MAX] = {
  388. 0.5000000000000000E+00, 0.5398278372770290E+00, 0.5792597094391030E+00,
  389. 0.6179114221889526E+00, 0.6554217416103242E+00, 0.6914624612740131E+00,
  390. 0.7257468822499270E+00, 0.7580363477769270E+00, 0.7881446014166033E+00,
  391. 0.8159398746532405E+00, 0.8413447460685429E+00, 0.9331927987311419E+00,
  392. 0.9772498680518208E+00, 0.9937903346742239E+00, 0.9986501019683699E+00,
  393. 0.9997673709209645E+00, 0.9999683287581669E+00};
  394. static double x_vec[N_MAX] = {
  395. 0.0000000000000000E+00, 0.1000000000000000E+00, 0.2000000000000000E+00,
  396. 0.3000000000000000E+00, 0.4000000000000000E+00, 0.5000000000000000E+00,
  397. 0.6000000000000000E+00, 0.7000000000000000E+00, 0.8000000000000000E+00,
  398. 0.9000000000000000E+00, 0.1000000000000000E+01, 0.1500000000000000E+01,
  399. 0.2000000000000000E+01, 0.2500000000000000E+01, 0.3000000000000000E+01,
  400. 0.3500000000000000E+01, 0.4000000000000000E+01};
  401. if (n_data < 0) {
  402. n_data = 0;
  403. }
  404. n_data = n_data + 1;
  405. if (N_MAX < n_data) {
  406. n_data = 0;
  407. x = 0.0;
  408. fx = 0.0;
  409. } else {
  410. x = x_vec[n_data - 1];
  411. fx = fx_vec[n_data - 1];
  412. }
  413. return;
  414. #undef N_MAX
  415. }
  416. //****************************************************************************80
  417. double normal_01_mean()
  418. //****************************************************************************80
  419. //
  420. // Purpose:
  421. //
  422. // NORMAL_01_MEAN returns the mean of the Normal 01 PDF.
  423. //
  424. // Licensing:
  425. //
  426. // This code is distributed under the GNU LGPL license.
  427. //
  428. // Modified:
  429. //
  430. // 18 September 2004
  431. //
  432. // Author:
  433. //
  434. // John Burkardt
  435. //
  436. // Parameters:
  437. //
  438. // Output, double MEAN, the mean of the PDF.
  439. //
  440. {
  441. double mean;
  442. mean = 0.0;
  443. return mean;
  444. }
  445. //****************************************************************************80
  446. double normal_01_moment(int order)
  447. //****************************************************************************80
  448. //
  449. // Purpose:
  450. //
  451. // NORMAL_01_MOMENT evaluates moments of the Normal 01 PDF.
  452. //
  453. // Licensing:
  454. //
  455. // This code is distributed under the GNU LGPL license.
  456. //
  457. // Modified:
  458. //
  459. // 31 August 2013
  460. //
  461. // Author:
  462. //
  463. // John Burkardt
  464. //
  465. // Parameters:
  466. //
  467. // Input, int ORDER, the order of the moment.
  468. // 0 <= ORDER.
  469. //
  470. // Output, double NORMAL_01_MOMENT, the value of the moment.
  471. //
  472. {
  473. double value;
  474. if ((order % 2) == 0) {
  475. value = r8_factorial2(order - 1);
  476. } else {
  477. value = 0.0;
  478. }
  479. return value;
  480. }
  481. //****************************************************************************80
  482. double normal_01_pdf(double x)
  483. //****************************************************************************80
  484. //
  485. // Purpose:
  486. //
  487. // NORMAL_01_PDF evaluates the Normal 01 PDF.
  488. //
  489. // Discussion:
  490. //
  491. // The Normal 01 PDF is also called the "Standard Normal" PDF, or
  492. // the Normal PDF with 0 mean and standard deviation 1.
  493. //
  494. // PDF(X) = exp ( - 0.5 * X^2 ) / sqrt ( 2 * PI )
  495. //
  496. // Licensing:
  497. //
  498. // This code is distributed under the GNU LGPL license.
  499. //
  500. // Modified:
  501. //
  502. // 18 September 2004
  503. //
  504. // Author:
  505. //
  506. // John Burkardt
  507. //
  508. // Parameters:
  509. //
  510. // Input, double X, the argument of the PDF.
  511. //
  512. // Output, double PDF, the value of the PDF.
  513. //
  514. {
  515. double pdf;
  516. const double r8_pi = 3.14159265358979323;
  517. pdf = exp(-0.5 * x * x) / sqrt(2.0 * r8_pi);
  518. return pdf;
  519. }
  520. //****************************************************************************80
  521. double normal_01_sample(int &seed)
  522. //****************************************************************************80
  523. //
  524. // Purpose:
  525. //
  526. // NORMAL_01_SAMPLE samples the standard normal probability distribution.
  527. //
  528. // Discussion:
  529. //
  530. // The standard normal probability distribution function (PDF) has
  531. // mean 0 and standard deviation 1.
  532. //
  533. // The Box-Muller method is used, which is efficient, but
  534. // generates two values at a time.
  535. //
  536. // Licensing:
  537. //
  538. // This code is distributed under the GNU LGPL license.
  539. //
  540. // Modified:
  541. //
  542. // 26 August 2013
  543. //
  544. // Author:
  545. //
  546. // John Burkardt
  547. //
  548. // Parameters:
  549. //
  550. // Input/output, int &SEED, a seed for the random number generator.
  551. //
  552. // Output, double NORMAL_01_SAMPLE, a normally distributed random value.
  553. //
  554. {
  555. double r1;
  556. double r2;
  557. const double r8_pi = 3.14159265358979323;
  558. double x;
  559. r1 = r8_uniform_01(seed);
  560. r2 = r8_uniform_01(seed);
  561. x = sqrt(-2.0 * log(r1)) * cos(2.0 * r8_pi * r2);
  562. return x;
  563. }
  564. //****************************************************************************80
  565. double normal_01_variance()
  566. //****************************************************************************80
  567. //
  568. // Purpose:
  569. //
  570. // NORMAL_01_VARIANCE returns the variance of the Normal 01 PDF.
  571. //
  572. // Licensing:
  573. //
  574. // This code is distributed under the GNU LGPL license.
  575. //
  576. // Modified:
  577. //
  578. // 18 September 2004
  579. //
  580. // Author:
  581. //
  582. // John Burkardt
  583. //
  584. // Parameters:
  585. //
  586. // Output, double VARIANCE, the variance of the PDF.
  587. //
  588. {
  589. double variance;
  590. variance = 1.0;
  591. return variance;
  592. }
  593. //****************************************************************************80
  594. double normal_ms_cdf(double x, double mu, double sigma)
  595. //****************************************************************************80
  596. //
  597. // Purpose:
  598. //
  599. // NORMAL_MS_CDF evaluates the Normal CDF.
  600. //
  601. // Licensing:
  602. //
  603. // This code is distributed under the GNU LGPL license.
  604. //
  605. // Modified:
  606. //
  607. // 19 September 2004
  608. //
  609. // Author:
  610. //
  611. // John Burkardt
  612. //
  613. // Parameters:
  614. //
  615. // Input, double X, the argument of the CDF.
  616. //
  617. // Input, double MU, SIGMA, the parameters of the PDF.
  618. // 0.0 < SIGMA.
  619. //
  620. // Output, double NORMAL_MS_CDF, the value of the CDF.
  621. //
  622. {
  623. double cdf;
  624. double y;
  625. y = (x - mu) / sigma;
  626. cdf = normal_01_cdf(y);
  627. return cdf;
  628. }
  629. //****************************************************************************80
  630. double normal_ms_cdf_inv(double cdf, double mu, double sigma)
  631. //****************************************************************************80
  632. //
  633. // Purpose:
  634. //
  635. // NORMAL_MS_CDF_INV inverts the Normal CDF.
  636. //
  637. // Licensing:
  638. //
  639. // This code is distributed under the GNU LGPL license.
  640. //
  641. // Modified:
  642. //
  643. // 19 September 2004
  644. //
  645. // Author:
  646. //
  647. // John Burkardt
  648. //
  649. // Reference:
  650. //
  651. // Algorithm AS 111,
  652. // Applied Statistics,
  653. // Volume 26, pages 118-121, 1977.
  654. //
  655. // Parameters:
  656. //
  657. // Input, double CDF, the value of the CDF.
  658. // 0.0 <= CDF <= 1.0.
  659. //
  660. // Input, double MU, SIGMA, the parameters of the PDF.
  661. // 0.0 < SIGMA.
  662. //
  663. // Output, double NORMAL_MS_CDF_INV, the corresponding argument.
  664. //
  665. {
  666. double x;
  667. double x2;
  668. if (cdf < 0.0 || 1.0 < cdf) {
  669. cout << "\n";
  670. cout << "NORMAL_MS_CDF_INV - Fatal error!\n";
  671. cout << " CDF < 0 or 1 < CDF.\n";
  672. exit(1);
  673. }
  674. x2 = normal_01_cdf_inv(cdf);
  675. x = mu + sigma * x2;
  676. return x;
  677. }
  678. //****************************************************************************80
  679. double normal_ms_mean(double mu, double sigma)
  680. //****************************************************************************80
  681. //
  682. // Purpose:
  683. //
  684. // NORMAL_MS_MEAN returns the mean of the Normal PDF.
  685. //
  686. // Licensing:
  687. //
  688. // This code is distributed under the GNU LGPL license.
  689. //
  690. // Modified:
  691. //
  692. // 19 September 2004
  693. //
  694. // Author:
  695. //
  696. // John Burkardt
  697. //
  698. // Parameters:
  699. //
  700. // Input, double MU, SIGMA, the parameters of the PDF.
  701. // 0.0 < SIGMA.
  702. //
  703. // Output, double NORMAL_MS_MEAN, the mean of the PDF.
  704. //
  705. {
  706. double mean;
  707. mean = mu;
  708. return mean;
  709. }
  710. //****************************************************************************80
  711. double normal_ms_moment(int order, double mu, double sigma)
  712. //****************************************************************************80
  713. //
  714. // Purpose:
  715. //
  716. // NORMAL_MS_MOMENT evaluates moments of the Normal PDF.
  717. //
  718. // Discussion:
  719. //
  720. // The formula was posted by John D Cook.
  721. //
  722. // Order Moment
  723. // ----- ------
  724. // 0 1
  725. // 1 mu
  726. // 2 mu^2 + sigma^2
  727. // 3 mu^3 + 3 mu sigma^2
  728. // 4 mu^4 + 6 mu^2 sigma^2 + 3 sigma^4
  729. // 5 mu^5 + 10 mu^3 sigma^2 + 15 mu sigma^4
  730. // 6 mu^6 + 15 mu^4 sigma^2 + 45 mu^2 sigma^4 + 15 sigma^6
  731. // 7 mu^7 + 21 mu^5 sigma^2 + 105 mu^3 sigma^4 + 105 mu sigma^6
  732. // 8 mu^8 + 28 mu^6 sigma^2 + 210 mu^4 sigma^4 + 420 mu^2 sigma^6
  733. // + 105 sigma^8
  734. //
  735. // Licensing:
  736. //
  737. // This code is distributed under the GNU LGPL license.
  738. //
  739. // Modified:
  740. //
  741. // 31 August 2013
  742. //
  743. // Author:
  744. //
  745. // John Burkardt
  746. //
  747. // Parameters:
  748. //
  749. // Input, int ORDER, the order of the moment.
  750. // 0 <= ORDER.
  751. //
  752. // Input, double MU, the mean of the distribution.
  753. //
  754. // Input, double SIGMA, the standard deviation of the distribution.
  755. //
  756. // Output, double NORMAL_MS_MOMENT, the value of the central moment.
  757. //
  758. {
  759. int j;
  760. int j_hi;
  761. double value;
  762. j_hi = (order / 2);
  763. value = 0.0;
  764. for (j = 0; j <= j_hi; j++) {
  765. value = value +
  766. r8_choose(order, 2 * j) * r8_factorial2(2 * j - 1) *
  767. pow(mu, order - 2 * j) * pow(sigma, 2 * j);
  768. }
  769. return value;
  770. }
  771. //****************************************************************************80
  772. double normal_ms_moment_central(int order, double mu, double sigma)
  773. //****************************************************************************80
  774. //
  775. // Purpose:
  776. //
  777. // NORMAL_MS_MOMENT_CENTRAL evaluates central moments of the Normal PDF.
  778. //
  779. // Licensing:
  780. //
  781. // This code is distributed under the GNU LGPL license.
  782. //
  783. // Modified:
  784. //
  785. // 31 August 2013
  786. //
  787. // Author:
  788. //
  789. // John Burkardt
  790. //
  791. // Parameters:
  792. //
  793. // Input, int ORDER, the order of the moment.
  794. // 0 <= ORDER.
  795. //
  796. // Input, double MU, the mean of the distribution.
  797. //
  798. // Input, double SIGMA, the standard deviation of the distribution.
  799. //
  800. // Output, double NORMAL_MS_MOMENT_CENTRAL, the value of the central moment.
  801. //
  802. {
  803. double value;
  804. if ((order % 2) == 0) {
  805. value = r8_factorial2(order - 1) * pow(sigma, order);
  806. } else {
  807. value = 0.0;
  808. }
  809. return value;
  810. }
  811. //****************************************************************************80
  812. double normal_ms_moment_central_values(int order, double mu, double sigma)
  813. //****************************************************************************80
  814. //
  815. // Purpose:
  816. //
  817. // NORMAL_MS_MOMENT_CENTRAL_VALUES: moments 0 through 10 of the Normal PDF.
  818. //
  819. // Discussion:
  820. //
  821. // The formula was posted by John D Cook.
  822. //
  823. // Order Moment
  824. // ----- ------
  825. // 0 1
  826. // 1 0
  827. // 2 sigma^2
  828. // 3 0
  829. // 4 3 sigma^4
  830. // 5 0
  831. // 6 15 sigma^6
  832. // 7 0
  833. // 8 105 sigma^8
  834. // 9 0
  835. // 10 945 sigma^10
  836. //
  837. // Licensing:
  838. //
  839. // This code is distributed under the GNU LGPL license.
  840. //
  841. // Modified:
  842. //
  843. // 31 August 2013
  844. //
  845. // Author:
  846. //
  847. // John Burkardt
  848. //
  849. // Parameters:
  850. //
  851. // Input, int ORDER, the order of the moment.
  852. // 0 <= ORDER <= 10.
  853. //
  854. // Input, double MU, the mean of the distribution.
  855. //
  856. // Input, double SIGMA, the standard deviation of the distribution.
  857. //
  858. // Output, double NORMAL_MS_MOMENT_CENTRAL_VALUES, the value of the
  859. // central moment.
  860. //
  861. {
  862. double value;
  863. if (order == 0) {
  864. value = 1.0;
  865. } else if (order == 1) {
  866. value = 0.0;
  867. } else if (order == 2) {
  868. value = pow(sigma, 2);
  869. } else if (order == 3) {
  870. value = 0.0;
  871. } else if (order == 4) {
  872. value = 3.0 * pow(sigma, 4);
  873. } else if (order == 5) {
  874. value = 0.0;
  875. } else if (order == 6) {
  876. value = 15.0 * pow(sigma, 6);
  877. } else if (order == 7) {
  878. value = 0.0;
  879. } else if (order == 8) {
  880. value = 105.0 * pow(sigma, 8);
  881. } else if (order == 9) {
  882. value = 0.0;
  883. } else if (order == 10) {
  884. value = 945.0 * pow(sigma, 10);
  885. } else {
  886. cerr << "\n";
  887. cerr << "NORMAL_MS_MOMENT_CENTRAL_VALUES - Fatal error!\n";
  888. cerr << " Only ORDERS 0 through 10 are available.\n";
  889. exit(1);
  890. }
  891. return value;
  892. }
  893. //****************************************************************************80
  894. double normal_ms_moment_values(int order, double mu, double sigma)
  895. //****************************************************************************80
  896. //
  897. // Purpose:
  898. //
  899. // NORMAL_MS_MOMENT_VALUES evaluates moments 0 through 8 of the Normal PDF.
  900. //
  901. // Discussion:
  902. //
  903. // The formula was posted by John D Cook.
  904. //
  905. // Order Moment
  906. // ----- ------
  907. // 0 1
  908. // 1 mu
  909. // 2 mu^2 + sigma^2
  910. // 3 mu^3 + 3 mu sigma^2
  911. // 4 mu^4 + 6 mu^2 sigma^2 + 3 sigma^4
  912. // 5 mu^5 + 10 mu^3 sigma^2 + 15 mu sigma^4
  913. // 6 mu^6 + 15 mu^4 sigma^2 + 45 mu^2 sigma^4 + 15 sigma^6
  914. // 7 mu^7 + 21 mu^5 sigma^2 + 105 mu^3 sigma^4 + 105 mu sigma^6
  915. // 8 mu^8 + 28 mu^6 sigma^2 + 210 mu^4 sigma^4 + 420 mu^2 sigma^6
  916. // + 105 sigma^8
  917. //
  918. // Licensing:
  919. //
  920. // This code is distributed under the GNU LGPL license.
  921. //
  922. // Modified:
  923. //
  924. // 31 August 2013
  925. //
  926. // Author:
  927. //
  928. // John Burkardt
  929. //
  930. // Parameters:
  931. //
  932. // Input, int ORDER, the order of the moment.
  933. // 0 <= ORDER <= 8.
  934. //
  935. // Input, double MU, the mean of the distribution.
  936. //
  937. // Input, double SIGMA, the standard deviation of the distribution.
  938. //
  939. // Output, double NORMAL_MS_MOMENT_VALUES, the value of the central moment.
  940. //
  941. {
  942. double value;
  943. if (order == 0) {
  944. value = 1.0;
  945. } else if (order == 1) {
  946. value = mu;
  947. } else if (order == 2) {
  948. value = pow(mu, 2) + pow(sigma, 2);
  949. } else if (order == 3) {
  950. value = pow(mu, 3) + 3.0 * mu * pow(sigma, 2);
  951. } else if (order == 4) {
  952. value = pow(mu, 4) + 6.0 * pow(mu, 2) * pow(sigma, 2) + 3.0 * pow(sigma, 4);
  953. } else if (order == 5) {
  954. value = pow(mu, 5) + 10.0 * pow(mu, 3) * pow(sigma, 2) +
  955. 15.0 * mu * pow(sigma, 4);
  956. } else if (order == 6) {
  957. value = pow(mu, 6) + 15.0 * pow(mu, 4) * pow(sigma, 2) +
  958. 45.0 * pow(mu, 2) * pow(sigma, 4) + 15.0 * pow(sigma, 6);
  959. } else if (order == 7) {
  960. value = pow(mu, 7) + 21.0 * pow(mu, 5) * pow(sigma, 2) +
  961. 105.0 * pow(mu, 3) * pow(sigma, 4) + 105.0 * mu * pow(sigma, 6);
  962. } else if (order == 8) {
  963. value = pow(mu, 8) + 28.0 * pow(mu, 6) * pow(sigma, 2) +
  964. 210.0 * pow(mu, 4) * pow(sigma, 4) +
  965. 420.0 * pow(mu, 2) * pow(sigma, 6) + 105.0 * pow(sigma, 8);
  966. } else {
  967. cerr << "\n";
  968. cerr << "NORMAL_MS_MOMENT_VALUES - Fatal error!\n";
  969. cerr << " Only ORDERS 0 through 8 are available.\n";
  970. exit(1);
  971. }
  972. return value;
  973. }
  974. //****************************************************************************80
  975. double normal_ms_pdf(double x, double mu, double sigma)
  976. //****************************************************************************80
  977. //
  978. // Purpose:
  979. //
  980. // NORMAL_MS_PDF evaluates the Normal PDF.
  981. //
  982. // Discussion:
  983. //
  984. // PDF(MU,SIGMA;X)
  985. // = exp ( - 0.5 * ( ( X - MU ) / SIGMA )^2 )
  986. // / ( SIGMA * SQRT ( 2 * PI ) )
  987. //
  988. // The normal PDF is also known as the Gaussian PDF.
  989. //
  990. // Licensing:
  991. //
  992. // This code is distributed under the GNU LGPL license.
  993. //
  994. // Modified:
  995. //
  996. // 19 September 2004
  997. //
  998. // Author:
  999. //
  1000. // John Burkardt
  1001. //
  1002. // Parameters:
  1003. //
  1004. // Input, double X, the argument of the PDF.
  1005. //
  1006. // Input, double MU, SIGMA, the parameters of the PDF.
  1007. // 0.0 < SIGMA.
  1008. //
  1009. // Output, double NORMAL_MS_PDF, the value of the PDF.
  1010. //
  1011. {
  1012. double pdf;
  1013. const double r8_pi = 3.14159265358979323;
  1014. double y;
  1015. y = (x - mu) / sigma;
  1016. pdf = exp(-0.5 * y * y) / (sigma * sqrt(2.0 * r8_pi));
  1017. return pdf;
  1018. }
  1019. //****************************************************************************80
  1020. double normal_ms_sample(double mu, double sigma, int &seed)
  1021. //****************************************************************************80
  1022. //
  1023. // Purpose:
  1024. //
  1025. // NORMAL_MS_SAMPLE samples the Normal PDF.
  1026. //
  1027. // Licensing:
  1028. //
  1029. // This code is distributed under the GNU LGPL license.
  1030. //
  1031. // Modified:
  1032. //
  1033. // 20 November 2004
  1034. //
  1035. // Author:
  1036. //
  1037. // John Burkardt
  1038. //
  1039. // Parameters:
  1040. //
  1041. // Input, double MU, SIGMA, the parameters of the PDF.
  1042. // 0.0 < SIGMA.
  1043. //
  1044. // Input/output, int &SEED, a seed for the random number generator.
  1045. //
  1046. // Output, double NORMAL_MS_SAMPLE, a sample of the PDF.
  1047. //
  1048. {
  1049. double x;
  1050. x = normal_01_sample(seed);
  1051. x = mu + sigma * x;
  1052. return x;
  1053. }
  1054. //****************************************************************************80
  1055. double normal_ms_variance(double mu, double sigma)
  1056. //****************************************************************************80
  1057. //
  1058. // Purpose:
  1059. //
  1060. // NORMAL_MS_VARIANCE returns the variance of the Normal PDF.
  1061. //
  1062. // Licensing:
  1063. //
  1064. // This code is distributed under the GNU LGPL license.
  1065. //
  1066. // Modified:
  1067. //
  1068. // 19 September 2004
  1069. //
  1070. // Author:
  1071. //
  1072. // John Burkardt
  1073. //
  1074. // Parameters:
  1075. //
  1076. // Input, double MU, SIGMA, the parameters of the PDF.
  1077. // 0.0 < SIGMA.
  1078. //
  1079. // Output, double NORMAL_MS_VARIANCE, the variance of the PDF.
  1080. //
  1081. {
  1082. double variance;
  1083. variance = sigma * sigma;
  1084. return variance;
  1085. }
  1086. //****************************************************************************80
  1087. double r8_abs(double x)
  1088. //****************************************************************************80
  1089. //
  1090. // Purpose:
  1091. //
  1092. // R8_ABS returns the absolute value of an R8.
  1093. //
  1094. // Licensing:
  1095. //
  1096. // This code is distributed under the GNU LGPL license.
  1097. //
  1098. // Modified:
  1099. //
  1100. // 14 November 2006
  1101. //
  1102. // Author:
  1103. //
  1104. // John Burkardt
  1105. //
  1106. // Parameters:
  1107. //
  1108. // Input, double X, the quantity whose absolute value is desired.
  1109. //
  1110. // Output, double R8_ABS, the absolute value of X.
  1111. //
  1112. {
  1113. double value;
  1114. if (0.0 <= x) {
  1115. value = x;
  1116. } else {
  1117. value = -x;
  1118. }
  1119. return value;
  1120. }
  1121. //****************************************************************************80
  1122. double r8_choose(int n, int k)
  1123. //****************************************************************************80
  1124. //
  1125. // Purpose:
  1126. //
  1127. // R8_CHOOSE computes the binomial coefficient C(N,K) as an R8.
  1128. //
  1129. // Discussion:
  1130. //
  1131. // The value is calculated in such a way as to avoid overflow and
  1132. // roundoff. The calculation is done in R8 arithmetic.
  1133. //
  1134. // The formula used is:
  1135. //
  1136. // C(N,K) = N! / ( K! * (N-K)! )
  1137. //
  1138. // Licensing:
  1139. //
  1140. // This code is distributed under the GNU LGPL license.
  1141. //
  1142. // Modified:
  1143. //
  1144. // 09 June 2013
  1145. //
  1146. // Author:
  1147. //
  1148. // John Burkardt
  1149. //
  1150. // Reference:
  1151. //
  1152. // ML Wolfson, HV Wright,
  1153. // Algorithm 160:
  1154. // Combinatorial of M Things Taken N at a Time,
  1155. // Communications of the ACM,
  1156. // Volume 6, Number 4, April 1963, page 161.
  1157. //
  1158. // Parameters:
  1159. //
  1160. // Input, int N, K, the values of N and K.
  1161. //
  1162. // Output, double R8_CHOOSE, the number of combinations of N
  1163. // things taken K at a time.
  1164. //
  1165. {
  1166. int i;
  1167. int mn;
  1168. int mx;
  1169. double value;
  1170. if (k < n - k) {
  1171. mn = k;
  1172. mx = n - k;
  1173. } else {
  1174. mn = n - k;
  1175. mx = k;
  1176. }
  1177. if (mn < 0) {
  1178. value = 0.0;
  1179. } else if (mn == 0) {
  1180. value = 1.0;
  1181. } else {
  1182. value = (double)(mx + 1);
  1183. for (i = 2; i <= mn; i++) {
  1184. value = (value * (double)(mx + i)) / (double)i;
  1185. }
  1186. }
  1187. return value;
  1188. }
  1189. //****************************************************************************80
  1190. double r8_factorial2(int n)
  1191. //****************************************************************************80
  1192. //
  1193. // Purpose:
  1194. //
  1195. // R8_FACTORIAL2 computes the double factorial function.
  1196. //
  1197. // Discussion:
  1198. //
  1199. // FACTORIAL2( N ) = Product ( N * (N-2) * (N-4) * ... * 2 ) (N even)
  1200. // = Product ( N * (N-2) * (N-4) * ... * 1 ) (N odd)
  1201. //
  1202. // Example:
  1203. //
  1204. // N Value
  1205. //
  1206. // 0 1
  1207. // 1 1
  1208. // 2 2
  1209. // 3 3
  1210. // 4 8
  1211. // 5 15
  1212. // 6 48
  1213. // 7 105
  1214. // 8 384
  1215. // 9 945
  1216. // 10 3840
  1217. //
  1218. // Licensing:
  1219. //
  1220. // This code is distributed under the GNU LGPL license.
  1221. //
  1222. // Modified:
  1223. //
  1224. // 22 January 2008
  1225. //
  1226. // Author:
  1227. //
  1228. // John Burkardt
  1229. //
  1230. // Parameters:
  1231. //
  1232. // Input, int N, the argument of the double factorial
  1233. // function. If N is less than 1, R8_FACTORIAL2 is returned as 1.0.
  1234. //
  1235. // Output, double R8_FACTORIAL2, the value of Factorial2(N).
  1236. //
  1237. {
  1238. int n_copy;
  1239. double value;
  1240. value = 1.0;
  1241. if (n < 1) {
  1242. return value;
  1243. }
  1244. n_copy = n;
  1245. while (1 < n_copy) {
  1246. value = value * (double)n_copy;
  1247. n_copy = n_copy - 2;
  1248. }
  1249. return value;
  1250. }
  1251. //****************************************************************************80
  1252. void r8_factorial2_values(int &n_data, int &n, double &f)
  1253. //****************************************************************************80
  1254. //
  1255. // Purpose:
  1256. //
  1257. // R8_FACTORIAL2_VALUES returns values of the double factorial function.
  1258. //
  1259. // Formula:
  1260. //
  1261. // FACTORIAL2( N ) = Product ( N * (N-2) * (N-4) * ... * 2 ) (N even)
  1262. // = Product ( N * (N-2) * (N-4) * ... * 1 ) (N odd)
  1263. //
  1264. // In Mathematica, the function can be evaluated by:
  1265. //
  1266. // n!!
  1267. //
  1268. // Example:
  1269. //
  1270. // N N!!
  1271. //
  1272. // 0 1
  1273. // 1 1
  1274. // 2 2
  1275. // 3 3
  1276. // 4 8
  1277. // 5 15
  1278. // 6 48
  1279. // 7 105
  1280. // 8 384
  1281. // 9 945
  1282. // 10 3840
  1283. //
  1284. // Licensing:
  1285. //
  1286. // This code is distributed under the GNU LGPL license.
  1287. //
  1288. // Modified:
  1289. //
  1290. // 07 February 2015
  1291. //
  1292. // Author:
  1293. //
  1294. // John Burkardt
  1295. //
  1296. // Reference:
  1297. //
  1298. // Milton Abramowitz, Irene Stegun,
  1299. // Handbook of Mathematical Functions,
  1300. // National Bureau of Standards, 1964,
  1301. // ISBN: 0-486-61272-4,
  1302. // LC: QA47.A34.
  1303. //
  1304. // Stephen Wolfram,
  1305. // The Mathematica Book,
  1306. // Fourth Edition,
  1307. // Cambridge University Press, 1999,
  1308. // ISBN: 0-521-64314-7,
  1309. // LC: QA76.95.W65.
  1310. //
  1311. // Daniel Zwillinger,
  1312. // CRC Standard Mathematical Tables and Formulae,
  1313. // 30th Edition,
  1314. // CRC Press, 1996, page 16.
  1315. //
  1316. // Parameters:
  1317. //
  1318. // Input/output, int &N_DATA. The user sets N_DATA to 0 before the
  1319. // first call. On each call, the routine increments N_DATA by 1, and
  1320. // returns the corresponding data; when there is no more data, the
  1321. // output value of N_DATA will be 0 again.
  1322. //
  1323. // Output, int &N, the argument of the function.
  1324. //
  1325. // Output, double &F, the value of the function.
  1326. //
  1327. {
  1328. #define N_MAX 16
  1329. static double f_vec[N_MAX] = {
  1330. 1.0, 1.0, 2.0, 3.0, 8.0, 15.0, 48.0, 105.0,
  1331. 384.0, 945.0, 3840.0, 10395.0, 46080.0, 135135.0, 645120.0, 2027025.0};
  1332. static int n_vec[N_MAX] = {0, 1, 2, 3, 4, 5, 6, 7,
  1333. 8, 9, 10, 11, 12, 13, 14, 15};
  1334. if (n_data < 0) {
  1335. n_data = 0;
  1336. }
  1337. n_data = n_data + 1;
  1338. if (N_MAX < n_data) {
  1339. n_data = 0;
  1340. n = 0;
  1341. f = 0.0;
  1342. } else {
  1343. n = n_vec[n_data - 1];
  1344. f = f_vec[n_data - 1];
  1345. }
  1346. return;
  1347. #undef N_MAX
  1348. }
  1349. //****************************************************************************80
  1350. double r8_huge()
  1351. //****************************************************************************80
  1352. //
  1353. // Purpose:
  1354. //
  1355. // R8_HUGE returns a "huge" R8.
  1356. //
  1357. // Discussion:
  1358. //
  1359. // HUGE_VAL is the largest representable legal real number, and is usually
  1360. // defined in math.h, or sometimes in stdlib.h.
  1361. //
  1362. // Licensing:
  1363. //
  1364. // This code is distributed under the GNU LGPL license.
  1365. //
  1366. // Modified:
  1367. //
  1368. // 08 May 2003
  1369. //
  1370. // Author:
  1371. //
  1372. // John Burkardt
  1373. //
  1374. // Parameters:
  1375. //
  1376. // Output, double R8_HUGE, a "huge" real value.
  1377. //
  1378. {
  1379. return HUGE_VAL;
  1380. }
  1381. //****************************************************************************80
  1382. double r8_log_2(double x)
  1383. //****************************************************************************80
  1384. //
  1385. // Purpose:
  1386. //
  1387. // R8_LOG_2 returns the logarithm base 2 of the absolute value of an R8.
  1388. //
  1389. // Discussion:
  1390. //
  1391. // value = Log ( |X| ) / Log ( 2.0 )
  1392. //
  1393. // Licensing:
  1394. //
  1395. // This code is distributed under the GNU LGPL license.
  1396. //
  1397. // Modified:
  1398. //
  1399. // 22 March 2004
  1400. //
  1401. // Author:
  1402. //
  1403. // John Burkardt
  1404. //
  1405. // Parameters:
  1406. //
  1407. // Input, double X, the number whose base 2 logarithm is desired.
  1408. // X should not be 0.
  1409. //
  1410. // Output, double R8_LOG_2, the logarithm base 2 of the absolute
  1411. // value of X. It should be true that |X| = 2^R_LOG_2.
  1412. //
  1413. {
  1414. double value;
  1415. if (x == 0.0) {
  1416. value = -r8_huge();
  1417. } else {
  1418. value = log(fabs(x)) / log(2.0);
  1419. }
  1420. return value;
  1421. }
  1422. //****************************************************************************80
  1423. double r8_mop(int i)
  1424. //****************************************************************************80
  1425. //
  1426. // Purpose:
  1427. //
  1428. // R8_MOP returns the I-th power of -1 as an R8 value.
  1429. //
  1430. // Discussion:
  1431. //
  1432. // An R8 is an double value.
  1433. //
  1434. // Licensing:
  1435. //
  1436. // This code is distributed under the GNU LGPL license.
  1437. //
  1438. // Modified:
  1439. //
  1440. // 16 November 2007
  1441. //
  1442. // Author:
  1443. //
  1444. // John Burkardt
  1445. //
  1446. // Parameters:
  1447. //
  1448. // Input, int I, the power of -1.
  1449. //
  1450. // Output, double R8_MOP, the I-th power of -1.
  1451. //
  1452. {
  1453. double value;
  1454. if ((i % 2) == 0) {
  1455. value = 1.0;
  1456. } else {
  1457. value = -1.0;
  1458. }
  1459. return value;
  1460. }
  1461. //****************************************************************************80
  1462. double r8_uniform_01(int &seed)
  1463. //****************************************************************************80
  1464. //
  1465. // Purpose:
  1466. //
  1467. // R8_UNIFORM_01 returns a unit pseudorandom R8.
  1468. //
  1469. // Discussion:
  1470. //
  1471. // This routine implements the recursion
  1472. //
  1473. // seed = 16807 * seed mod ( 2^31 - 1 )
  1474. // unif = seed / ( 2^31 - 1 )
  1475. //
  1476. // The integer arithmetic never requires more than 32 bits,
  1477. // including a sign bit.
  1478. //
  1479. // Licensing:
  1480. //
  1481. // This code is distributed under the GNU LGPL license.
  1482. //
  1483. // Modified:
  1484. //
  1485. // 11 August 2004
  1486. //
  1487. // Author:
  1488. //
  1489. // John Burkardt
  1490. //
  1491. // Reference:
  1492. //
  1493. // Paul Bratley, Bennett Fox, Linus Schrage,
  1494. // A Guide to Simulation,
  1495. // Springer Verlag, pages 201-202, 1983.
  1496. //
  1497. // Bennett Fox,
  1498. // Algorithm 647:
  1499. // Implementation and Relative Efficiency of Quasirandom
  1500. // Sequence Generators,
  1501. // ACM Transactions on Mathematical Software,
  1502. // Volume 12, Number 4, pages 362-376, 1986.
  1503. //
  1504. // Parameters:
  1505. //
  1506. // Input/output, int &SEED, the "seed" value. Normally, this
  1507. // value should not be 0. On output, SEED has been updated.
  1508. //
  1509. // Output, double R8_UNIFORM_01, a new pseudorandom variate, strictly between
  1510. // 0 and 1.
  1511. //
  1512. {
  1513. int k;
  1514. double r;
  1515. k = seed / 127773;
  1516. seed = 16807 * (seed - k * 127773) - k * 2836;
  1517. if (seed < 0) {
  1518. seed = seed + 2147483647;
  1519. }
  1520. r = (double)(seed)*4.656612875E-10;
  1521. return r;
  1522. }
  1523. //****************************************************************************80
  1524. void r8poly_print(int n, double a[], string title)
  1525. //****************************************************************************80
  1526. //
  1527. // Purpose:
  1528. //
  1529. // R8POLY_PRINT prints out a polynomial.
  1530. //
  1531. // Licensing:
  1532. //
  1533. // This code is distributed under the GNU LGPL license.
  1534. //
  1535. // Modified:
  1536. //
  1537. // 26 February 2015
  1538. //
  1539. // Author:
  1540. //
  1541. // John Burkardt
  1542. //
  1543. // Parameters:
  1544. //
  1545. // Input, int N, the dimension of A.
  1546. //
  1547. // Input, double A[N+1], the polynomial coefficients.
  1548. // A(0) is the constant term and
  1549. // A(N) is the coefficient of X^N.
  1550. //
  1551. // Input, string TITLE, a title.
  1552. //
  1553. {
  1554. int i;
  1555. double mag;
  1556. int n2;
  1557. char plus_minus;
  1558. cout << "\n";
  1559. cout << title << "\n";
  1560. cout << "\n";
  1561. if (n <= 0) {
  1562. cout << " p(x) = 0\n";
  1563. return;
  1564. }
  1565. if (a[n] < 0.0) {
  1566. plus_minus = '-';
  1567. } else {
  1568. plus_minus = ' ';
  1569. }
  1570. mag = fabs(a[n]);
  1571. if (2 <= n) {
  1572. cout << " p(x) = " << plus_minus << setw(14) << mag << " * x ^ " << n
  1573. << "\n";
  1574. } else if (n == 1) {
  1575. cout << " p(x) = " << plus_minus << setw(14) << mag << " * x\n";
  1576. } else if (n == 0) {
  1577. cout << " p(x) = " << plus_minus << setw(14) << mag << "\n";
  1578. }
  1579. for (i = n - 1; 0 <= i; i--) {
  1580. if (a[i] < 0.0) {
  1581. plus_minus = '-';
  1582. } else {
  1583. plus_minus = '+';
  1584. }
  1585. mag = fabs(a[i]);
  1586. if (mag != 0.0) {
  1587. if (2 <= i) {
  1588. cout << " " << plus_minus << setw(14) << mag << " * x ^ " << i
  1589. << "\n";
  1590. } else if (i == 1) {
  1591. cout << " " << plus_minus << setw(14) << mag << " * x\n";
  1592. } else if (i == 0) {
  1593. cout << " " << plus_minus << setw(14) << mag << "\n";
  1594. }
  1595. }
  1596. }
  1597. return;
  1598. }
  1599. //****************************************************************************80
  1600. double r8poly_value_horner(int m, double c[], double x)
  1601. //****************************************************************************80
  1602. //
  1603. // Purpose:
  1604. //
  1605. // R8POLY_VALUE_HORNER evaluates a polynomial using Horner's method.
  1606. //
  1607. // Discussion:
  1608. //
  1609. // The polynomial
  1610. //
  1611. // p(x) = c0 + c1 * x + c2 * x^2 + ... + cm * x^m
  1612. //
  1613. // is to be evaluated at the value X.
  1614. //
  1615. // Licensing:
  1616. //
  1617. // This code is distributed under the GNU LGPL license.
  1618. //
  1619. // Modified:
  1620. //
  1621. // 02 January 2015
  1622. //
  1623. // Author:
  1624. //
  1625. // John Burkardt
  1626. //
  1627. // Parameters:
  1628. //
  1629. // Input, int M, the degree of the polynomial.
  1630. //
  1631. // Input, double C[M+1], the coefficients of the polynomial.
  1632. // A[0] is the constant term.
  1633. //
  1634. // Input, double X, the point at which the polynomial is to be evaluated.
  1635. //
  1636. // Output, double R8POLY_VALUE_HORNER, the value of the polynomial at X.
  1637. //
  1638. {
  1639. int i;
  1640. double value;
  1641. value = c[m];
  1642. for (i = m - 1; 0 <= i; i--) {
  1643. value = value * x + c[i];
  1644. }
  1645. return value;
  1646. }
  1647. //****************************************************************************80
  1648. double *r8vec_linspace_new(int n, double a_first, double a_last)
  1649. //****************************************************************************80
  1650. //
  1651. // Purpose:
  1652. //
  1653. // R8VEC_LINSPACE_NEW creates a vector of linearly spaced values.
  1654. //
  1655. // Discussion:
  1656. //
  1657. // An R8VEC is a vector of R8's.
  1658. //
  1659. // 4 points evenly spaced between 0 and 12 will yield 0, 4, 8, 12.
  1660. //
  1661. // In other words, the interval is divided into N-1 even subintervals,
  1662. // and the endpoints of intervals are used as the points.
  1663. //
  1664. // Licensing:
  1665. //
  1666. // This code is distributed under the GNU LGPL license.
  1667. //
  1668. // Modified:
  1669. //
  1670. // 29 March 2011
  1671. //
  1672. // Author:
  1673. //
  1674. // John Burkardt
  1675. //
  1676. // Parameters:
  1677. //
  1678. // Input, int N, the number of entries in the vector.
  1679. //
  1680. // Input, double A_FIRST, A_LAST, the first and last entries.
  1681. //
  1682. // Output, double R8VEC_LINSPACE_NEW[N], a vector of linearly spaced data.
  1683. //
  1684. {
  1685. double *a;
  1686. int i;
  1687. a = new double[n];
  1688. if (n == 1) {
  1689. a[0] = (a_first + a_last) / 2.0;
  1690. } else {
  1691. for (i = 0; i < n; i++) {
  1692. a[i] = ((double)(n - 1 - i) * a_first + (double)(i)*a_last) /
  1693. (double)(n - 1);
  1694. }
  1695. }
  1696. return a;
  1697. }
  1698. //****************************************************************************80
  1699. double r8vec_max(int n, double *dvec)
  1700. //****************************************************************************80
  1701. //
  1702. // Purpose:
  1703. //
  1704. // R8VEC_MAX returns the value of the maximum element in an R8VEC.
  1705. //
  1706. // Licensing:
  1707. //
  1708. // This code is distributed under the GNU LGPL license.
  1709. //
  1710. // Modified:
  1711. //
  1712. // 15 October 1998
  1713. //
  1714. // Author:
  1715. //
  1716. // John Burkardt
  1717. //
  1718. // Parameters:
  1719. //
  1720. // Input, int N, the number of entries in the array.
  1721. //
  1722. // Input, double *RVEC, a pointer to the first entry of the array.
  1723. //
  1724. // Output, double R8VEC_MAX, the value of the maximum element. This
  1725. // is set to 0.0 if N <= 0.
  1726. //
  1727. {
  1728. int i;
  1729. double rmax;
  1730. double *r8vec_pointer;
  1731. if (n <= 0) {
  1732. return 0.0;
  1733. }
  1734. for (i = 0; i < n; i++) {
  1735. if (i == 0) {
  1736. rmax = *dvec;
  1737. r8vec_pointer = dvec;
  1738. } else {
  1739. r8vec_pointer++;
  1740. if (rmax < *r8vec_pointer) {
  1741. rmax = *r8vec_pointer;
  1742. }
  1743. }
  1744. }
  1745. return rmax;
  1746. }
  1747. //****************************************************************************80
  1748. double r8vec_mean(int n, double x[])
  1749. //****************************************************************************80
  1750. //
  1751. // Purpose:
  1752. //
  1753. // R8VEC_MEAN returns the mean of an R8VEC.
  1754. //
  1755. // Licensing:
  1756. //
  1757. // This code is distributed under the GNU LGPL license.
  1758. //
  1759. // Modified:
  1760. //
  1761. // 01 May 1999
  1762. //
  1763. // Author:
  1764. //
  1765. // John Burkardt
  1766. //
  1767. // Parameters:
  1768. //
  1769. // Input, int N, the number of entries in the vector.
  1770. //
  1771. // Input, double X[N], the vector whose mean is desired.
  1772. //
  1773. // Output, double R8VEC_MEAN, the mean, or average, of the vector entries.
  1774. //
  1775. {
  1776. int i;
  1777. double mean;
  1778. mean = 0.0;
  1779. for (i = 0; i < n; i++) {
  1780. mean = mean + x[i];
  1781. }
  1782. mean = mean / (double)n;
  1783. return mean;
  1784. }
  1785. //****************************************************************************80
  1786. double r8vec_min(int n, double *dvec)
  1787. //****************************************************************************80
  1788. //
  1789. // Purpose:
  1790. //
  1791. // R8VEC_MIN returns the value of the minimum element in an R8VEC.
  1792. //
  1793. // Licensing:
  1794. //
  1795. // This code is distributed under the GNU LGPL license.
  1796. //
  1797. // Modified:
  1798. //
  1799. // 15 October 1998
  1800. //
  1801. // Author:
  1802. //
  1803. // John Burkardt
  1804. //
  1805. // Parameters:
  1806. //
  1807. // Input, int N, the number of entries in the array.
  1808. //
  1809. // Input, double *RVEC, a pointer to the first entry of the array.
  1810. //
  1811. // Output, double R8VEC_MIN, the value of the minimum element. This
  1812. // is set to 0.0 if N <= 0.
  1813. //
  1814. {
  1815. int i;
  1816. double rmin;
  1817. double *r8vec_pointer;
  1818. if (n <= 0) {
  1819. return 0.0;
  1820. }
  1821. for (i = 0; i < n; i++) {
  1822. if (i == 0) {
  1823. rmin = *dvec;
  1824. r8vec_pointer = dvec;
  1825. } else {
  1826. r8vec_pointer++;
  1827. if (*r8vec_pointer < rmin) {
  1828. rmin = *r8vec_pointer;
  1829. }
  1830. }
  1831. }
  1832. return rmin;
  1833. }
  1834. //****************************************************************************80
  1835. void r8vec_print(int n, double a[], string title)
  1836. //****************************************************************************80
  1837. //
  1838. // Purpose:
  1839. //
  1840. // R8VEC_PRINT prints an R8VEC.
  1841. //
  1842. // Discussion:
  1843. //
  1844. // An R8VEC is a vector of R8's.
  1845. //
  1846. // Licensing:
  1847. //
  1848. // This code is distributed under the GNU LGPL license.
  1849. //
  1850. // Modified:
  1851. //
  1852. // 16 August 2004
  1853. //
  1854. // Author:
  1855. //
  1856. // John Burkardt
  1857. //
  1858. // Parameters:
  1859. //
  1860. // Input, int N, the number of components of the vector.
  1861. //
  1862. // Input, double A[N], the vector to be printed.
  1863. //
  1864. // Input, string TITLE, a title.
  1865. //
  1866. {
  1867. int i;
  1868. cout << "\n";
  1869. cout << title << "\n";
  1870. cout << "\n";
  1871. for (i = 0; i < n; i++) {
  1872. cout << " " << setw(8) << i << ": " << setw(14) << a[i] << "\n";
  1873. }
  1874. return;
  1875. }
  1876. //****************************************************************************80
  1877. double r8vec_variance(int n, double x[])
  1878. //****************************************************************************80
  1879. //
  1880. // Purpose:
  1881. //
  1882. // R8VEC_VARIANCE returns the variance of an R8VEC.
  1883. //
  1884. // Licensing:
  1885. //
  1886. // This code is distributed under the GNU LGPL license.
  1887. //
  1888. // Modified:
  1889. //
  1890. // 01 May 1999
  1891. //
  1892. // Author:
  1893. //
  1894. // John Burkardt
  1895. //
  1896. // Parameters:
  1897. //
  1898. // Input, int N, the number of entries in the vector.
  1899. //
  1900. // Input, double X[N], the vector whose variance is desired.
  1901. //
  1902. // Output, double R8VEC_VARIANCE, the variance of the vector entries.
  1903. //
  1904. {
  1905. int i;
  1906. double mean;
  1907. double variance;
  1908. mean = r8vec_mean(n, x);
  1909. variance = 0.0;
  1910. for (i = 0; i < n; i++) {
  1911. variance = variance + (x[i] - mean) * (x[i] - mean);
  1912. }
  1913. if (1 < n) {
  1914. variance = variance / (double)(n - 1);
  1915. } else {
  1916. variance = 0.0;
  1917. }
  1918. return variance;
  1919. }
  1920. //****************************************************************************80
  1921. void timestamp()
  1922. //****************************************************************************80
  1923. //
  1924. // Purpose:
  1925. //
  1926. // TIMESTAMP prints the current YMDHMS date as a time stamp.
  1927. //
  1928. // Example:
  1929. //
  1930. // 31 May 2001 09:45:54 AM
  1931. //
  1932. // Licensing:
  1933. //
  1934. // This code is distributed under the GNU LGPL license.
  1935. //
  1936. // Modified:
  1937. //
  1938. // 08 July 2009
  1939. //
  1940. // Author:
  1941. //
  1942. // John Burkardt
  1943. //
  1944. // Parameters:
  1945. //
  1946. // None
  1947. //
  1948. {
  1949. #define TIME_SIZE 40
  1950. static char time_buffer[TIME_SIZE];
  1951. const struct std::tm *tm_ptr;
  1952. size_t len;
  1953. std::time_t now;
  1954. now = std::time(NULL);
  1955. tm_ptr = std::localtime(&now);
  1956. len = std::strftime(time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr);
  1957. std::cout << time_buffer << "\n";
  1958. return;
  1959. #undef TIME_SIZE
  1960. }
  1961. //****************************************************************************80
  1962. double truncated_normal_ab_cdf(double x, double mu, double sigma, double a,
  1963. double b)
  1964. //****************************************************************************80
  1965. //
  1966. // Purpose:
  1967. //
  1968. // TRUNCATED_NORMAL_AB_CDF evaluates the truncated Normal CDF.
  1969. //
  1970. // Licensing:
  1971. //
  1972. // This code is distributed under the GNU LGPL license.
  1973. //
  1974. // Modified:
  1975. //
  1976. // 14 August 2013
  1977. //
  1978. // Author:
  1979. //
  1980. // John Burkardt
  1981. //
  1982. // Parameters:
  1983. //
  1984. // Input, double X, the argument of the CDF.
  1985. //
  1986. // Input, double MU, SIGMA, the mean and standard deviation of the
  1987. // parent Normal distribution.
  1988. //
  1989. // Input, double A, B, the lower and upper truncation limits.
  1990. //
  1991. // Output, double TRUNCATED_NORMAL_AB_CDF, the value of the CDF.
  1992. //
  1993. {
  1994. double alpha;
  1995. double alpha_cdf;
  1996. double beta;
  1997. double beta_cdf;
  1998. double cdf;
  1999. double xi;
  2000. double xi_cdf;
  2001. alpha = (a - mu) / sigma;
  2002. beta = (b - mu) / sigma;
  2003. xi = (x - mu) / sigma;
  2004. alpha_cdf = normal_01_cdf(alpha);
  2005. beta_cdf = normal_01_cdf(beta);
  2006. xi_cdf = normal_01_cdf(xi);
  2007. cdf = (xi_cdf - alpha_cdf) / (beta_cdf - alpha_cdf);
  2008. return cdf;
  2009. }
  2010. //****************************************************************************80
  2011. void truncated_normal_ab_cdf_values(int &n_data, double &mu, double &sigma,
  2012. double &a, double &b, double &x, double &fx)
  2013. //****************************************************************************80
  2014. //
  2015. // Purpose:
  2016. //
  2017. // TRUNCATED_NORMAL_AB_CDF_VALUES: values of the Truncated Normal CDF.
  2018. //
  2019. // Discussion:
  2020. //
  2021. // The Normal distribution, with mean Mu and standard deviation Sigma,
  2022. // is truncated to the interval [A,B].
  2023. //
  2024. // Licensing:
  2025. //
  2026. // This code is distributed under the GNU LGPL license.
  2027. //
  2028. // Modified:
  2029. //
  2030. // 13 September 2013
  2031. //
  2032. // Author:
  2033. //
  2034. // John Burkardt
  2035. //
  2036. // Reference:
  2037. //
  2038. // Stephen Wolfram,
  2039. // The Mathematica Book,
  2040. // Fourth Edition,
  2041. // Cambridge University Press, 1999,
  2042. // ISBN: 0-521-64314-7,
  2043. // LC: QA76.95.W65.
  2044. //
  2045. // Parameters:
  2046. //
  2047. // Input/output, int &N_DATA. The user sets N_DATA to 0 before the
  2048. // first call. On each call, the routine increments N_DATA by 1, and
  2049. // returns the corresponding data; when there is no more data, the
  2050. // output value of N_DATA will be 0 again.
  2051. //
  2052. // Output, double &MU, the mean of the distribution.
  2053. //
  2054. // Output, double &SIGMA, the standard deviation of the distribution.
  2055. //
  2056. // Output, double &A, &B, the lower and upper truncation limits.
  2057. //
  2058. // Output, double &X, the argument of the function.
  2059. //
  2060. // Output, double &FX, the value of the function.
  2061. //
  2062. {
  2063. #define N_MAX 11
  2064. static double a_vec[N_MAX] = {50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
  2065. 50.0, 50.0, 50.0, 50.0, 50.0};
  2066. static double b_vec[N_MAX] = {150.0, 150.0, 150.0, 150.0, 150.0, 150.0,
  2067. 150.0, 150.0, 150.0, 150.0, 150.0};
  2068. static double fx_vec[N_MAX] = {
  2069. 0.3371694242213513, 0.3685009225506048, 0.4006444233448185,
  2070. 0.4334107066903040, 0.4665988676496338, 0.5000000000000000,
  2071. 0.5334011323503662, 0.5665892933096960, 0.5993555766551815,
  2072. 0.6314990774493952, 0.6628305757786487};
  2073. static double mu_vec[N_MAX] = {100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
  2074. 100.0, 100.0, 100.0, 100.0, 100.0};
  2075. static double sigma_vec[N_MAX] = {25.0, 25.0, 25.0, 25.0, 25.0, 25.0,
  2076. 25.0, 25.0, 25.0, 25.0, 25.0};
  2077. static double x_vec[N_MAX] = {90.0, 92.0, 94.0, 96.0, 98.0, 100.0,
  2078. 102.0, 104.0, 106.0, 108.0, 110.0};
  2079. if (n_data < 0) {
  2080. n_data = 0;
  2081. }
  2082. n_data = n_data + 1;
  2083. if (N_MAX < n_data) {
  2084. n_data = 0;
  2085. a = 0.0;
  2086. b = 0.0;
  2087. mu = 0.0;
  2088. sigma = 0.0;
  2089. x = 0.0;
  2090. fx = 0.0;
  2091. } else {
  2092. a = a_vec[n_data - 1];
  2093. b = b_vec[n_data - 1];
  2094. mu = mu_vec[n_data - 1];
  2095. sigma = sigma_vec[n_data - 1];
  2096. x = x_vec[n_data - 1];
  2097. fx = fx_vec[n_data - 1];
  2098. }
  2099. return;
  2100. #undef N_MAX
  2101. }
  2102. //****************************************************************************80
  2103. double truncated_normal_ab_cdf_inv(double cdf, double mu, double sigma,
  2104. double a, double b)
  2105. //****************************************************************************80
  2106. //
  2107. // Purpose:
  2108. //
  2109. // TRUNCATED_NORMAL_AB_CDF_INV inverts the truncated Normal CDF.
  2110. //
  2111. // Licensing:
  2112. //
  2113. // This code is distributed under the GNU LGPL license.
  2114. //
  2115. // Modified:
  2116. //
  2117. // 14 August 2013
  2118. //
  2119. // Author:
  2120. //
  2121. // John Burkardt
  2122. //
  2123. // Parameters:
  2124. //
  2125. // Input, double CDF, the value of the CDF.
  2126. // 0.0 <= CDF <= 1.0.
  2127. //
  2128. // Input, double MU, SIGMA, the mean and standard deviation of the
  2129. // parent Normal distribution.
  2130. //
  2131. // Input, double A, B, the lower and upper truncation limits.
  2132. //
  2133. // Output, double TRUNCATED_NORMAL_AB_CDF_INV, the corresponding argument.
  2134. //
  2135. {
  2136. double alpha;
  2137. double alpha_cdf;
  2138. double beta;
  2139. double beta_cdf;
  2140. double x;
  2141. double xi;
  2142. double xi_cdf;
  2143. if (cdf < 0.0 || 1.0 < cdf) {
  2144. cerr << "\n";
  2145. cerr << "TRUNCATED_NORMAL_AB_CDF_INV - Fatal error!\n";
  2146. cerr << " CDF < 0 or 1 < CDF.\n";
  2147. exit(1);
  2148. }
  2149. alpha = (a - mu) / sigma;
  2150. beta = (b - mu) / sigma;
  2151. alpha_cdf = normal_01_cdf(alpha);
  2152. beta_cdf = normal_01_cdf(beta);
  2153. xi_cdf = (beta_cdf - alpha_cdf) * cdf + alpha_cdf;
  2154. xi = normal_01_cdf_inv(xi_cdf);
  2155. x = mu + sigma * xi;
  2156. return x;
  2157. }
  2158. //****************************************************************************80
  2159. double truncated_normal_ab_mean(double mu, double sigma, double a, double b)
  2160. //****************************************************************************80
  2161. //
  2162. // Purpose:
  2163. //
  2164. // TRUNCATED_NORMAL_AB_MEAN returns the mean of the truncated Normal PDF.
  2165. //
  2166. // Licensing:
  2167. //
  2168. // This code is distributed under the GNU LGPL license.
  2169. //
  2170. // Modified:
  2171. //
  2172. // 14 August 2013
  2173. //
  2174. // Author:
  2175. //
  2176. // John Burkardt
  2177. //
  2178. // Parameters:
  2179. //
  2180. // Input, double MU, SIGMA, the mean and standard deviation of the
  2181. // parent Normal distribution.
  2182. //
  2183. // Input, double A, B, the lower and upper truncation limits.
  2184. //
  2185. // Output, double TRUNCATED_NORMAL_AB_MEAN, the mean of the PDF.
  2186. //
  2187. {
  2188. double alpha;
  2189. double alpha_cdf;
  2190. double alpha_pdf;
  2191. double beta;
  2192. double beta_cdf;
  2193. double beta_pdf;
  2194. double mean;
  2195. alpha = (a - mu) / sigma;
  2196. beta = (b - mu) / sigma;
  2197. alpha_cdf = normal_01_cdf(alpha);
  2198. beta_cdf = normal_01_cdf(beta);
  2199. alpha_pdf = normal_01_pdf(alpha);
  2200. beta_pdf = normal_01_pdf(beta);
  2201. mean = mu + sigma * (alpha_pdf - beta_pdf) / (beta_cdf - alpha_cdf);
  2202. return mean;
  2203. }
  2204. //****************************************************************************80
  2205. double truncated_normal_ab_moment(int order, double mu, double sigma, double a,
  2206. double b)
  2207. //****************************************************************************80
  2208. //
  2209. // Purpose:
  2210. //
  2211. // TRUNCATED_NORMAL_AB_MOMENT: moments of the truncated Normal PDF.
  2212. //
  2213. // Licensing:
  2214. //
  2215. // This code is distributed under the GNU LGPL license.
  2216. //
  2217. // Modified:
  2218. //
  2219. // 11 September 2013
  2220. //
  2221. // Author:
  2222. //
  2223. // John Burkardt
  2224. //
  2225. // Reference:
  2226. //
  2227. // Phoebus Dhrymes,
  2228. // Moments of Truncated Normal Distributions,
  2229. // May 2005.
  2230. //
  2231. // Parameters:
  2232. //
  2233. // Input, int ORDER, the order of the moment.
  2234. // 0 <= ORDER.
  2235. //
  2236. // Input, double MU, SIGMA, the mean and standard deviation of the
  2237. // parent Normal distribution.
  2238. // 0.0 < S.
  2239. //
  2240. // Input, double A, B, the lower and upper truncation limits.
  2241. // A < B.
  2242. //
  2243. // Output, double TRUNCATED_NORMAL_AB_MOMENT, the moment of the PDF.
  2244. //
  2245. {
  2246. double a_cdf;
  2247. double a_h;
  2248. double a_pdf;
  2249. double b_cdf;
  2250. double b_h;
  2251. double b_pdf;
  2252. double ir;
  2253. double irm1;
  2254. double irm2;
  2255. double moment;
  2256. int r;
  2257. if (order < 0) {
  2258. cerr << "\n";
  2259. cerr << "TRUNCATED_NORMAL_AB_MOMENT - Fatal error!\n";
  2260. cerr << " ORDER < 0.\n";
  2261. exit(1);
  2262. }
  2263. if (sigma <= 0.0) {
  2264. cerr << "\n";
  2265. cerr << "TRUNCATED_NORMAL_AB_MOMENT - Fatal error!\n";
  2266. cerr << " SIGMA <= 0.0.\n";
  2267. exit(1);
  2268. }
  2269. if (b <= a) {
  2270. cerr << "\n";
  2271. cerr << "TRUNCATED_NORMAL_AB_MOMENT - Fatal error!\n";
  2272. cerr << " B <= A.\n";
  2273. exit(1);
  2274. }
  2275. a_h = (a - mu) / sigma;
  2276. a_pdf = normal_01_pdf(a_h);
  2277. a_cdf = normal_01_cdf(a_h);
  2278. if (a_cdf == 0.0) {
  2279. cerr << "\n";
  2280. cerr << "TRUNCATED_NORMAL_AB_MOMENT - Fatal error!\n";
  2281. cerr << " PDF/CDF ratio fails, because A_CDF too small.\n";
  2282. cerr << " A_PDF = " << a_pdf << "\n";
  2283. cerr << " A_CDF = " << a_cdf << "\n";
  2284. exit(1);
  2285. }
  2286. b_h = (b - mu) / sigma;
  2287. b_pdf = normal_01_pdf(b_h);
  2288. b_cdf = normal_01_cdf(b_h);
  2289. if (b_cdf == 0.0) {
  2290. cerr << "\n";
  2291. cerr << "TRUNCATED_NORMAL_AB_MOMENT - Fatal error!\n";
  2292. cerr << " PDF/CDF ratio fails, because B_CDF too small.\n";
  2293. cerr << " B_PDF = " << b_pdf << "\n";
  2294. cerr << " B_CDF = " << b_cdf << "\n";
  2295. exit(1);
  2296. }
  2297. moment = 0.0;
  2298. irm2 = 0.0;
  2299. irm1 = 0.0;
  2300. for (r = 0; r <= order; r++) {
  2301. if (r == 0) {
  2302. ir = 1.0;
  2303. } else if (r == 1) {
  2304. ir = -(b_pdf - a_pdf) / (b_cdf - a_cdf);
  2305. } else {
  2306. ir =
  2307. (double)(r - 1) * irm2 -
  2308. (pow(b_h, r - 1) * b_pdf - pow(a_h, r - 1) * a_pdf) / (b_cdf - a_cdf);
  2309. }
  2310. moment =
  2311. moment + r8_choose(order, r) * pow(mu, order - r) * pow(sigma, r) * ir;
  2312. irm2 = irm1;
  2313. irm1 = ir;
  2314. }
  2315. return moment;
  2316. }
  2317. //****************************************************************************80
  2318. double truncated_normal_ab_pdf(double x, double mu, double sigma, double a,
  2319. double b)
  2320. //****************************************************************************80
  2321. //
  2322. // Purpose:
  2323. //
  2324. // TRUNCATED_NORMAL_AB_PDF evaluates the truncated Normal PDF.
  2325. //
  2326. // Licensing:
  2327. //
  2328. // This code is distributed under the GNU LGPL license.
  2329. //
  2330. // Modified:
  2331. //
  2332. // 14 August 2013
  2333. //
  2334. // Author:
  2335. //
  2336. // John Burkardt
  2337. //
  2338. // Parameters:
  2339. //
  2340. // Input, double X, the argument of the PDF.
  2341. //
  2342. // Input, double MU, SIGMA, the mean and standard deviation of the
  2343. // parent Normal distribution.
  2344. //
  2345. // Input, double A, B, the lower and upper truncation limits.
  2346. //
  2347. // Output, double TRUNCATED_NORMAL_AB_PDF, the value of the PDF.
  2348. //
  2349. {
  2350. double alpha;
  2351. double alpha_cdf;
  2352. double beta;
  2353. double beta_cdf;
  2354. double pdf;
  2355. double xi;
  2356. double xi_pdf;
  2357. alpha = (a - mu) / sigma;
  2358. beta = (b - mu) / sigma;
  2359. xi = (x - mu) / sigma;
  2360. alpha_cdf = normal_01_cdf(alpha);
  2361. beta_cdf = normal_01_cdf(beta);
  2362. xi_pdf = normal_01_pdf(xi);
  2363. pdf = xi_pdf / (beta_cdf - alpha_cdf) / sigma;
  2364. return pdf;
  2365. }
  2366. //****************************************************************************80
  2367. void truncated_normal_ab_pdf_values(int &n_data, double &mu, double &sigma,
  2368. double &a, double &b, double &x, double &fx)
  2369. //****************************************************************************80
  2370. //
  2371. // Purpose:
  2372. //
  2373. // TRUNCATED_NORMAL_AB_PDF_VALUES: values of the Truncated Normal PDF.
  2374. //
  2375. // Discussion:
  2376. //
  2377. // The Normal distribution, with mean Mu and standard deviation Sigma,
  2378. // is truncated to the interval [A,B].
  2379. //
  2380. // Licensing:
  2381. //
  2382. // This code is distributed under the GNU LGPL license.
  2383. //
  2384. // Modified:
  2385. //
  2386. // 13 September 2013
  2387. //
  2388. // Author:
  2389. //
  2390. // John Burkardt
  2391. //
  2392. // Reference:
  2393. //
  2394. // Stephen Wolfram,
  2395. // The Mathematica Book,
  2396. // Fourth Edition,
  2397. // Cambridge University Press, 1999,
  2398. // ISBN: 0-521-64314-7,
  2399. // LC: QA76.95.W65.
  2400. //
  2401. // Parameters:
  2402. //
  2403. // Input/output, int &N_DATA. The user sets N_DATA to 0 before the
  2404. // first call. On each call, the routine increments N_DATA by 1, and
  2405. // returns the corresponding data; when there is no more data, the
  2406. // output value of N_DATA will be 0 again.
  2407. //
  2408. // Output, double &MU, the mean of the distribution.
  2409. //
  2410. // Output, double &SIGMA, the standard deviation of the distribution.
  2411. //
  2412. // Output, double &A, &B, the lower and upper truncation limits.
  2413. //
  2414. // Output, double &X, the argument of the function.
  2415. //
  2416. // Output, double &FX, the value of the function.
  2417. //
  2418. {
  2419. #define N_MAX 11
  2420. static double a_vec[N_MAX] = {50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
  2421. 50.0, 50.0, 50.0, 50.0, 50.0};
  2422. static double b_vec[N_MAX] = {150.0, 150.0, 150.0, 150.0, 150.0, 150.0,
  2423. 150.0, 150.0, 150.0, 150.0, 150.0};
  2424. static double fx_vec[N_MAX] = {
  2425. 0.01543301171801836, 0.01588394472270638, 0.01624375997031919,
  2426. 0.01650575046469259, 0.01666496869385951, 0.01671838200940538,
  2427. 0.01666496869385951, 0.01650575046469259, 0.01624375997031919,
  2428. 0.01588394472270638, 0.01543301171801836};
  2429. static double mu_vec[N_MAX] = {100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
  2430. 100.0, 100.0, 100.0, 100.0, 100.0};
  2431. static double sigma_vec[N_MAX] = {25.0, 25.0, 25.0, 25.0, 25.0, 25.0,
  2432. 25.0, 25.0, 25.0, 25.0, 25.0};
  2433. static double x_vec[N_MAX] = {90.0, 92.0, 94.0, 96.0, 98.0, 100.0,
  2434. 102.0, 104.0, 106.0, 108.0, 110.0};
  2435. if (n_data < 0) {
  2436. n_data = 0;
  2437. }
  2438. n_data = n_data + 1;
  2439. if (N_MAX < n_data) {
  2440. n_data = 0;
  2441. a = 0.0;
  2442. b = 0.0;
  2443. mu = 0.0;
  2444. sigma = 0.0;
  2445. x = 0.0;
  2446. fx = 0.0;
  2447. } else {
  2448. a = a_vec[n_data - 1];
  2449. b = b_vec[n_data - 1];
  2450. mu = mu_vec[n_data - 1];
  2451. sigma = sigma_vec[n_data - 1];
  2452. x = x_vec[n_data - 1];
  2453. fx = fx_vec[n_data - 1];
  2454. }
  2455. return;
  2456. #undef N_MAX
  2457. }
  2458. //****************************************************************************80
  2459. double truncated_normal_ab_sample(double mu, double sigma, double a, double b,
  2460. int &seed)
  2461. //****************************************************************************80
  2462. //
  2463. // Purpose:
  2464. //
  2465. // TRUNCATED_NORMAL_AB_SAMPLE samples the truncated Normal PDF.
  2466. //
  2467. // Licensing:
  2468. //
  2469. // This code is distributed under the GNU LGPL license.
  2470. //
  2471. // Modified:
  2472. //
  2473. // 14 August 2013
  2474. //
  2475. // Author:
  2476. //
  2477. // John Burkardt
  2478. //
  2479. // Parameters:
  2480. //
  2481. // Input, double MU, SIGMA, the mean and standard deviation of the
  2482. // parent Normal distribution.
  2483. //
  2484. // Input, double A, B, the lower and upper truncation limits.
  2485. //
  2486. // Input/output, int &SEED, a seed for the random number
  2487. // generator.
  2488. //
  2489. // Output, double TRUNCATED_NORMAL_AB_SAMPLE, a sample of the PDF.
  2490. //
  2491. {
  2492. double alpha;
  2493. double alpha_cdf;
  2494. double beta;
  2495. double beta_cdf;
  2496. double u;
  2497. double x;
  2498. double xi;
  2499. double xi_cdf;
  2500. alpha = (a - mu) / sigma;
  2501. beta = (b - mu) / sigma;
  2502. alpha_cdf = normal_01_cdf(alpha);
  2503. beta_cdf = normal_01_cdf(beta);
  2504. u = r8_uniform_01(seed);
  2505. xi_cdf = alpha_cdf + u * (beta_cdf - alpha_cdf);
  2506. xi = normal_01_cdf_inv(xi_cdf);
  2507. x = mu + sigma * xi;
  2508. return x;
  2509. }
  2510. //****************************************************************************80
  2511. double truncated_normal_ab_variance(double mu, double sigma, double a, double b)
  2512. //****************************************************************************80
  2513. //
  2514. // Purpose:
  2515. //
  2516. // TRUNCATED_NORMAL_AB_VARIANCE returns the variance of the truncated Normal
  2517. // PDF.
  2518. //
  2519. // Licensing:
  2520. //
  2521. // This code is distributed under the GNU LGPL license.
  2522. //
  2523. // Modified:
  2524. //
  2525. // 14 August 2013
  2526. //
  2527. // Author:
  2528. //
  2529. // John Burkardt
  2530. //
  2531. // Parameters:
  2532. //
  2533. // Input, double MU, SIGMA, the mean and standard deviation of the
  2534. // parent Normal distribution.
  2535. //
  2536. // Input, double A, B, the lower and upper truncation limits.
  2537. //
  2538. // Output, double TRUNCATED_NORMAL_AB_VARIANCE, the variance of the PDF.
  2539. //
  2540. {
  2541. double alpha;
  2542. double alpha_cdf;
  2543. double alpha_pdf;
  2544. double beta;
  2545. double beta_cdf;
  2546. double beta_pdf;
  2547. double variance;
  2548. alpha = (a - mu) / sigma;
  2549. beta = (b - mu) / sigma;
  2550. alpha_pdf = normal_01_pdf(alpha);
  2551. beta_pdf = normal_01_pdf(beta);
  2552. alpha_cdf = normal_01_cdf(alpha);
  2553. beta_cdf = normal_01_cdf(beta);
  2554. variance =
  2555. sigma * sigma *
  2556. (1.0 + (alpha * alpha_pdf - beta * beta_pdf) / (beta_cdf - alpha_cdf) -
  2557. pow((alpha_pdf - beta_pdf) / (beta_cdf - alpha_cdf), 2));
  2558. return variance;
  2559. }
  2560. //****************************************************************************80
  2561. double truncated_normal_a_cdf(double x, double mu, double sigma, double a)
  2562. //****************************************************************************80
  2563. //
  2564. // Purpose:
  2565. //
  2566. // TRUNCATED_NORMAL_A_CDF evaluates the lower truncated Normal CDF.
  2567. //
  2568. // Licensing:
  2569. //
  2570. // This code is distributed under the GNU LGPL license.
  2571. //
  2572. // Modified:
  2573. //
  2574. // 21 August 2013
  2575. //
  2576. // Author:
  2577. //
  2578. // John Burkardt
  2579. //
  2580. // Parameters:
  2581. //
  2582. // Input, double X, the argument of the CDF.
  2583. //
  2584. // Input, double MU, SIGMA, the mean and standard deviation of the
  2585. // parent Normal distribution.
  2586. //
  2587. // Input, double A, the lower truncation limit.
  2588. //
  2589. // Output, double TRUNCATED_NORMAL_A_CDF, the value of the CDF.
  2590. //
  2591. {
  2592. double alpha;
  2593. double alpha_cdf;
  2594. double cdf;
  2595. double xi;
  2596. double xi_cdf;
  2597. alpha = (a - mu) / sigma;
  2598. xi = (x - mu) / sigma;
  2599. alpha_cdf = normal_01_cdf(alpha);
  2600. xi_cdf = normal_01_cdf(xi);
  2601. cdf = (xi_cdf - alpha_cdf) / (1.0 - alpha_cdf);
  2602. return cdf;
  2603. }
  2604. //****************************************************************************80
  2605. void truncated_normal_a_cdf_values(int &n_data, double &mu, double &sigma,
  2606. double &a, double &x, double &fx)
  2607. //****************************************************************************80
  2608. //
  2609. // Purpose:
  2610. //
  2611. // TRUNCATED_NORMAL_A_CDF_VALUES: values of the Lower Truncated Normal CDF.
  2612. //
  2613. // Discussion:
  2614. //
  2615. // The Normal distribution, with mean Mu and standard deviation Sigma,
  2616. // is truncated to the interval [A,+oo).
  2617. //
  2618. // Licensing:
  2619. //
  2620. // This code is distributed under the GNU LGPL license.
  2621. //
  2622. // Modified:
  2623. //
  2624. // 14 September 2013
  2625. //
  2626. // Author:
  2627. //
  2628. // John Burkardt
  2629. //
  2630. // Reference:
  2631. //
  2632. // Stephen Wolfram,
  2633. // The Mathematica Book,
  2634. // Fourth Edition,
  2635. // Cambridge University Press, 1999,
  2636. // ISBN: 0-521-64314-7,
  2637. // LC: QA76.95.W65.
  2638. //
  2639. // Parameters:
  2640. //
  2641. // Input/output, int &N_DATA. The user sets N_DATA to 0 before the
  2642. // first call. On each call, the routine increments N_DATA by 1, and
  2643. // returns the corresponding data; when there is no more data, the
  2644. // output value of N_DATA will be 0 again.
  2645. //
  2646. // Output, double &MU, the mean of the distribution.
  2647. //
  2648. // Output, double &SIGMA, the standard deviation of the distribution.
  2649. //
  2650. // Output, double &A, the lower truncation limit.
  2651. //
  2652. // Output, double &X, the argument of the function.
  2653. //
  2654. // Output, double &FX, the value of the function.
  2655. //
  2656. {
  2657. #define N_MAX 11
  2658. static double a_vec[N_MAX] = {50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
  2659. 50.0, 50.0, 50.0, 50.0, 50.0};
  2660. static double fx_vec[N_MAX] = {
  2661. 0.3293202045481688, 0.3599223134505957, 0.3913175216041539,
  2662. 0.4233210140873113, 0.4557365629792204, 0.4883601253415709,
  2663. 0.5209836877039214, 0.5533992365958304, 0.5854027290789878,
  2664. 0.6167979372325460, 0.6474000461349729};
  2665. static double mu_vec[N_MAX] = {100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
  2666. 100.0, 100.0, 100.0, 100.0, 100.0};
  2667. static double sigma_vec[N_MAX] = {25.0, 25.0, 25.0, 25.0, 25.0, 25.0,
  2668. 25.0, 25.0, 25.0, 25.0, 25.0};
  2669. static double x_vec[N_MAX] = {90.0, 92.0, 94.0, 96.0, 98.0, 100.0,
  2670. 102.0, 104.0, 106.0, 108.0, 110.0};
  2671. if (n_data < 0) {
  2672. n_data = 0;
  2673. }
  2674. n_data = n_data + 1;
  2675. if (N_MAX < n_data) {
  2676. n_data = 0;
  2677. a = 0.0;
  2678. mu = 0.0;
  2679. sigma = 0.0;
  2680. x = 0.0;
  2681. fx = 0.0;
  2682. } else {
  2683. a = a_vec[n_data - 1];
  2684. mu = mu_vec[n_data - 1];
  2685. sigma = sigma_vec[n_data - 1];
  2686. x = x_vec[n_data - 1];
  2687. fx = fx_vec[n_data - 1];
  2688. }
  2689. return;
  2690. #undef N_MAX
  2691. }
  2692. //****************************************************************************80
  2693. double truncated_normal_a_cdf_inv(double cdf, double mu, double sigma, double a)
  2694. //****************************************************************************80
  2695. //
  2696. // Purpose:
  2697. //
  2698. // TRUNCATED_NORMAL_A_CDF_INV inverts the lower truncated Normal CDF.
  2699. //
  2700. // Licensing:
  2701. //
  2702. // This code is distributed under the GNU LGPL license.
  2703. //
  2704. // Modified:
  2705. //
  2706. // 21 August 2013
  2707. //
  2708. // Author:
  2709. //
  2710. // John Burkardt
  2711. //
  2712. // Parameters:
  2713. //
  2714. // Input, double CDF, the value of the CDF.
  2715. // 0.0 <= CDF <= 1.0.
  2716. //
  2717. // Input, double MU, SIGMA, the mean and standard deviation of the
  2718. // parent Normal distribution.
  2719. //
  2720. // Input, double A, the lower truncation limit.
  2721. //
  2722. // Output, double TRUNCATED_NORMAL_A_CDF_INV, the corresponding argument.
  2723. //
  2724. {
  2725. double alpha;
  2726. double alpha_cdf;
  2727. double x;
  2728. double xi;
  2729. double xi_cdf;
  2730. if (cdf < 0.0 || 1.0 < cdf) {
  2731. cerr << "\n";
  2732. cerr << "TRUNCATED_NORMAL_A_CDF_INV - Fatal error!\n";
  2733. cerr << " CDF < 0 or 1 < CDF.\n";
  2734. exit(1);
  2735. }
  2736. alpha = (a - mu) / sigma;
  2737. alpha_cdf = normal_01_cdf(alpha);
  2738. xi_cdf = (1.0 - alpha_cdf) * cdf + alpha_cdf;
  2739. xi = normal_01_cdf_inv(xi_cdf);
  2740. x = mu + sigma * xi;
  2741. return x;
  2742. }
  2743. //****************************************************************************80
  2744. double truncated_normal_a_mean(double mu, double sigma, double a)
  2745. //****************************************************************************80
  2746. //
  2747. // Purpose:
  2748. //
  2749. // TRUNCATED_NORMAL_A_MEAN returns the mean of the lower truncated Normal
  2750. // PDF.
  2751. //
  2752. // Licensing:
  2753. //
  2754. // This code is distributed under the GNU LGPL license.
  2755. //
  2756. // Modified:
  2757. //
  2758. // 21 August 2013
  2759. //
  2760. // Author:
  2761. //
  2762. // John Burkardt
  2763. //
  2764. // Parameters:
  2765. //
  2766. // Input, double MU, SIGMA, the mean and standard deviation of the
  2767. // parent Normal distribution.
  2768. //
  2769. // Input, double A, the lower truncation limit.
  2770. //
  2771. // Output, double TRUNCATED_NORMAL_A_MEAN, the mean of the PDF.
  2772. //
  2773. {
  2774. double alpha;
  2775. double alpha_cdf;
  2776. double alpha_pdf;
  2777. double mean;
  2778. alpha = (a - mu) / sigma;
  2779. alpha_cdf = normal_01_cdf(alpha);
  2780. alpha_pdf = normal_01_pdf(alpha);
  2781. mean = mu + sigma * alpha_pdf / (1.0 - alpha_cdf);
  2782. return mean;
  2783. }
  2784. //****************************************************************************80
  2785. double truncated_normal_a_moment(int order, double mu, double sigma, double a)
  2786. //****************************************************************************80
  2787. //
  2788. // Purpose:
  2789. //
  2790. // TRUNCATED_NORMAL_A_MOMENT: moments of the lower truncated Normal PDF.
  2791. //
  2792. // Licensing:
  2793. //
  2794. // This code is distributed under the GNU LGPL license.
  2795. //
  2796. // Modified:
  2797. //
  2798. // 11 September 2013
  2799. //
  2800. // Author:
  2801. //
  2802. // John Burkardt
  2803. //
  2804. // Reference:
  2805. //
  2806. // Phoebus Dhrymes,
  2807. // Moments of Truncated Normal Distributions,
  2808. // May 2005.
  2809. //
  2810. // Parameters:
  2811. //
  2812. // Input, int ORDER, the order of the moment.
  2813. // 0 <= ORDER.
  2814. //
  2815. // Input, double MU, SIGMA, the mean and standard deviation of the
  2816. // parent Normal distribution.
  2817. //
  2818. // Input, double A, the lower truncation limit.
  2819. //
  2820. // Output, double TRUNCATED_NORMAL_A_MOMENT, the moment of the PDF.
  2821. //
  2822. {
  2823. double moment;
  2824. moment = r8_mop(order) * truncated_normal_b_moment(order, -mu, sigma, -a);
  2825. return moment;
  2826. }
  2827. //****************************************************************************80
  2828. double truncated_normal_a_pdf(double x, double mu, double sigma, double a)
  2829. //****************************************************************************80
  2830. //
  2831. // Purpose:
  2832. //
  2833. // TRUNCATED_NORMAL_A_PDF evaluates the lower truncated Normal PDF.
  2834. //
  2835. // Licensing:
  2836. //
  2837. // This code is distributed under the GNU LGPL license.
  2838. //
  2839. // Modified:
  2840. //
  2841. // 21 August 2013
  2842. //
  2843. // Author:
  2844. //
  2845. // John Burkardt
  2846. //
  2847. // Parameters:
  2848. //
  2849. // Input, double X, the argument of the PDF.
  2850. //
  2851. // Input, double MU, SIGMA, the mean and standard deviation of the
  2852. // parent Normal distribution.
  2853. //
  2854. // Input, double A, the lower truncation limit.
  2855. //
  2856. // Output, double TRUNCATED_NORMAL_A_PDF, the value of the PDF.
  2857. //
  2858. {
  2859. double alpha;
  2860. double alpha_cdf;
  2861. double pdf;
  2862. double xi;
  2863. double xi_pdf;
  2864. alpha = (a - mu) / sigma;
  2865. xi = (x - mu) / sigma;
  2866. alpha_cdf = normal_01_cdf(alpha);
  2867. xi_pdf = normal_01_pdf(xi);
  2868. pdf = xi_pdf / (1.0 - alpha_cdf) / sigma;
  2869. return pdf;
  2870. }
  2871. //****************************************************************************80
  2872. void truncated_normal_a_pdf_values(int &n_data, double &mu, double &sigma,
  2873. double &a, double &x, double &fx)
  2874. //****************************************************************************80
  2875. //
  2876. // Purpose:
  2877. //
  2878. // TRUNCATED_NORMAL_A_PDF_VALUES: values of the Lower Truncated Normal PDF.
  2879. //
  2880. // Discussion:
  2881. //
  2882. // The Normal distribution, with mean Mu and standard deviation Sigma,
  2883. // is truncated to the interval [A,+oo).
  2884. //
  2885. // Licensing:
  2886. //
  2887. // This code is distributed under the GNU LGPL license.
  2888. //
  2889. // Modified:
  2890. //
  2891. // 14 September 2013
  2892. //
  2893. // Author:
  2894. //
  2895. // John Burkardt
  2896. //
  2897. // Reference:
  2898. //
  2899. // Stephen Wolfram,
  2900. // The Mathematica Book,
  2901. // Fourth Edition,
  2902. // Cambridge University Press, 1999,
  2903. // ISBN: 0-521-64314-7,
  2904. // LC: QA76.95.W65.
  2905. //
  2906. // Parameters:
  2907. //
  2908. // Input/output, int &N_DATA. The user sets N_DATA to 0 before the
  2909. // first call. On each call, the routine increments N_DATA by 1, and
  2910. // returns the corresponding data; when there is no more data, the
  2911. // output value of N_DATA will be 0 again.
  2912. //
  2913. // Output, double &MU, the mean of the distribution.
  2914. //
  2915. // Output, double &SIGMA, the standard deviation of the distribution.
  2916. //
  2917. // Output, double &A, the lower truncation limit.
  2918. //
  2919. // Output, double &X, the argument of the function.
  2920. //
  2921. // Output, double &FX, the value of the function.
  2922. //
  2923. {
  2924. #define N_MAX 11
  2925. static double a_vec[N_MAX] = {50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
  2926. 50.0, 50.0, 50.0, 50.0, 50.0};
  2927. static double fx_vec[N_MAX] = {
  2928. 0.01507373507401876, 0.01551417047139894, 0.01586560931024694,
  2929. 0.01612150073158793, 0.01627701240029317, 0.01632918226724295,
  2930. 0.01627701240029317, 0.01612150073158793, 0.01586560931024694,
  2931. 0.01551417047139894, 0.01507373507401876};
  2932. static double mu_vec[N_MAX] = {100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
  2933. 100.0, 100.0, 100.0, 100.0, 100.0};
  2934. static double sigma_vec[N_MAX] = {25.0, 25.0, 25.0, 25.0, 25.0, 25.0,
  2935. 25.0, 25.0, 25.0, 25.0, 25.0};
  2936. static double x_vec[N_MAX] = {90.0, 92.0, 94.0, 96.0, 98.0, 100.0,
  2937. 102.0, 104.0, 106.0, 108.0, 110.0};
  2938. if (n_data < 0) {
  2939. n_data = 0;
  2940. }
  2941. n_data = n_data + 1;
  2942. if (N_MAX < n_data) {
  2943. n_data = 0;
  2944. a = 0.0;
  2945. mu = 0.0;
  2946. sigma = 0.0;
  2947. x = 0.0;
  2948. fx = 0.0;
  2949. } else {
  2950. a = a_vec[n_data - 1];
  2951. mu = mu_vec[n_data - 1];
  2952. sigma = sigma_vec[n_data - 1];
  2953. x = x_vec[n_data - 1];
  2954. fx = fx_vec[n_data - 1];
  2955. }
  2956. return;
  2957. #undef N_MAX
  2958. }
  2959. //****************************************************************************80
  2960. double truncated_normal_a_sample(double mu, double sigma, double a, int &seed)
  2961. //****************************************************************************80
  2962. //
  2963. // Purpose:
  2964. //
  2965. // TRUNCATED_NORMAL_A_SAMPLE samples the lower truncated Normal PDF.
  2966. //
  2967. // Licensing:
  2968. //
  2969. // This code is distributed under the GNU LGPL license.
  2970. //
  2971. // Modified:
  2972. //
  2973. // 21 August 2013
  2974. //
  2975. // Author:
  2976. //
  2977. // John Burkardt
  2978. //
  2979. // Parameters:
  2980. //
  2981. // Input, double MU, SIGMA, the mean and standard deviation of the
  2982. // parent Normal distribution.
  2983. //
  2984. // Input, double A, the lower truncation limit.
  2985. //
  2986. // Input/output, int &SEED, a seed for the random number
  2987. // generator.
  2988. //
  2989. // Output, double TRUNCATED_NORMAL_A_SAMPLE, a sample of the PDF.
  2990. //
  2991. {
  2992. double alpha;
  2993. double alpha_cdf;
  2994. double u;
  2995. double x;
  2996. double xi;
  2997. double xi_cdf;
  2998. alpha = (a - mu) / sigma;
  2999. alpha_cdf = normal_01_cdf(alpha);
  3000. u = r8_uniform_01(seed);
  3001. xi_cdf = alpha_cdf + u * (1.0 - alpha_cdf);
  3002. xi = normal_01_cdf_inv(xi_cdf);
  3003. x = mu + sigma * xi;
  3004. return x;
  3005. }
  3006. //****************************************************************************80
  3007. double truncated_normal_a_variance(double mu, double sigma, double a)
  3008. //****************************************************************************80
  3009. //
  3010. // Purpose:
  3011. //
  3012. // TRUNCATED_NORMAL_A_VARIANCE: variance of the lower truncated Normal PDF.
  3013. //
  3014. // Licensing:
  3015. //
  3016. // This code is distributed under the GNU LGPL license.
  3017. //
  3018. // Modified:
  3019. //
  3020. // 21 August 2013
  3021. //
  3022. // Author:
  3023. //
  3024. // John Burkardt
  3025. //
  3026. // Parameters:
  3027. //
  3028. // Input, double MU, SIGMA, the mean and standard deviation of the
  3029. // parent Normal distribution.
  3030. //
  3031. // Input, double A, the lower truncation limit.
  3032. //
  3033. // Output, double TRUNCATED_NORMAL_A_VARIANCE, the variance of the PDF.
  3034. //
  3035. {
  3036. double alpha;
  3037. double alpha_cdf;
  3038. double alpha_pdf;
  3039. double variance;
  3040. alpha = (a - mu) / sigma;
  3041. alpha_pdf = normal_01_pdf(alpha);
  3042. alpha_cdf = normal_01_cdf(alpha);
  3043. variance = sigma * sigma * (1.0 + alpha * alpha_pdf / (1.0 - alpha_cdf) -
  3044. pow(alpha_pdf / (1.0 - alpha_cdf), 2));
  3045. return variance;
  3046. }
  3047. //****************************************************************************80
  3048. double truncated_normal_b_cdf(double x, double mu, double sigma, double b)
  3049. //****************************************************************************80
  3050. //
  3051. // Purpose:
  3052. //
  3053. // TRUNCATED_NORMAL_B_CDF evaluates the upper truncated Normal CDF.
  3054. //
  3055. // Licensing:
  3056. //
  3057. // This code is distributed under the GNU LGPL license.
  3058. //
  3059. // Modified:
  3060. //
  3061. // 21 August 2013
  3062. //
  3063. // Author:
  3064. //
  3065. // John Burkardt
  3066. //
  3067. // Parameters:
  3068. //
  3069. // Input, double X, the argument of the CDF.
  3070. //
  3071. // Input, double MU, SIGMA, the mean and standard deviation of the
  3072. // parent Normal distribution.
  3073. //
  3074. // Input, double B, the upper truncation limit.
  3075. //
  3076. // Output, double TRUNCATED_NORMAL_B_CDF, the value of the CDF.
  3077. //
  3078. {
  3079. double beta;
  3080. double beta_cdf;
  3081. double cdf;
  3082. double xi;
  3083. double xi_cdf;
  3084. beta = (b - mu) / sigma;
  3085. xi = (x - mu) / sigma;
  3086. beta_cdf = normal_01_cdf(beta);
  3087. xi_cdf = normal_01_cdf(xi);
  3088. cdf = xi_cdf / beta_cdf;
  3089. return cdf;
  3090. }
  3091. //****************************************************************************80
  3092. void truncated_normal_b_cdf_values(int &n_data, double &mu, double &sigma,
  3093. double &b, double &x, double &fx)
  3094. //****************************************************************************80
  3095. //
  3096. // Purpose:
  3097. //
  3098. // TRUNCATED_NORMAL_B_CDF_VALUES: values of the upper Truncated Normal CDF.
  3099. //
  3100. // Discussion:
  3101. //
  3102. // The Normal distribution, with mean Mu and standard deviation Sigma,
  3103. // is truncated to the interval (-oo,B].
  3104. //
  3105. // Licensing:
  3106. //
  3107. // This code is distributed under the GNU LGPL license.
  3108. //
  3109. // Modified:
  3110. //
  3111. // 14 September 2013
  3112. //
  3113. // Author:
  3114. //
  3115. // John Burkardt
  3116. //
  3117. // Reference:
  3118. //
  3119. // Stephen Wolfram,
  3120. // The Mathematica Book,
  3121. // Fourth Edition,
  3122. // Cambridge University Press, 1999,
  3123. // ISBN: 0-521-64314-7,
  3124. // LC: QA76.95.W65.
  3125. //
  3126. // Parameters:
  3127. //
  3128. // Input/output, int &N_DATA. The user sets N_DATA to 0 before the
  3129. // first call. On each call, the routine increments N_DATA by 1, and
  3130. // returns the corresponding data; when there is no more data, the
  3131. // output value of N_DATA will be 0 again.
  3132. //
  3133. // Output, double &MU, the mean of the distribution.
  3134. //
  3135. // Output, double &SIGMA, the standard deviation of the distribution.
  3136. //
  3137. // Output, double &B, the upper truncation limit.
  3138. //
  3139. // Output, double &X, the argument of the function.
  3140. //
  3141. // Output, double &FX, the value of the function.
  3142. //
  3143. {
  3144. #define N_MAX 11
  3145. static double b_vec[N_MAX] = {150.0, 150.0, 150.0, 150.0, 150.0, 150.0,
  3146. 150.0, 150.0, 150.0, 150.0, 150.0};
  3147. static double fx_vec[N_MAX] = {
  3148. 0.3525999538650271, 0.3832020627674540, 0.4145972709210122,
  3149. 0.4466007634041696, 0.4790163122960786, 0.5116398746584291,
  3150. 0.5442634370207796, 0.5766789859126887, 0.6086824783958461,
  3151. 0.6400776865494043, 0.6706797954518312};
  3152. static double mu_vec[N_MAX] = {100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
  3153. 100.0, 100.0, 100.0, 100.0, 100.0};
  3154. static double sigma_vec[N_MAX] = {25.0, 25.0, 25.0, 25.0, 25.0, 25.0,
  3155. 25.0, 25.0, 25.0, 25.0, 25.0};
  3156. static double x_vec[N_MAX] = {90.0, 92.0, 94.0, 96.0, 98.0, 100.0,
  3157. 102.0, 104.0, 106.0, 108.0, 110.0};
  3158. if (n_data < 0) {
  3159. n_data = 0;
  3160. }
  3161. n_data = n_data + 1;
  3162. if (N_MAX < n_data) {
  3163. n_data = 0;
  3164. b = 0.0;
  3165. mu = 0.0;
  3166. sigma = 0.0;
  3167. x = 0.0;
  3168. fx = 0.0;
  3169. } else {
  3170. b = b_vec[n_data - 1];
  3171. mu = mu_vec[n_data - 1];
  3172. sigma = sigma_vec[n_data - 1];
  3173. x = x_vec[n_data - 1];
  3174. fx = fx_vec[n_data - 1];
  3175. }
  3176. return;
  3177. #undef N_MAX
  3178. }
  3179. //****************************************************************************80
  3180. double truncated_normal_b_cdf_inv(double cdf, double mu, double sigma, double b)
  3181. //****************************************************************************80
  3182. //
  3183. // Purpose:
  3184. //
  3185. // TRUNCATED_NORMAL_B_CDF_INV inverts the upper truncated Normal CDF.
  3186. //
  3187. // Licensing:
  3188. //
  3189. // This code is distributed under the GNU LGPL license.
  3190. //
  3191. // Modified:
  3192. //
  3193. // 21 August 2013
  3194. //
  3195. // Author:
  3196. //
  3197. // John Burkardt
  3198. //
  3199. // Parameters:
  3200. //
  3201. // Input, double CDF, the value of the CDF.
  3202. // 0.0 <= CDF <= 1.0.
  3203. //
  3204. // Input, double MU, SIGMA, the mean and standard deviation of the
  3205. // parent Normal distribution.
  3206. //
  3207. // Input, double B, the upper truncation limit.
  3208. //
  3209. // Output, double TRUNCATED_NORMAL_B_CDF_INV, the corresponding argument.
  3210. //
  3211. {
  3212. double beta;
  3213. double beta_cdf;
  3214. double x;
  3215. double xi;
  3216. double xi_cdf;
  3217. if (cdf < 0.0 || 1.0 < cdf) {
  3218. cerr << "\n";
  3219. cerr << "TRUNCATED_NORMAL_B_CDF_INV - Fatal error!\n";
  3220. cerr << " CDF < 0 or 1 < CDF.\n";
  3221. exit(1);
  3222. }
  3223. beta = (b - mu) / sigma;
  3224. beta_cdf = normal_01_cdf(beta);
  3225. xi_cdf = beta_cdf * cdf;
  3226. xi = normal_01_cdf_inv(xi_cdf);
  3227. x = mu + sigma * xi;
  3228. return x;
  3229. }
  3230. //****************************************************************************80
  3231. double truncated_normal_b_mean(double mu, double sigma, double b)
  3232. //****************************************************************************80
  3233. //
  3234. // Purpose:
  3235. //
  3236. // TRUNCATED_NORMAL_B_MEAN returns the mean of the upper truncated Normal
  3237. // PDF.
  3238. //
  3239. // Licensing:
  3240. //
  3241. // This code is distributed under the GNU LGPL license.
  3242. //
  3243. // Modified:
  3244. //
  3245. // 21 August 2013
  3246. //
  3247. // Author:
  3248. //
  3249. // John Burkardt
  3250. //
  3251. // Parameters:
  3252. //
  3253. // Input, double MU, SIGMA, the mean and standard deviation of the
  3254. // parent Normal distribution.
  3255. //
  3256. // Input, double B, the upper truncation limit.
  3257. //
  3258. // Output, double TRUNCATED_NORMAL_B_MEAN, the mean of the PDF.
  3259. //
  3260. {
  3261. double beta;
  3262. double beta_cdf;
  3263. double beta_pdf;
  3264. double mean;
  3265. beta = (b - mu) / sigma;
  3266. beta_cdf = normal_01_cdf(beta);
  3267. beta_pdf = normal_01_pdf(beta);
  3268. mean = mu - sigma * beta_pdf / beta_cdf;
  3269. return mean;
  3270. }
  3271. //****************************************************************************80
  3272. double truncated_normal_b_moment(int order, double mu, double sigma, double b)
  3273. //****************************************************************************80
  3274. //
  3275. // Purpose:
  3276. //
  3277. // TRUNCATED_NORMAL_B_MOMENT: moments of the upper truncated Normal PDF.
  3278. //
  3279. // Licensing:
  3280. //
  3281. // This code is distributed under the GNU LGPL license.
  3282. //
  3283. // Modified:
  3284. //
  3285. // 11 September 2013
  3286. //
  3287. // Author:
  3288. //
  3289. // John Burkardt
  3290. //
  3291. // Reference:
  3292. //
  3293. // Phoebus Dhrymes,
  3294. // Moments of Truncated Normal Distributions,
  3295. // May 2005.
  3296. //
  3297. // Parameters:
  3298. //
  3299. // Input, int ORDER, the order of the moment.
  3300. // 0 <= ORDER.
  3301. //
  3302. // Input, double MU, SIGMA, the mean and standard deviation of the
  3303. // parent Normal distribution.
  3304. //
  3305. // Input, double B, the upper truncation limit.
  3306. //
  3307. // Output, double TRUNCATED_NORMAL_B_MOMENT, the moment of the PDF.
  3308. //
  3309. {
  3310. double f;
  3311. double h;
  3312. double h_cdf;
  3313. double h_pdf;
  3314. double ir;
  3315. double irm1;
  3316. double irm2;
  3317. double moment;
  3318. int r;
  3319. if (order < 0) {
  3320. cerr << "\n";
  3321. cerr << "TRUNCATED_NORMAL_B_MOMENT - Fatal error!\n";
  3322. cerr << " ORDER < 0.\n";
  3323. exit(1);
  3324. }
  3325. h = (b - mu) / sigma;
  3326. h_pdf = normal_01_pdf(h);
  3327. h_cdf = normal_01_cdf(h);
  3328. if (h_cdf == 0.0) {
  3329. cerr << "\n";
  3330. cerr << "TRUNCATED_NORMAL_B_MOMENT - Fatal error!\n";
  3331. cerr << " CDF((B-MU)/SIGMA) = 0.\n";
  3332. exit(1);
  3333. }
  3334. f = h_pdf / h_cdf;
  3335. moment = 0.0;
  3336. irm2 = 0.0;
  3337. irm1 = 0.0;
  3338. for (r = 0; r <= order; r++) {
  3339. if (r == 0) {
  3340. ir = 1.0;
  3341. } else if (r == 1) {
  3342. ir = -f;
  3343. } else {
  3344. ir = -pow(h, r - 1) * f + (double)(r - 1) * irm2;
  3345. }
  3346. moment =
  3347. moment + r8_choose(order, r) * pow(mu, order - r) * pow(sigma, r) * ir;
  3348. irm2 = irm1;
  3349. irm1 = ir;
  3350. }
  3351. return moment;
  3352. }
  3353. //****************************************************************************80
  3354. double truncated_normal_b_pdf(double x, double mu, double sigma, double b)
  3355. //****************************************************************************80
  3356. //
  3357. // Purpose:
  3358. //
  3359. // TRUNCATED_NORMAL_B_PDF evaluates the upper truncated Normal PDF.
  3360. //
  3361. // Licensing:
  3362. //
  3363. // This code is distributed under the GNU LGPL license.
  3364. //
  3365. // Modified:
  3366. //
  3367. // 21 August 2013
  3368. //
  3369. // Author:
  3370. //
  3371. // John Burkardt
  3372. //
  3373. // Parameters:
  3374. //
  3375. // Input, double X, the argument of the PDF.
  3376. //
  3377. // Input, double MU, SIGMA, the mean and standard deviation of the
  3378. // parent Normal distribution.
  3379. //
  3380. // Input, double B, the upper truncation limit.
  3381. //
  3382. // Output, double TRUNCATED_NORMAL_B_PDF, the value of the PDF.
  3383. //
  3384. {
  3385. double beta;
  3386. double beta_cdf;
  3387. double pdf;
  3388. double xi;
  3389. double xi_pdf;
  3390. beta = (b - mu) / sigma;
  3391. xi = (x - mu) / sigma;
  3392. beta_cdf = normal_01_cdf(beta);
  3393. xi_pdf = normal_01_pdf(xi);
  3394. pdf = xi_pdf / beta_cdf / sigma;
  3395. return pdf;
  3396. }
  3397. //****************************************************************************80
  3398. void truncated_normal_b_pdf_values(int &n_data, double &mu, double &sigma,
  3399. double &b, double &x, double &fx)
  3400. //****************************************************************************80
  3401. //
  3402. // Purpose:
  3403. //
  3404. // TRUNCATED_NORMAL_B_PDF_VALUES: values of the Upper Truncated Normal PDF.
  3405. //
  3406. // Discussion:
  3407. //
  3408. // The Normal distribution, with mean Mu and standard deviation Sigma,
  3409. // is truncated to the interval (-oo,B].
  3410. //
  3411. // Licensing:
  3412. //
  3413. // This code is distributed under the GNU LGPL license.
  3414. //
  3415. // Modified:
  3416. //
  3417. // 14 September 2013
  3418. //
  3419. // Author:
  3420. //
  3421. // John Burkardt
  3422. //
  3423. // Reference:
  3424. //
  3425. // Stephen Wolfram,
  3426. // The Mathematica Book,
  3427. // Fourth Edition,
  3428. // Cambridge University Press, 1999,
  3429. // ISBN: 0-521-64314-7,
  3430. // LC: QA76.95.W65.
  3431. //
  3432. // Parameters:
  3433. //
  3434. // Input/output, int &N_DATA. The user sets N_DATA to 0 before the
  3435. // first call. On each call, the routine increments N_DATA by 1, and
  3436. // returns the corresponding data; when there is no more data, the
  3437. // output value of N_DATA will be 0 again.
  3438. //
  3439. // Output, double &MU, the mean of the distribution.
  3440. //
  3441. // Output, double &SIGMA, the standard deviation of the distribution.
  3442. //
  3443. // Output, double &B, the upper truncation limit.
  3444. //
  3445. // Output, double &X, the argument of the function.
  3446. //
  3447. // Output, double &FX, the value of the function.
  3448. //
  3449. {
  3450. #define N_MAX 11
  3451. static double b_vec[N_MAX] = {150.0, 150.0, 150.0, 150.0, 150.0, 150.0,
  3452. 150.0, 150.0, 150.0, 150.0, 150.0};
  3453. static double fx_vec[N_MAX] = {
  3454. 0.01507373507401876, 0.01551417047139894, 0.01586560931024694,
  3455. 0.01612150073158793, 0.01627701240029317, 0.01632918226724295,
  3456. 0.01627701240029317, 0.01612150073158793, 0.01586560931024694,
  3457. 0.01551417047139894, 0.01507373507401876};
  3458. static double mu_vec[N_MAX] = {100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
  3459. 100.0, 100.0, 100.0, 100.0, 100.0};
  3460. static double sigma_vec[N_MAX] = {25.0, 25.0, 25.0, 25.0, 25.0, 25.0,
  3461. 25.0, 25.0, 25.0, 25.0, 25.0};
  3462. static double x_vec[N_MAX] = {90.0, 92.0, 94.0, 96.0, 98.0, 100.0,
  3463. 102.0, 104.0, 106.0, 108.0, 110.0};
  3464. if (n_data < 0) {
  3465. n_data = 0;
  3466. }
  3467. n_data = n_data + 1;
  3468. if (N_MAX < n_data) {
  3469. n_data = 0;
  3470. b = 0.0;
  3471. mu = 0.0;
  3472. sigma = 0.0;
  3473. x = 0.0;
  3474. fx = 0.0;
  3475. } else {
  3476. b = b_vec[n_data - 1];
  3477. mu = mu_vec[n_data - 1];
  3478. sigma = sigma_vec[n_data - 1];
  3479. x = x_vec[n_data - 1];
  3480. fx = fx_vec[n_data - 1];
  3481. }
  3482. return;
  3483. #undef N_MAX
  3484. }
  3485. //****************************************************************************80
  3486. double truncated_normal_b_sample(double mu, double sigma, double b, int &seed)
  3487. //****************************************************************************80
  3488. //
  3489. // Purpose:
  3490. //
  3491. // TRUNCATED_NORMAL_B_SAMPLE samples the upper truncated Normal PDF.
  3492. //
  3493. // Licensing:
  3494. //
  3495. // This code is distributed under the GNU LGPL license.
  3496. //
  3497. // Modified:
  3498. //
  3499. // 21 August 2013
  3500. //
  3501. // Author:
  3502. //
  3503. // John Burkardt
  3504. //
  3505. // Parameters:
  3506. //
  3507. // Input, double MU, SIGMA, the mean and standard deviation of the
  3508. // parent Normal distribution.
  3509. //
  3510. // Input, double B, the upper truncation limit.
  3511. //
  3512. // Input/output, int &SEED, a seed for the random number
  3513. // generator.
  3514. //
  3515. // Output, double TRUNCATED_NORMAL_B_SAMPLE, a sample of the PDF.
  3516. //
  3517. {
  3518. double beta;
  3519. double beta_cdf;
  3520. double u;
  3521. double x;
  3522. double xi;
  3523. double xi_cdf;
  3524. beta = (b - mu) / sigma;
  3525. beta_cdf = normal_01_cdf(beta);
  3526. u = r8_uniform_01(seed);
  3527. xi_cdf = u * beta_cdf;
  3528. xi = normal_01_cdf_inv(xi_cdf);
  3529. x = mu + sigma * xi;
  3530. return x;
  3531. }
  3532. //****************************************************************************80
  3533. double truncated_normal_b_variance(double mu, double sigma, double b)
  3534. //****************************************************************************80
  3535. //
  3536. // Purpose:
  3537. //
  3538. // TRUNCATED_NORMAL_B_VARIANCE: variance of the upper truncated Normal PDF.
  3539. //
  3540. // Licensing:
  3541. //
  3542. // This code is distributed under the GNU LGPL license.
  3543. //
  3544. // Modified:
  3545. //
  3546. // 14 August 2013
  3547. //
  3548. // Author:
  3549. //
  3550. // John Burkardt
  3551. //
  3552. // Parameters:
  3553. //
  3554. // Input, double MU, SIGMA, the mean and standard deviation of the
  3555. // parent Normal distribution.
  3556. //
  3557. // Input, double B, the upper truncation limit.
  3558. //
  3559. // Output, double TRUNCATED_NORMAL_B_VARIANCE, the variance of the PDF.
  3560. //
  3561. {
  3562. double beta;
  3563. double beta_cdf;
  3564. double beta_pdf;
  3565. double variance;
  3566. beta = (b - mu) / sigma;
  3567. beta_pdf = normal_01_pdf(beta);
  3568. beta_cdf = normal_01_cdf(beta);
  3569. variance = sigma * sigma *
  3570. (1.0 - beta * beta_pdf / beta_cdf - pow(beta_pdf / beta_cdf, 2));
  3571. return variance;
  3572. }