Commit d8375b6b897d01ee44ea4ed91d012eb586a85b6c

Authored by Alexander Ziegler
1 parent 6f53a5ab

Add function to write out covariance matrix in FITS file

include/GObservations.hpp
... ... @@ -129,6 +129,7 @@ public:
129 129 void optimize(GOptimizer& opt);
130 130 void errors(GOptimizer& opt);
131 131 void errors_hessian(void);
  132 + void save_covmat(const GFilename& filename);
132 133 void eval(void);
133 134 double logL(void) const;
134 135 int nobserved(void) const;
... ...
src/obs/GObservations.cpp
... ... @@ -35,6 +35,9 @@
35 35 #include "GObservationRegistry.hpp"
36 36 #include "GMatrixSparse.hpp"
37 37 #include "GOptimizer.hpp"
  38 +#include "GFitsBinTable.hpp"
  39 +#include "GFitsTableStringCol.hpp"
  40 +#include "GFitsTableDoubleCol.hpp"
38 41  
39 42 /* __ Method name definitions ____________________________________________ */
40 43 #define G_AT "GObservations::at(int&)"
... ... @@ -835,6 +838,69 @@ void GObservations::errors_hessian(void)
835 838 return;
836 839 }
837 840  
  841 +/***********************************************************************//**
  842 + * @brief Save covariance matrix into FITS file
  843 + *
  844 + * @param[in] filename FITS filename.
  845 + *
  846 + * Saves the covariance matrix into FITS file.
  847 + * The covariance matrix is calculated as the inverse of the curvature
  848 + * matrix.
  849 + ***************************************************************************/
  850 +void GObservations::save_covmat(const GFilename& filename)
  851 +{
  852 + // Calculate covariance matrix
  853 + GMatrixSparse covmat = m_fct.curvature()->invert();
  854 +
  855 + // Create covariance matrix entry names
  856 + std::vector<std::string> covmat_entries;
  857 +
  858 + for (int i = 0 ; i < m_models.size(); ++i){
  859 + for (int j = 0; j < m_models[i]->size(); j++){
  860 + covmat_entries.push_back(m_models[i]->at(j).name() + "(" +
  861 + m_models[i]->name() + ")" );
  862 + }
  863 + }
  864 +
  865 + // Allocate FITS object
  866 + GFits fits;
  867 +
  868 + // Create binary table and columns
  869 + int size = covmat_entries.size();
  870 + GFitsBinTable covmat_table;
  871 + GFitsTableStringCol par("Parameters", 1, 50, size);
  872 + GFitsTableDoubleCol cov("Covariance", 1, size*size);
  873 +
  874 + // Fill tables
  875 + int counter = 0;
  876 + for (int i = 0; i < size; ++i){
  877 + par(0, i) = covmat_entries[i];
  878 + for (int j = 0; j < size; ++j){
  879 + cov(0, counter) = covmat(i,j);
  880 + ++counter;
  881 + }
  882 + }
  883 +
  884 + // Set dimension
  885 + std::vector<int> dim;
  886 + dim.push_back(size);
  887 + dim.push_back(size);
  888 + cov.dim(dim);
  889 +
  890 + // Append columns to table
  891 + covmat_table.append(par);
  892 + covmat_table.append(cov);
  893 +
  894 + // Append table to FITS object
  895 + fits.append(covmat_table);
  896 +
  897 + // Save FITS file to disk
  898 + fits.saveto(filename, true);
  899 +
  900 + // Close FITS object
  901 + fits.close();
  902 +}
  903 +
838 904  
839 905 /***********************************************************************//**
840 906 * @brief Evaluate function
... ...