STK++ 0.9.13
STK_JointBernoulliModel.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2015 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_JOINTBERNOULLIMODEL_H
38#define STK_JOINTBERNOULLIMODEL_H
39
40#include <cmath>
41
42#include "STK_IMultiStatModel.h"
44
45namespace STK
46{
47
51class JointBernoulliParameters: public IMultiParameters<JointBernoulliParameters>
52{
53 public:
58 : prob_(size, 0.5)
59 , lnProb_(size, -Const::_LN2_)
60 , ln1mProb_(size, -Const::_LN2_)
61 {}
64 : prob_(param.prob_)
65 , lnProb_(param.lnProb_)
66 , ln1mProb_(param.ln1mProb_)
67 {}
71 inline Real const prob(int const& j) const { return prob_[j];}
73 inline Real const lnProb(int const& j) const { return lnProb_[j];}
75 inline Real const ln1mProb(int const& j) const { return ln1mProb_[j];}
77 inline void setProb(int const& j, Real const& prob)
78 { prob_[j] = prob;
79 if (prob>0) { lnProb_[j] = std::log(prob);}
81 if (prob<1) { ln1mProb_[j] = std::log(1.-prob);}
83 }
87
88 protected:
92};
93
104template <class Array, class WColVector = CVectorX>
105class JointBernoulliModel: public IMultiStatModel<Array, WColVector, JointBernoulliParameters >
106{
107 public:
109 typedef typename Array::Type Type;
116 using Base::p_data;
117 using Base::p_param;
121 JointBernoulliModel(Array const& data): Base(data) {}
129 JointBernoulliModel* clone() const { return new JointBernoulliModel(*this);}
130
132 virtual int computeNbFreeParameters() const
133 { return p_data()->sizeCols();}
136 {
137 Real sum =0.;
138 for (Integer j= rowData.begin(); j < rowData.end(); ++j)
139 {
140 sum += rowData[j] * p_param()->lnProb(j)
141 + (1-rowData[j] * p_param()->ln1mProb(j) );
142 }
143 return sum;
144 }
145 protected:
147 virtual void computeParameters()
148 {
149 for (int j=p_data()->beginCols(); j < p_data()->endCols(); ++j)
150 {
151 Real sum=0.;
152 int nbObs=p_data()->sizeRows();
153
154 for (int i=p_data()->beginRows(); i<=p_data()->lastIdxRows(); ++i)
155 { (p_data()->elt(i,j) == binaryNA_) ? --nbObs : sum += p_data()->elt(i,j);}
156 if (nbObs != 0) { p_param()->setProb(j,sum/nbObs);}
157 else { p_param()->setProb(j, Arithmetic<Real>::NA());}
158 }
159 }
164 {
165 // compute
166 for (int j=p_data()->beginCols(); j < p_data()->endCols(); ++j)
167 {
168 Real sum=0., wsum = 0.;
169 for (int i=p_data()->beginRows(); i<=p_data()->lastIdxRows(); ++i)
170 { if (p_data()->elt(i,j) != binaryNA_)
171 { sum += weights[i]*p_data()->elt(i,j);
172 wsum += weights[i];
173 }
174 }
175 if (wsum != 0) { p_param()->setProb(j, sum/wsum);}
176 else { p_param()->setProb(j, Arithmetic<Real>::NA());}
177 }
178 }
179};
180
181} // namespace STK
182
183#endif /* STK_JOINTBERNOULLIMODEL_H */
In this file we define the class IMultiStatModel.
In this file we define the Bernoulli distribution.
Derived & resize(Range const &I, Range const &J)
resize the array.
Interface base class for the parameters of a multivariate model.
Interface base class for all Multivariate Statistical Models.
hidden::StatModelTraits< Derived >::WColVector WColVector
Type of the vector with the weights.
Data const *const p_data() const
A joint Bernoulli model is a statistical model of the form: following form.
virtual void computeParameters()
compute the parameters
JointBernoulliModel(Array const *p_data)
Constructor with a ptr on the data set.
virtual int computeNbFreeParameters() const
compute the number of free parameters
hidden::Traits< Array >::Row RowVector
Type of the row vector of the container.
Array::Type Type
Type of the data contained in the container.
virtual Real computeLnLikelihood(RowVector const &rowData) const
compute the log Likelihood of an observation.
JointBernoulliModel()
default constructor.
JointBernoulliModel(Array const &data)
Constructor with data set.
Data const *const p_data() const
virtual ~JointBernoulliModel()
destructor
JointBernoulliModel(JointBernoulliModel const &model)
Copy constructor.
virtual void computeParameters(WColVector const &weights)
compute the weighted parameters
IMultiStatModel< Array, WColVector, JointBernoulliParameters > Base
Base class.
hidden::Traits< Array >::Col ColVector
Type of the column vector of the container.
JointBernoulliModel * clone() const
clone pattern.
Structure encapsulating the parameters of a Joint Bernoulli model.
JointBernoulliParameters(Range const &size)
constructor with fixed size
Real const ln1mProb(int const &j) const
Real const prob(int const &j) const
JointBernoulliParameters()
default constructor
Real const lnProb(int const &j) const
JointBernoulliParameters(JointBernoulliParameters const &param)
copy constructor.
void resizeImpl(Range const &range)
resize the set of parameter
void setProb(int const &j, Real const &prob)
set the probability of success of the jth law
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.
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.