Skip to content

feat(stats): add C implementation for stats/base/dists/negative-binomial/mgf #4771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(src): add the C source files
  • Loading branch information
anandkaranubc committed Jan 5, 2025
commit 7f0e049ebb0ca72ea484a906dba48bf04279c55f
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef STDLIB_STATS_BASE_DISTS_NEGATIVE_BINOMIAL_MGF_H
#define STDLIB_STATS_BASE_DISTS_NEGATIVE_BINOMIAL_MGF_H

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
*/
#ifdef __cplusplus
extern "C" {
#endif

/**
* Evaluates the moment-generating function (MGF) for a negative binomial distribution.
*
* @param t input value
* @param r number of successes until experiment is stopped
* @param p success probability
* @return evaluated MGF
*/
double stdlib_base_negative_binomial_mgf(const double t, const double r, const double p);

#ifdef __cplusplus
}
#endif

#endif // !STDLIB_STATS_BASE_DISTS_NEGATIVE_BINOMIAL_MGF_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#/
# @license Apache-2.0
#
# Copyright (c) 2024 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#/

# VARIABLES #

ifndef VERBOSE
QUIET := @
else
QUIET :=
endif

# Determine the OS ([1][1], [2][2]).
#
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
# [2]: http://stackoverflow.com/a/27776822/2225624
OS ?= $(shell uname)
ifneq (, $(findstring MINGW,$(OS)))
OS := WINNT
else
ifneq (, $(findstring MSYS,$(OS)))
OS := WINNT
else
ifneq (, $(findstring CYGWIN,$(OS)))
OS := WINNT
else
ifneq (, $(findstring Windows_NT,$(OS)))
OS := WINNT
endif
endif
endif
endif


# RULES #

#/
# Removes generated files for building an add-on.
#
# @example
# make clean-addon
#/
clean-addon:
$(QUIET) -rm -f *.o *.node

.PHONY: clean-addon

#/
# Removes generated files.
#
# @example
# make clean
#/
clean: clean-addon

.PHONY: clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "stdlib/stats/base/dists/negative-binomial/mgf.h"
#include "stdlib/math/base/napi/ternary.h"

// cppcheck-suppress shadowFunction
STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_negative_binomial_mgf )
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/exp.h"
#include "stdlib/math/base/special/pow.h"
#include "stdlib/math/base/special/ln.h"

/**
* Evaluates the moment-generating function (MGF) for a negative binomial distribution.
*
* @param t input value
* @param r number of successes until experiment is stopped
* @param p success probability
* @return evaluated MGF
*
* @example
* double y = stdlib_base_negative_binomial_mgf( 0.05, 20.0, 0.8 );
* // returns ~267.839
*/
double stdlib_base_negative_binomial_mgf(const double t, const double r, const double p) {
// Check for invalid input values:
if (
stdlib_base_is_nan(t) ||
stdlib_base_is_nan(r) ||
stdlib_base_is_nan(p) ||
r <= 0.0 ||
p < 0.0 ||
p > 1.0 ||
t >= -stdlib_base_ln(p)
) {
return 0.0 / 0.0; // NaN
}

// Compute the MGF value:
double exp_t = stdlib_base_exp(t);
double numerator = (1.0 - p) * exp_t;
double denominator = 1.0 - (p * exp_t);
double ratio = numerator / denominator;

return stdlib_base_pow(ratio, r);
}