1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/***********************************************/
/**
* @file gravityfield2EmpiricalCovariance.cpp
*
* @brief Estimate a empircal covariance function from time series.
*
* @author Torsten Mayer-Guerr
* @date 2009-03-30
*
*/
/***********************************************/
// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This program estimates an spatial and temporal covariance matrix from
a time series of gravity fields.
Firstly for every time step $t_i$
a spherical harmonics vector $\M x_i$ from the time variable gravity field
is generated. The coefficients of the spherical harmonics expansion are
in the sequence given by \configClass{numbering}{sphericalHarmonicsNumberingType}.
If set the expansion is limited in the range between \config{minDegree}
and \config{maxDegree} inclusivly. The coefficients are related to the
reference radius~\config{R} and the Earth gravitational constant \config{GM}.
In the next step the empirical covariance matrix is estimated according to
\begin{equation}
\M\Sigma(\Delta i)_{full} = \frac{1}{N}\sum_{i=1}^N \M x_i \M x_{i+\Delta i}^T,
\end{equation}
where $\Delta i$ is given by \config{differenceStep}.
From the diagonal elements of $\M\Sigma(\Delta i)$ the isotropic accuracies
are computed
\begin{equation}
\sigma_n^2 = \frac{1}{2n+1}\sum_{m=0}^n \sigma_{cnm}^2+\sigma_{snm}^2,
\end{equation}
and a diagonal matrix is constructed $\Sigma_{iso} = \text{diag}(\sigma_2^2,\ldots,\sigma_N^2)$.
The result is computed:
\begin{equation}
\M\Sigma(\Delta i) = \alpha_{full}\M\Sigma(\Delta i)_{full}+\alpha_{iso}\M\Sigma(\Delta i)_{iso}.
\end{equation}
)";
/***********************************************/
#include "programs/program.h"
#include "files/fileMatrix.h"
#include "files/fileSphericalHarmonics.h"
#include "classes/timeSeries/timeSeries.h"
#include "classes/gravityfield/gravityfield.h"
#include "classes/sphericalHarmonicsNumbering/sphericalHarmonicsNumbering.h"
/***** CLASS ***********************************/
/** @brief Estimate a empircal covariance function from time series.
* @ingroup programsGroup */
class Gravityfield2EmpiricalCovariance
{
public:
void run(Config &config, Parallel::CommunicatorPtr comm);
};
GROOPS_REGISTER_PROGRAM(Gravityfield2EmpiricalCovariance, SINGLEPROCESS, "Estimate a empircal covariance function from time series", Gravityfield, Covariance)
/***********************************************/
void Gravityfield2EmpiricalCovariance::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
try
{
FileName outputName, fileNamePotCoeff;
UInt minDegree, maxDegree = INFINITYDEGREE;
Time time;
Double GM, R;
GravityfieldPtr gravityfield;
TimeSeriesPtr timeSeriesPtr, timesIntervalPtr;
Bool removeMean;
UInt delay;
SphericalHarmonicsNumberingPtr numbering;
Double factorFullMatrix, factorIsotropic;
readConfig(config, "outputfileCovarianceMatrix", outputName, Config::MUSTSET, "", "");
readConfig(config, "outputfilePotentialCoefficients", fileNamePotCoeff, Config::OPTIONAL, "", "");
readConfig(config, "gravityfield", gravityfield, Config::MUSTSET, "", "");
readConfig(config, "minDegree", minDegree, Config::DEFAULT, "2", "");
readConfig(config, "maxDegree", maxDegree, Config::MUSTSET, "", "");
readConfig(config, "GM", GM, Config::DEFAULT, STRING_DEFAULT_GM, "Geocentric gravitational constant");
readConfig(config, "R", R, Config::DEFAULT, STRING_DEFAULT_R, "reference radius");
readConfig(config, "numbering", numbering, Config::MUSTSET, "", "numbering scheme for solution vector");
readConfig(config, "removeMean", removeMean, Config::DEFAULT, "1", "");
readConfig(config, "timeSeries", timeSeriesPtr, Config::MUSTSET, "", "sampling of the gravityfield");
readConfig(config, "differenceStep", delay, Config::DEFAULT, "0", "choose dt for: x,i(t) - x,j(t+dt)");
readConfig(config, "factorFullMatrixPart", factorFullMatrix, Config::DEFAULT, "1", "");
readConfig(config, "factorIsotropicPart", factorIsotropic, Config::DEFAULT, "0.1", "");
readConfig(config, "intervals", timesIntervalPtr, Config::DEFAULT, "", "");
if(isCreateSchema(config)) return;
// ============================
// init time intervals
// -------------------
std::vector<Time> timeSeries = timeSeriesPtr->times();
std::vector<Time> timesInterval;
if(timesIntervalPtr)
timesInterval = timesIntervalPtr->times();
if(timesInterval.size()==0)
{
timesInterval.push_back(timeSeries.at(0));
timesInterval.push_back(timeSeries.back()+seconds2time(1));
}
// ============================
// numbering of covariance matrix
// ------------------------------
std::vector< std::vector<UInt> > idxC, idxS;
numbering->numbering(maxDegree, minDegree, idxC, idxS);
const UInt dim = numbering->parameterCount(maxDegree, minDegree);
Matrix CovFull(dim, dim);
logStatus<<"estimate covariance covariance function in each interval"<<Log::endl;
UInt countTotal = 0;
Single::forEach(timesInterval.size()-1, [&](UInt idInterval)
{
// init time series
// ----------------
std::vector<Time> times;
for(UInt i=0; i<timeSeries.size(); i++)
if(timeSeries.at(i).isInInterval(timesInterval.at(idInterval), timesInterval.at(idInterval+1)))
times.push_back(timeSeries.at(i));
// create time series of potential coefficients
// --------------------------------------------
UInt count = times.size();
std::vector<SphericalHarmonics> harm(count);
for(UInt i=0; i<count; i++)
{
harm.at(i) = gravityfield->sphericalHarmonics(times.at(i), maxDegree, minDegree, GM, R);
maxDegree = harm.at(i).maxDegree();
}
// remove temporal mean
// --------------------
if(removeMean)
{
SphericalHarmonics harmMean;
for(UInt i=0; i<count; i++)
harmMean += harm.at(i);
harmMean *= 1./count;
for(UInt i=0; i<count; i++)
harm.at(i) -= harmMean;
}
// covariance function
// -------------------
count = harm.size()-delay;
for(UInt i=0; i<count; i++)
{
Matrix cnm1 = harm.at(i).cnm();
Matrix snm1 = harm.at(i).snm();
Matrix cnm2 = harm.at(i+delay).cnm();
Matrix snm2 = harm.at(i+delay).snm();
// sort coefficients into vectors
Vector x1(dim), x2(dim);
for(UInt n=0; n<=maxDegree; n++)
{
if(idxC[n][0]!=NULLINDEX) x1(idxC[n][0]) = cnm1(n,0);
if(idxC[n][0]!=NULLINDEX) x2(idxC[n][0]) = cnm2(n,0);
for(UInt m=1; m<=n; m++)
{
if(idxC[n][m]!=NULLINDEX) x1(idxC[n][m]) = cnm1(n,m);
if(idxC[n][m]!=NULLINDEX) x2(idxC[n][m]) = cnm2(n,m);
if(idxS[n][m]!=NULLINDEX) x1(idxS[n][m]) = snm1(n,m);
if(idxS[n][m]!=NULLINDEX) x2(idxS[n][m]) = snm2(n,m);
}
}
matMult(1., x1, x2.trans(), CovFull);
}
countTotal += count;
if(removeMean)
countTotal -= 1;
});
// ============================
if(delay==0)
CovFull.setType(Matrix::SYMMETRIC);
CovFull *= 1./countTotal;
Matrix Cov = CovFull;
Cov *= factorFullMatrix;
if(factorIsotropic!=0)
{
Vector kn(maxDegree+1);
for(UInt n=0; n<=maxDegree; n++)
{
if(idxC[n][0]!=NULLINDEX) kn(n) += CovFull(idxC[n][0],idxC[n][0]);
for(UInt m=1; m<=n; m++)
{
if(idxC[n][m]!=NULLINDEX) kn(n) += CovFull(idxC[n][m],idxC[n][m]);
if(idxS[n][m]!=NULLINDEX) kn(n) += CovFull(idxS[n][m],idxS[n][m]);
}
}
for(UInt n=0; n<=maxDegree; n++)
{
if(idxC[n][0]!=NULLINDEX) Cov(idxC[n][0],idxC[n][0]) += factorIsotropic * kn(n)/(2*n+1);
for(UInt m=1; m<=n; m++)
{
if(idxC[n][m]!=NULLINDEX) Cov(idxC[n][m],idxC[n][m]) += factorIsotropic * kn(n)/(2*n+1);
if(idxS[n][m]!=NULLINDEX) Cov(idxS[n][m],idxS[n][m]) += factorIsotropic * kn(n)/(2*n+1);
}
}
}
// ============================
// Write
// -----
logStatus << "save covariance matrix to <"<<outputName<<">"<< Log::endl;
if(delay == 0)
Cov.setType(Matrix::SYMMETRIC);
writeFileMatrix(outputName, Cov);
logInfo<<" number of fields : "<<countTotal<<Log::endl;
logInfo<<" minDegree : "<<minDegree<< Log::endl;
logInfo<<" maxDegree : "<<maxDegree<< Log::endl;
logInfo<<" size of state vector : "<<dim<< Log::endl;
logInfo<<" number of unknowns : "<<dim*dim<< Log::endl;
logInfo<<" redundancy : "<<countTotal/static_cast<Double>(dim) << Log::endl;
// Spherical Harmonics
// -------------------
if(!fileNamePotCoeff.empty())
{
logStatus<<"save potential coefficients to <"<<fileNamePotCoeff<<">"<< Log::endl;
Matrix cnm(maxDegree+1, Matrix::SYMMETRIC);
Matrix snm(maxDegree+1, Matrix::SYMMETRIC);
Matrix sigma2cnm(maxDegree+1, Matrix::SYMMETRIC);
Matrix sigma2snm(maxDegree+1, Matrix::SYMMETRIC);
for(UInt n=0; n<=maxDegree; n++)
{
if(idxC[n][0]!=NULLINDEX) cnm(n,0) = sqrt(Cov(idxC[n][0],idxC[n][0]));
if(idxC[n][0]!=NULLINDEX) sigma2cnm(n,0) = Cov(idxC[n][0],idxC[n][0]);
for(UInt m=1; m<=n; m++)
{
if(idxC[n][m]!=NULLINDEX) cnm(n,m) = sqrt(Cov(idxC[n][m],idxC[n][m]));
if(idxS[n][m]!=NULLINDEX) snm(n,m) = sqrt(Cov(idxS[n][m],idxS[n][m]));
if(idxC[n][m]!=NULLINDEX) sigma2cnm(n,m) = Cov(idxC[n][m],idxC[n][m]);
if(idxS[n][m]!=NULLINDEX) sigma2snm(n,m) = Cov(idxS[n][m],idxS[n][m]);
}
}
writeFileSphericalHarmonics(fileNamePotCoeff, SphericalHarmonics(GM, R, cnm, snm, sigma2cnm, sigma2snm));
}
// Test for positive definite
// --------------------------
try
{
if(delay==0)
cholesky(Cov);
}
catch(std::exception &/*e*/)
{
logWarning<<"covariance matrix is not positive definite!"<<Log::endl;
}
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
|