Commit d8e7ceabf06a1c90f68b55ec9c743c78e47e6464

Authored by Alexander Ziegler
1 parent d8375b6b

Clean up new function to write out covariance matrix

Showing 1 changed file with 14 additions and 7 deletions
src/obs/GObservations.cpp
... ... @@ -843,12 +843,12 @@ void GObservations::errors_hessian(void)
843 843 *
844 844 * @param[in] filename FITS filename.
845 845 *
846   - * Saves the covariance matrix into FITS file.
847   - * The covariance matrix is calculated as the inverse of the curvature
848   - * matrix.
  846 + * Saves the covariance matrix into FITS file. The covariance matrix is
  847 + * calculated as the inverse of the curvature matrix.
849 848 ***************************************************************************/
850 849 void GObservations::save_covmat(const GFilename& filename)
851 850 {
  851 +
852 852 // Calculate covariance matrix
853 853 GMatrixSparse covmat = m_fct.curvature()->invert();
854 854  
... ... @@ -856,10 +856,10 @@ void GObservations::save_covmat(const GFilename& filename)
856 856 std::vector<std::string> covmat_entries;
857 857  
858 858 for (int i = 0 ; i < m_models.size(); ++i){
859   - for (int j = 0; j < m_models[i]->size(); j++){
  859 + for (int j = 0; j < m_models[i]->size(); j++){
860 860 covmat_entries.push_back(m_models[i]->at(j).name() + "(" +
861 861 m_models[i]->name() + ")" );
862   - }
  862 + }
863 863 }
864 864  
865 865 // Allocate FITS object
... ... @@ -872,16 +872,19 @@ void GObservations::save_covmat(const GFilename&amp; filename)
872 872 GFitsTableDoubleCol cov("Covariance", 1, size*size);
873 873  
874 874 // Fill tables
  875 + // We need to take the scale factors into account as
  876 + // covariance matrix is for the unscaled parameters
875 877 int counter = 0;
876 878 for (int i = 0; i < size; ++i){
877 879 par(0, i) = covmat_entries[i];
878 880 for (int j = 0; j < size; ++j){
879   - cov(0, counter) = covmat(i,j);
  881 + cov(0, counter) = covmat(i,j) * m_models.pars()[i]->scale()
  882 + * m_models.pars()[j]->scale();
880 883 ++counter;
881 884 }
882 885 }
883 886  
884   - // Set dimension
  887 + // Set dimension for covariance matrix column
885 888 std::vector<int> dim;
886 889 dim.push_back(size);
887 890 dim.push_back(size);
... ... @@ -891,6 +894,9 @@ void GObservations::save_covmat(const GFilename&amp; filename)
891 894 covmat_table.append(par);
892 895 covmat_table.append(cov);
893 896  
  897 + // Set extension name
  898 + covmat_table.extname("CovarianceTable");
  899 +
894 900 // Append table to FITS object
895 901 fits.append(covmat_table);
896 902  
... ... @@ -899,6 +905,7 @@ void GObservations::save_covmat(const GFilename&amp; filename)
899 905  
900 906 // Close FITS object
901 907 fits.close();
  908 +
902 909 }
903 910  
904 911  
... ...