gsl_histogram__pdf.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* histogram/pdf.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include "gsl_errno.h"
  22. #include "gsl_histogram.h"
  23. #include "gsl_histogram__find.c"
  24. double
  25. gsl_histogram_pdf_sample (const gsl_histogram_pdf * p, double r)
  26. {
  27. size_t i;
  28. int status;
  29. /* Wrap the exclusive top of the bin down to the inclusive bottom of
  30. the bin. Since this is a single point it should not affect the
  31. distribution. */
  32. if (r == 1.0)
  33. {
  34. r = 0.0;
  35. }
  36. status = find (p->n, p->sum, r, &i);
  37. if (status)
  38. {
  39. GSL_ERROR_VAL ("cannot find r in cumulative pdf", GSL_EDOM, 0);
  40. }
  41. else
  42. {
  43. double delta = (r - p->sum[i]) / (p->sum[i + 1] - p->sum[i]);
  44. double x = p->range[i] + delta * (p->range[i + 1] - p->range[i]);
  45. return x;
  46. }
  47. }
  48. gsl_histogram_pdf *
  49. gsl_histogram_pdf_alloc (const size_t n)
  50. {
  51. gsl_histogram_pdf *p;
  52. if (n == 0)
  53. {
  54. GSL_ERROR_VAL ("histogram pdf length n must be positive integer",
  55. GSL_EDOM, 0);
  56. }
  57. p = (gsl_histogram_pdf *) malloc (sizeof (gsl_histogram_pdf));
  58. if (p == 0)
  59. {
  60. GSL_ERROR_VAL ("failed to allocate space for histogram pdf struct",
  61. GSL_ENOMEM, 0);
  62. }
  63. p->range = (double *) malloc ((n + 1) * sizeof (double));
  64. if (p->range == 0)
  65. {
  66. free (p); /* exception in constructor, avoid memory leak */
  67. GSL_ERROR_VAL ("failed to allocate space for histogram pdf ranges",
  68. GSL_ENOMEM, 0);
  69. }
  70. p->sum = (double *) malloc ((n + 1) * sizeof (double));
  71. if (p->sum == 0)
  72. {
  73. free (p->range);
  74. free (p); /* exception in constructor, avoid memory leak */
  75. GSL_ERROR_VAL ("failed to allocate space for histogram pdf sums",
  76. GSL_ENOMEM, 0);
  77. }
  78. p->n = n;
  79. return p;
  80. }
  81. int
  82. gsl_histogram_pdf_init (gsl_histogram_pdf * p, const gsl_histogram * h)
  83. {
  84. size_t i;
  85. size_t n = p->n;
  86. if (n != h->n)
  87. {
  88. GSL_ERROR ("histogram length must match pdf length", GSL_EINVAL);
  89. }
  90. for (i = 0; i < n; i++)
  91. {
  92. if (h->bin[i] < 0)
  93. {
  94. GSL_ERROR ("histogram bins must be non-negative to compute"
  95. "a probability distribution", GSL_EDOM);
  96. }
  97. }
  98. for (i = 0; i < n + 1; i++)
  99. {
  100. p->range[i] = h->range[i];
  101. }
  102. {
  103. double mean = 0, sum = 0;
  104. for (i = 0; i < n; i++)
  105. {
  106. mean += (h->bin[i] - mean) / ((double) (i + 1));
  107. }
  108. p->sum[0] = 0;
  109. for (i = 0; i < n; i++)
  110. {
  111. sum += (h->bin[i] / mean) / n;
  112. p->sum[i + 1] = sum;
  113. }
  114. }
  115. return GSL_SUCCESS;
  116. }
  117. void
  118. gsl_histogram_pdf_free (gsl_histogram_pdf * p)
  119. {
  120. free (p->range);
  121. free (p->sum);
  122. free (p);
  123. }