Compartir a través de


negative_binomial_distribution (Clase)

Genera una distribución binomial negativa.

Sintaxis

template<class IntType = int>
class negative_binomial_distribution
{
public:
    // types
    typedef IntType result_type;
    struct param_type;

    // constructor and reset functions
    explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
    explicit negative_binomial_distribution(const param_type& parm);
    void reset();

    // generating functions
    template `<`class URNG>
    result_type operator()(URNG& gen);
    template `<`class URNG>
    result_type operator()(URNG& gen, const param_type& parm);

    // property functions
    result_type k() const;
    double p() const;
    param_type param() const;
    void param(const param_type& parm);
    result_type min() const;
    result_type max() const;
};

Parámetros

IntType
Un tipo de resultado de entero, el valor predeterminado es int. Para obtener información sobre los tipos posibles, consulte <random>.

Comentarios

La plantilla de clase describe una distribución que produce valores de un tipo entero especificado por el usuario, o de tipo int si no se proporciona ninguno, distribuido según la función de probabilidad discreta de distribución binomial negativa. La tabla siguiente incluye vínculos a artículos sobre miembros individuales.

negative_binomial_distribution
param_type

Los miembros de propiedad k() y p() devuelven los valores de los parámetros de distribución almacenados actualmente k y p, respectivamente.

El miembro de propiedad param() establece o devuelve el paquete de parámetros de distribución almacenado param_type.

Las funciones miembro min() y max() devuelven el resultado posible más pequeño y el resultado posible más grande, respectivamente.

La función miembro reset() descarta cualquier valor almacenado en caché, de modo que la siguiente llamada a operator() no depende de ningún valor obtenido del motor antes de la llamada.

Las funciones miembro operator() devuelven el siguiente valor generado basado en el motor URNG, desde el paquete de parámetros actual o desde el paquete de parámetros especificado.

Para obtener más información sobre las clases de distribución y sus miembros, vea <random>.

Para obtener información detallada sobre la función de probabilidad discreta de distribución binomial negativa, vea el artículo de Wolfram MathWorld sobre la distribución binomial negativa.

Ejemplo

// compile with: /EHsc /W4
#include <random>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>

void test(const int k, const double p, const int& s) {

    // uncomment to use a non-deterministic seed
    //    std::random_device rd;
    //    std::mt19937 gen(rd());
    std::mt19937 gen(1729);

    std::negative_binomial_distribution<> distr(k, p);

    std::cout << std::endl;
    std::cout << "k == " << distr.k() << std::endl;
    std::cout << "p == " << distr.p() << std::endl;

    // generate the distribution as a histogram
    std::map<int, int> histogram;
    for (int i = 0; i < s; ++i) {
        ++histogram[distr(gen)];
    }

    // print results
    std::cout << "Histogram for " << s << " samples:" << std::endl;
    for (const auto& elem : histogram) {
        std::cout << std::setw(5) << elem.first << ' ' << std::string(elem.second, ':') << std::endl;
    }
    std::cout << std::endl;
}

int main()
{
    int    k_dist = 1;
    double p_dist = 0.5;
    int    samples = 100;

    std::cout << "Use CTRL-Z to bypass data entry and run using default values." << std::endl;
    std::cout << "Enter an integer value for k distribution (where 0 < k): ";
    std::cin >> k_dist;
    std::cout << "Enter a double value for p distribution (where 0.0 < p <= 1.0): ";
    std::cin >> p_dist;
    std::cout << "Enter an integer value for a sample count: ";
    std::cin >> samples;

    test(k_dist, p_dist, samples);
}

Primera ejecución:

Use CTRL-Z to bypass data entry and run using default values.
Enter an integer value for k distribution (where 0 `<` k): 1
Enter a double value for p distribution (where 0.0 `<`p `<`= 1.0): .5
Enter an integer value for a sample count: 100

k == 1
p == 0.5
Histogram for 100 samples:
    0 :::::::::::::::::::::::::::::::::::::::::::
    1 ::::::::::::::::::::::::::::::::
    2 ::::::::::::
    3 :::::::
    4 ::::
    5 ::

Segunda ejecución:

Use CTRL-Z to bypass data entry and run using default values.
Enter an integer value for k distribution (where 0 `<` k): 100
Enter a double value for p distribution (where 0.0 `<` p <= 1.0): .667
Enter an integer value for a sample count: 100

k == 100
p == 0.667
Histogram for 100 samples:
    31 ::
    32 :
    33 ::
    34 :
    35 ::
    37 ::
    38 :
    39 :
    40 ::
    41 :::
    42 :::
    43 :::::
    44 :::::
    45 ::::
    46 ::::::
    47 ::::::::
    48 :::
    49 :::
    50 :::::::::
    51 :::::::
    52 ::
    53 :::
    54 :::::
    56 ::::
    58 :
    59 :::::
    60 ::
    61 :
    62 ::
    64 :
    69 ::::

Requisitos

Encabezado:<random>

Espacio de nombres: std

negative_binomial_distribution::negative_binomial_distribution

Construye la distribución.

explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
explicit negative_binomial_distribution(const param_type& parm);

Parámetros

k
El parámetro de distribución k.

p
El parámetro de distribución p.

parm
La estructura de parámetros utilizada para construir la distribución.

Comentarios

Condición previa:0.0 < k y 0.0 < p ≤ 1.0

El primer constructor crea un objeto cuyo valor p almacenado contiene el valor p y cuyo valor k almacenado contiene el valor k.

El segundo constructor crea un objeto cuyos parámetros almacenados se inicializan desde parm. Los parámetros actuales de una distribución existente se pueden obtener y definir llamando a la función miembro param().

negative_binomial_distribution::p aram_type

Almacena los parámetros de la distribución.

struct param_type { typedef negative_binomial_distribution<result_type> distribution_type; param_type(result_type k = 1, double p = 0.5); result_type k() const; double p() const;

bool operator==(const param_type& right) const; bool operator!=(const param_type& right) const; };

Parámetros

k
El parámetro de distribución k.

p
El parámetro de distribución p.

right
La estructura param_type que se usa para comparar.

Comentarios

Condición previa:0.0 < k y 0.0 < p ≤ 1.0

Esta estructura se puede pasar al constructor de clases de la distribución en el momento de creación de instancias, a la función miembro param() para definir los parámetros almacenados de una distribución existente y a operator() para usarse en lugar de los parámetros almacenados.

Consulte también

<random>