STK++ 0.9.13
STK_BernoulliModel.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Model
27 * created on: 22 juil. 2011
28 * Purpose: define the class IUnivStatModel.
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 *
31 **/
32
37#ifndef STK_BERNOULLIMODEL_H
38#define STK_BERNOULLIMODEL_H
39
40#include <cmath>
41
42#include "STK_IUnivStatModel.h"
45
46namespace STK
47{
48
56template <class Array, class WColVector>
57class BernoulliModel: public IUnivStatModel<Array, WColVector, Law::Bernoulli>
58{
59 using IUnivStatModel<Array, WColVector, Law::Bernoulli>::p_data;
60
61 public:
63 typedef typename Array::Type Type;
69 BernoulliModel(Array const& data): Base(data) {}
71 BernoulliModel(Array const* p_data): Base(p_data) {}
75 BernoulliModel* clone() const { return new BernoulliModel(*this);}
77 virtual ~BernoulliModel() {}
79 virtual int computeNbFreeParameters() const { return 1;}
80
81 protected:
85 virtual void computeParameters()
86 {
87 Real sum=0.;
88 int nbObs=p_data()->size();
89 for (int i=p_data()->begin(); i<=p_data()->lastIdx(); ++i)
90 { (p_data()->elt(i) == binaryNA_) ? --nbObs : sum += p_data()->elt(i);}
91 if (nbObs != 0) { this->law_.setProb(sum/nbObs);}
92 else { this->law_.setProb(0.);}
93 }
97 virtual void computeParameters(WColVector const& weights)
98 {
99 Real sum=0.;
100 int nbObs=p_data()->size();
101 for (int i=p_data()->begin(); i<=p_data()->lastIdx(); ++i)
102 { (p_data()->elt(i) == binaryNA_) ? --nbObs : sum += weights[i]*p_data()->elt(i);}
103 if (nbObs != 0) { this->law_.setProb(sum/nbObs);}
104 else { this->law_.setProb(0.);}
105 }
106};
107
108} // namespace STK
109
110#endif /* STK_BERNOULLIMODEL_H */
In this file we implement the final class CArrayVector.
In this file we define the class IUnivStatModel.
In this file we define the Bernoulli distribution.
A Bernoulli model, is a statistical model of the following form.
BernoulliModel()
default constructor.
Array::Type Type
Type of the data contained in the container.
virtual void computeParameters()
compute the empirical probability of success based on the observed variables.
virtual int computeNbFreeParameters() const
compute the number of free parameters
BernoulliModel(BernoulliModel const &model)
Copy constructor.
BernoulliModel(Array const *p_data)
Constructor with a ptr on the data set.
virtual void computeParameters(WColVector const &weights)
compute the weighted empirical probability of success based on the observed variables.
IUnivStatModel< Array, WColVector, Law::Bernoulli > Base
Base class.
virtual ~BernoulliModel()
destructor
BernoulliModel * clone() const
clone patern
BernoulliModel(Array const &data)
Constructor with data set.
Array const * p_data() const
get the data set
Base class for all Statistical Models.
Law::Bernoulli law_
The probability law of the model.
Bernoulli probability law.
void setProb(Real const &prob)
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
@ binaryNA_
Not Available value.
Definition STK_Binary.h:51
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.