Class: Numo::GSL::HistogramPdf
- Inherits:
-
Object
- Object
- Numo::GSL::HistogramPdf
- Defined in:
- ext/numo/gsl/histogram/gsl_histogram.c
Class Method Summary collapse
-
.new(n) ⇒ Object
allocate instance of HistogramPdf class.
Instance Method Summary collapse
-
#init(h) ⇒ Bool
This function initializes the probability distribution p with the contents of the histogram h.
-
#n ⇒ Integer
returns n field in gsl_histogram_pdf struct.
-
#range ⇒ DFloat
returns range field in gsl_histogram_pdf struct.
-
#sample(r) ⇒ DFloat
This function uses r, a uniform random number between zero and one, to compute a single random sample from the probability distribution p.
-
#sum ⇒ DFloat
returns sum field in gsl_histogram_pdf struct.
Class Method Details
.new(n) ⇒ Object
allocate instance of HistogramPdf class.
This function allocates memory for a probability distribution with n bins and returns a pointer to a newly initialized gsl_histogram_pdf struct. If insufficient memory is available a null pointer is returned and the error handler is invoked with an error code of GSL_ENOMEM.
996 997 998 999 1000 1001 1002 1003 1004 1005 |
# File 'ext/numo/gsl/histogram/gsl_histogram.c', line 996
static VALUE
histogram_pdf_s_new(VALUE self, VALUE v1)
{
gsl_histogram_pdf *w;
w = gsl_histogram_pdf_alloc(NUM2SIZET(v1));
if (!w) {
rb_raise(rb_eNoMemError,"fail to allocate struct");
}
return TypedData_Wrap_Struct(cHistogramPdf, &histogram_pdf_data_type, (void*)w);
}
|
Instance Method Details
#init(h) ⇒ Bool
This function initializes the probability distribution p with the contents of the histogram h. If any of the bins of h are negative then the error handler is invoked with an error code of GSL_EDOM because a probability distribution cannot contain negative values.
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 |
# File 'ext/numo/gsl/histogram/gsl_histogram.c', line 1021
static VALUE
histogram_pdf_init(VALUE self, VALUE v1)
{
int stat;
gsl_histogram_pdf *w;
gsl_histogram *w1;
TypedData_Get_Struct(self, gsl_histogram_pdf, &histogram_pdf_data_type, w);
TypedData_Get_Struct(v1, gsl_histogram, &histogram_data_type, w1);
stat = gsl_histogram_pdf_init(w, w1);
return (stat) ? Qtrue: Qfalse;
}
|
#n ⇒ Integer
returns n field in gsl_histogram_pdf struct.
909 910 911 912 913 914 915 916 917 |
# File 'ext/numo/gsl/histogram/gsl_histogram.c', line 909
static VALUE
histogram_pdf_n(VALUE self)
{
gsl_histogram_pdf *w;
TypedData_Get_Struct(self, gsl_histogram_pdf, &histogram_pdf_data_type, w);
return SIZET2NUM(w->n);
}
|
#range ⇒ DFloat
returns range field in gsl_histogram_pdf struct.
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 |
# File 'ext/numo/gsl/histogram/gsl_histogram.c', line 928
static VALUE
histogram_pdf_range(VALUE self)
{
gsl_histogram_pdf *w;
double *d;
VALUE v;
size_t n, i, shape[1];
TypedData_Get_Struct(self, gsl_histogram_pdf, &histogram_pdf_data_type, w);
shape[0] = n = w->n+1;
v = rb_narray_new(cDF, 1, shape);
d = (double*)na_get_pointer_for_write(v);
for (i=0; i<n; i++) {
d[i] = w->range[i];
}
return v;
}
|
#sample(r) ⇒ DFloat
This function uses r, a uniform random number between zero and one, to compute a single random sample from the probability distribution p. The algorithm used to compute the sample s is given by the following formula,
s = range[i] + delta * (range[i+1] - range[i])
where i is the index which satisfies $sum[i] \le r < sum[i+1]$ sum[i] <= r < sum[i+1] and delta is $(r - sum[i])/(sum[i+1] - sum[i])$ (r - sum[i])/(sum[i+1] - sum[i]).
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 |
# File 'ext/numo/gsl/histogram/gsl_histogram.c', line 1077
static VALUE
histogram_pdf_sample(VALUE self, VALUE v1)
{
gsl_histogram_pdf *w;
ndfunc_arg_in_t ain[1] = {{cDF,0}};
ndfunc_arg_out_t aout[1] = {{cDF,0}};
ndfunc_t ndf = {iter_histogram_pdf_sample, STRIDE_LOOP|NDF_EXTRACT, 1,1, ain,aout};
TypedData_Get_Struct(self, gsl_histogram_pdf, &histogram_pdf_data_type, w);
return na_ndloop3(&ndf, w, 1, v1);
}
|
#sum ⇒ DFloat
returns sum field in gsl_histogram_pdf struct.
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 |
# File 'ext/numo/gsl/histogram/gsl_histogram.c', line 959
static VALUE
histogram_pdf_sum(VALUE self)
{
gsl_histogram_pdf *w;
double *d;
VALUE v;
size_t n, i, shape[1];
TypedData_Get_Struct(self, gsl_histogram_pdf, &histogram_pdf_data_type, w);
shape[0] = n = w->n;
v = rb_narray_new(cDF, 1, shape);
d = (double*)na_get_pointer_for_write(v);
for (i=0; i<n; i++) {
d[i] = w->sum[i];
}
return v;
}
|