yat/regression/Poisson.cc

Code
Comments
Other
Rev Date Author Line
3615 06 Feb 17 peter 1 // $Id$
3615 06 Feb 17 peter 2
3615 06 Feb 17 peter 3 /*
4207 26 Aug 22 peter 4   Copyright (C) 2017, 2022 Peter Johansson
3615 06 Feb 17 peter 5
3615 06 Feb 17 peter 6   This file is part of the yat library, http://dev.thep.lu.se/yat
3615 06 Feb 17 peter 7
3615 06 Feb 17 peter 8   The yat library is free software; you can redistribute it and/or
3615 06 Feb 17 peter 9   modify it under the terms of the GNU General Public License as
3615 06 Feb 17 peter 10   published by the Free Software Foundation; either version 3 of the
3615 06 Feb 17 peter 11   License, or (at your option) any later version.
3615 06 Feb 17 peter 12
3615 06 Feb 17 peter 13   The yat library is distributed in the hope that it will be useful,
3615 06 Feb 17 peter 14   but WITHOUT ANY WARRANTY; without even the implied warranty of
3615 06 Feb 17 peter 15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3615 06 Feb 17 peter 16   General Public License for more details.
3615 06 Feb 17 peter 17
3615 06 Feb 17 peter 18   You should have received a copy of the GNU General Public License
3615 06 Feb 17 peter 19   along with yat. If not, see <http://www.gnu.org/licenses/>.
3615 06 Feb 17 peter 20 */
3615 06 Feb 17 peter 21
3615 06 Feb 17 peter 22 #include <config.h>
3615 06 Feb 17 peter 23
3615 06 Feb 17 peter 24 #include "Poisson.h"
3658 13 Jul 17 peter 25 #include "detail/PoissonFitter.h"
3615 06 Feb 17 peter 26
3615 06 Feb 17 peter 27 #include <yat/utility/Matrix.h>
3615 06 Feb 17 peter 28 #include <yat/utility/Vector.h>
3615 06 Feb 17 peter 29 #include <yat/utility/VectorBase.h>
3615 06 Feb 17 peter 30 #include <yat/utility/VectorConstView.h>
3615 06 Feb 17 peter 31
3615 06 Feb 17 peter 32 #include <cassert>
3615 06 Feb 17 peter 33 #include <cmath>
3615 06 Feb 17 peter 34
3615 06 Feb 17 peter 35 namespace theplu {
3615 06 Feb 17 peter 36 namespace yat {
3615 06 Feb 17 peter 37 namespace regression {
3615 06 Feb 17 peter 38
3662 19 Jul 17 peter 39   const utility::Matrix& Poisson::covariance(void)
3662 19 Jul 17 peter 40   {
3662 19 Jul 17 peter 41     return covariance_;
3662 19 Jul 17 peter 42   }
3662 19 Jul 17 peter 43
3662 19 Jul 17 peter 44
4130 20 Jan 22 peter 45   void Poisson::fit(const utility::Matrix& x, const utility::VectorBase& y)
3615 06 Feb 17 peter 46   {
4130 20 Jan 22 peter 47     fit2(x, y);
4130 20 Jan 22 peter 48   }
4130 20 Jan 22 peter 49
4130 20 Jan 22 peter 50
4130 20 Jan 22 peter 51   void Poisson::fit2(const utility::MatrixBase& X, const utility::VectorBase& y)
4130 20 Jan 22 peter 52   {
3615 06 Feb 17 peter 53     assert(X.rows() == y.size());
3658 13 Jul 17 peter 54     utility::Matrix X2(X.rows(), X.columns()+1, 1.0);
3658 13 Jul 17 peter 55     for (size_t i=0; i<X.columns(); ++i)
3658 13 Jul 17 peter 56       X2.column_view(i+1) = X.column_const_view(i);
3658 13 Jul 17 peter 57
3658 13 Jul 17 peter 58     detail::PoissonFitter fitter(X2, y, beta_, covariance_);
3658 13 Jul 17 peter 59     assert(beta_.size() == X.columns() + 1);
3615 06 Feb 17 peter 60   }
3615 06 Feb 17 peter 61
3615 06 Feb 17 peter 62
3615 06 Feb 17 peter 63   const utility::Vector& Poisson::fit_parameters(void) const
3615 06 Feb 17 peter 64   {
3615 06 Feb 17 peter 65     return beta_;
3615 06 Feb 17 peter 66   }
3615 06 Feb 17 peter 67
3615 06 Feb 17 peter 68
3615 06 Feb 17 peter 69   double Poisson::predict(const utility::VectorBase& x) const
3615 06 Feb 17 peter 70   {
3615 06 Feb 17 peter 71     assert(x.size() + 1 == beta_.size());
3615 06 Feb 17 peter 72     using utility::VectorConstView;
3615 06 Feb 17 peter 73     double logm = beta_(0) + VectorConstView(beta_, 1, x.size()) * x;
3615 06 Feb 17 peter 74     return std::exp(logm);
3615 06 Feb 17 peter 75   }
3615 06 Feb 17 peter 76
3615 06 Feb 17 peter 77 }}} // of namespaces regression, yat, and theplu