Skip to content

Commit

Permalink
Improve calculation of R-factor. Refs #6747.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Apr 4, 2013
1 parent 95d0e93 commit 0c6fdf5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
12 changes: 11 additions & 1 deletion Code/Mantid/Framework/Kernel/inc/MantidKernel/Statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ namespace Mantid
/// standard_deviation of the values
double standard_deviation;
};

/** R factor for powder data analysis
*/
struct Rfactor
{
/// Rwp
double Rwp;
/// Rp
double Rp;
};

/// Return a statistics object for the given data set
template<typename TYPE>
Expand All @@ -63,7 +73,7 @@ namespace Mantid
template<typename TYPE>
std::vector<double> getModifiedZscore(const std::vector<TYPE>& data, const bool sorted=false);
/// Return the R-factors (Rwp) of a diffraction pattern data
double MANTID_KERNEL_DLL getRFactor(const std::vector<double>& obsI, const std::vector<double>& calI, const std::vector<double>& obsE);
Rfactor MANTID_KERNEL_DLL getRFactor(const std::vector<double>& obsI, const std::vector<double>& calI, const std::vector<double>& obsE);

} // namespace Kernel
} // namespace Mantid
Expand Down
15 changes: 11 additions & 4 deletions Code/Mantid/Framework/Kernel/src/Statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ namespace Mantid
* @param obsI :: array of observed intensity values
* @param calI :: array of calculated intensity values;
* @param obsE :: array of error of the observed data;
* @return :: Rwp
* @return :: RFactor including Rp and Rwp
*
*/
double getRFactor(const std::vector<double>& obsI, const std::vector<double>& calI, const std::vector<double>& obsE)
Rfactor getRFactor(const std::vector<double>& obsI, const std::vector<double>& calI, const std::vector<double>& obsE)
{
// 1. Check
if (obsI.size() != calI.size() || obsI.size() != obsE.size())
Expand All @@ -241,6 +241,8 @@ namespace Mantid

double sumnom = 0;
double sumdenom = 0;
double sumrpnom = 0;
double sumrpdenom = 0;

size_t numpts = obsI.size();
for (size_t i = 0; i < numpts; ++i)
Expand All @@ -251,13 +253,18 @@ namespace Mantid
double weight = 1.0/(sigma*sigma);
double diff = obs_i - cal_i;

sumrpnom += fabs(diff);
sumrpdenom += fabs(cal_i);

sumnom += weight*diff*diff;
sumdenom += weight*obs_i*obs_i;
}

double rwp = std::sqrt(sumnom/sumdenom);
Rfactor rfactor;
rfactor.Rp = std::sqrt(sumrpnom/sumrpdenom);
rfactor.Rwp = std::sqrt(sumnom/sumdenom);

return rwp;
return rfactor;
}


Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/Kernel/test/StatisticsTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ class StatisticsTest : public CxxTest::TestSuite
calY[3] = 1.3;
obsE[3] = 1.0;

double rwp = getRFactor(obsY, calY, obsE);
Rfactor rfactor = getRFactor(obsY, calY, obsE);

TS_ASSERT_DELTA(rwp, 0.1582, 0.0001);
TS_ASSERT_DELTA(rfactor.Rwp, 0.1582, 0.0001);
}

/** Test throw exception
Expand Down

0 comments on commit 0c6fdf5

Please sign in to comment.