STK++ 0.9.13
STK_ModelBernoulli_pj.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_MODELBERNOULLI_PJ_H
38#define STK_MODELBERNOULLI_PJ_H
39
40#include <cmath>
41
42#include "STK_Model_Util.h"
43#include "STK_IMultiStatModel.h"
45
46namespace STK
47{
48// forward declaration
49template <class Data, class WColVector = CVectorX> class ModelBernoulli_pj;
50class Bernoulli_pjParameters;
51
52namespace hidden
53{
58template<class Data_, class WColVector_>
70
71} // hidden
72
77{
78 public:
83 : prob_(size, 0.5)
84 , lnProb_(size, -Const::_LN2_)
85 , ln1mProb_(size, -Const::_LN2_)
86 {}
89 : prob_(param.prob_)
90 , lnProb_(param.lnProb_)
91 , ln1mProb_(param.ln1mProb_)
92 {}
96 inline Range const& range() const { return range_;}
98 inline CPointX const& prob() const { return prob_;}
100 inline CPointX const& lnProb() const { return lnProb_;}
102 inline CPointX const& ln1mProb() const { return ln1mProb_;}
103
105 inline void setProb(int j, Real const& prob)
106 { prob_[j] = prob;
107 if (prob>0) { lnProb_[j] = std::log(prob);}
109 if (prob<1) { ln1mProb_[j] = std::log(1.-prob);}
111 }
116 inline void resize(Range const& range)
117 {
118 if (range != range_)
119 {
121 range_ = range;
122 }
123 }
124
129};
130
141template <class Data_, class WColVector_>
142class ModelBernoulli_pj: public IMultiStatModel< ModelBernoulli_pj<Data_, WColVector_> >
143{
144 public:
154 using Base::p_data;
155 using Base::param;
156
160 ModelBernoulli_pj( Data const& data): Base(data) {}
167
169 inline CPointX const& prob() const { return param().prob();}
171 inline CPointX const& lnProb() const { return param().lnProb();}
173 inline CPointX const& ln1mProb() const { return param().ln1mProb();}
174
176 inline int computeNbFreeParameters() const { return p_data()->dataij().sizeCols();}
180 void computeParameters();
186 void writeParametersImpl(ostream& os) const;
187};
188
189/* compute the log Likelihood of an observation. */
190template<class Data_, class WColVector_>
192{
193 Real sum =0.;
194 for (Integer j= rowData.begin(); j < rowData.end(); ++j)
195 {
196 sum += rowData[j] * lnProb()[j] + (1-rowData[j] * ln1mProb()[j] );
197 }
198 return sum;
199}
200
201/* compute the parameters */
202template<class Data_, class WColVector_>
204{
205 for (int j=p_data()->dataij().beginCols(); j < p_data()->dataij().endCols(); ++j)
206 {
207 Real sum=0.;
208 int nbObs=p_data()->dataij().sizeRows();
209
210 for (int i=p_data()->dataij().beginRows(); i<p_data()->dataij().endRows(); ++i)
211 { (p_data()->dataij().elt(i,j) == binaryNA_) ? --nbObs : sum += p_data()->dataij().elt(i,j);}
212 if (nbObs != 0) { param().prob_[j] = sum/nbObs;}
213 else { param().prob_[j] = Arithmetic<Real>::NA();}
214 }
215}
216/* compute the weighted parameters */
217template<class Data_, class WColVector_>
219{
220 // compute
221 for (int j=p_data()->dataij().beginCols(); j < p_data()->dataij().endCols(); ++j)
222 {
223 Real sum=0., wsum = 0.;
224 for (int i=p_data()->dataij().beginRows(); i<p_data()->dataij().endRows(); ++i)
225 { if (p_data()->dataij().elt(i,j) != binaryNA_)
226 { sum += weights[i]*p_data()->dataij().elt(i,j);
227 wsum += weights[i];
228 }
229 }
230 if (wsum != 0) { param().prob_[j] = sum/wsum;}
231 else { param().prob_[j] = Arithmetic<Real>::NA();}
232 }
233}
234
235/* Write the parameters on the output stream os */
236template<class Data_, class WColVector_>
238{
239 os << _T("prob = ") << prob();
240 os << _T("lnProb = ") << lnProb();
241 os << _T("ln1mProb = ") << ln1mProb();
242}
243
244} // namespace STK
245
246#endif /* STK_MODELBERNOULLI_PJ_H */
In this file we define the class IMultiStatModel.
In this file we define the Bernoulli distribution.
In this file we define the constant and utilities methods used in the project Model.
#define _T(x)
Let x unmodified.
Structure encapsulating the parameters of a Joint Bernoulli model.
void resize(Range const &range)
resize the parameters only if the range is modified, otherwise, stay with the current values.
Bernoulli_pjParameters()
default constructor
Bernoulli_pjParameters(Bernoulli_pjParameters const &param)
copy constructor.
Bernoulli_pjParameters(Range const &size)
constructor with fixed size
void setProb(int j, Real const &prob)
set the probability of success of the jth law
CPointX const & ln1mProb() const
Derived & resize(Range const &I, Range const &J)
resize the Array.
Interface base class for all Multivariate Statistical Models.
Real computeLnLikelihood() const
compute the log Likelihood of the statistical model.
A joint Bernoulli model is a statistical model of the form: following form.
CPointX const & ln1mProb() const
vector of the log probabilities of the reversed observations
WColVector_ WColVector
Type of the array storing the weights of the data.
DataBridge< Data_ > Data
Type of the container storing the data.
IMultiStatModel< ModelBernoulli_pj< Data_, WColVector_ > > Base
Base class.
CPointX const & prob() const
hidden::Traits< Data_ >::Type Type
Type of the data in the container.
ModelBernoulli_pj()
default constructor.
ModelBernoulli_pj(Data const *p_data)
Constructor with a ptr on the data set.
int computeNbFreeParameters() const
compute the number of free parameters
CPointX const & lnProb() const
vector of the log probabilities of the observations
void writeParametersImpl(ostream &os) const
Write the parameters on the output stream os.
hidden::Traits< Data_ >::Row RowVector
void computeParameters()
compute the parameters
ModelBernoulli_pj(ModelBernoulli_pj const &model)
Copy constructor.
ModelBernoulli_pj(Data const &data)
Constructor with data set.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
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.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
static Type NA()
Adding a Non Available (NA) special number.
Bernoulli_pjParameters Parameters
Type of the parameters of the ModelBernoulli_pj.
Traits< Data_ >::Type Type
Type of the data in the container.
DataBridge< Data_ > Data
Type of the container storing the data.
WColVector_ WColVector
Type of the array storing the weights of the data.
Policy trait class for (Stat) Model classes.