package DoseMe::Algorithms::Distributions::BCCG;

use v5.36;

use strict;
use warnings;
use utf8;

use version; our $VERSION = qv('0.3.1');

# ABSTRACT: Methods and algorithms related to BCCG (Box-Cox Cole & Green) distributions

use Exporter;
use base 'Exporter';
use Carp qw(confess);
use Statistics::Distributions qw(uprob);

use DoseMe::Util::Types;

our @ISA = qw(Exporter);
our @EXPORT = qw(
    bccg_cdf
);



### -- Unit.Fault:BCCG_CDF.Params.Undefined{Q}
### -- Unit.Fault:BCCG_CDF.Params.Invalid{Q,Mu,Sigma,Nu}
### -- Unit.Accept:BCCG_CDF.Params.Valid.PositiveNonZero{Mu,Sigma}
### -- Unit.Accept:BCCG_CDF.Params.Valid.LowerTail.{True,False}
### -- Unit.Accept:BCCG_CDF.Params.Valid.LogP.{True,False}
sub bccg_cdf {
    my ($q, $mu, $sigma, $nu, $lower_tail, $log_p) = @_;

    # Set defaults as per pBCCG()
    $mu //= 1;
    $sigma //= 0.1;
    $nu //= 1;
    $lower_tail //= 1; # return lower tail probability; default true
    $log_p //= 0; # return probability as log(p); default false

    DoseMe::Util::Types::verify_numeric($q);
    DoseMe::Util::Types::verify_positive($mu);
    DoseMe::Util::Types::verify_positive($sigma);
    DoseMe::Util::Types::verify_numeric($nu);

    my $z = undef;
    if ($nu != 0) {
        $z = (($q/$mu)**$nu-1) / ($nu*$sigma);
    } else {
        $z = log($q/$mu) / $sigma; # log() is natural log in Perl and R
    }

    my $FYy1 = 1 - uprob($z); # equivalent to R pnorm(z, mean=0, sd=1, lower.tail=TRUE)

    my $FYy2 = 0;
    if ($nu > 0) {
        $FYy2 = 1 - uprob(-1 / ($sigma*abs($nu)));
    }

    my $FYy3 = 1 - uprob(1 / ($sigma*abs($nu)));

    my $FYy = ($FYy1-$FYy2) / $FYy3;

    if (! $lower_tail) {
        $FYy = 1 - $FYy;
    }
    if ($log_p) {
        $FYy = log($FYy);
    }
    return $FYy;
}



# A Perl module must end with a true value or else it is considered not to
# have loaded.  By convention this value is usually 1 though it can be
# any true value.  A module can end with false to indicate failure but
# this is rarely used and it would instead die() (exit with an error).
1;

__END__

=pod

=encoding UTF-8

=head1 NAME

DoseMe::Algorithms::Distributions::BCCG - Methods and algorithms related to BCCG (Box-Cox Cole & Green) distributions

=head1 VERSION

version 0.3.1

=head2 bccg_cdf

Returns the Cumulative Distribution Function for a value in a given BCCG distribution.

Equivalent implementation to pBCCG() from gamlss.dist R package.

=head1 AUTHOR

DoseMe Pty Ltd

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2023, DoseMe Pty Ltd. All rights reserved.

This module is not for distribution. It is for internal DoseMe use only.

=head1 DISCLAIMER OF WARRANTY

THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE
LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE
QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE
SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO
LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.

=head1 AUTHOR

DoseMe Pty Ltd <software@doseme.com.au>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2025 by DoseMe Pty Ltd.  No
license is granted to other entities.

=cut
