FOX/ObjCryst++  1.10.X (development)
PowderPatternBackgroundBayesianMinimiser.cpp
1 /* ObjCryst++ Object-Oriented Crystallographic Library
2  (c) 2000-2002 Vincent Favre-Nicolin vincefn@users.sourceforge.net
3  2000-2001 University of Geneva (Switzerland)
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 /* PowderPatternBackgroundBayesianMinimiser.h
20 * Bayesian estimation of powder pattern background
21 *
22 */
23 
24 #include "ObjCryst/ObjCryst/PowderPatternBackgroundBayesianMinimiser.h"
25 namespace ObjCryst
26 {
27 PowderPatternBackgroundBayesianMinimiser::
28  PowderPatternBackgroundBayesianMinimiser(PowderPatternBackground &backgd):
29 mpBackground(&backgd)
30 {
31  this->AddSubRefObj(*mpBackground);
32 }
33 
34 PowderPatternBackgroundBayesianMinimiser::~PowderPatternBackgroundBayesianMinimiser()
35 {
36  this->RemoveSubRefObj(*mpBackground);
37 }
38 
40 {
41  static string className="PowderPatternBackgroundBayesianMinimiser";
42  return className;
43 }
44 
46 {
47  TAU_PROFILE("PowderPatternBackgroundBayesianMinimiser::GetLogLikelihood()","void ()",TAU_DEFAULT);
48  REAL llk=0;
49  const long nb=mpBackground->GetPowderPatternCalc().numElements();
50  const long step=1;
51  {// Calc (obs-calc)/sigma
52  const REAL *pBackgd=mpBackground->GetPowderPatternCalc().data();
53  const REAL *pObs=mpBackground->GetParentPowderPattern().GetPowderPatternObs().data();
54  const REAL *pSigma=mpBackground->GetParentPowderPattern().GetPowderPatternObsSigma().data();
55  for(long i=0;i<nb;i+=step)
56  {
57  if(*pSigma>0)
58  {
61  ((*pObs-*pBackgd) / (1.4142135623730951**pSigma));
62  }
63  pObs+=step;
64  pBackgd+=step;
65  pSigma+=step;
66  }
67  }
68  return llk;
69 }
70 
72 
73 const CrystVector_REAL& PowderPatternBackgroundBayesianMinimiser::GetLSQCalc (const unsigned int id) const
74 {
75  const long nb=mpBackground->GetPowderPatternCalc().numElements();
76  const long step=1;
77  if(mBayesianCalc.numElements()!=nb) mBayesianCalc.resize(nb);
78 
79  const REAL *pBackgd=mpBackground->GetPowderPatternCalc().data();
80  const REAL *pObs=mpBackground->GetParentPowderPattern().GetPowderPatternObs().data();
81  const REAL *pSigma=mpBackground->GetParentPowderPattern().GetPowderPatternObsSigma().data();
82  REAL *pBayesCalc=mBayesianCalc.data();
83  for(long i=0;i<nb;i+=step)
84  {
85  if(*pSigma>0)
86  {
87  *pBayesCalc = PowderPatternBackgroundBayesianMinimiser::BayesianBackgroundLogLikelihood((*pObs-*pBackgd) / (1.4142135623730951**pSigma));
88  }
89  else *pBayesCalc =0;
90  pObs+=step;
91  pBackgd+=step;
92  pSigma+=step;
93  pBayesCalc+=step;
94  }
95  return mBayesianCalc;
96 }
97 
98 const CrystVector_REAL& PowderPatternBackgroundBayesianMinimiser::GetLSQObs (const unsigned int) const
99 {
100  const long nb=mpBackground->GetPowderPatternCalc().numElements();
101  if(mBayesianObs.numElements()!=nb)
102  {
103  mBayesianObs.resize(nb);
104  mBayesianObs=0;
105  }
106  return mBayesianObs;
107 }
108 
109 const CrystVector_REAL& PowderPatternBackgroundBayesianMinimiser::GetLSQWeight (const unsigned int) const
110 {
111  const long nb=mpBackground->GetPowderPatternCalc().numElements();
112  if(mBayesianWeight.numElements()!=nb)
113  {
114  mBayesianWeight.resize(nb);
115  mBayesianWeight=1;
116  }
117  return mBayesianWeight;
118 }
119 
121 {
122  static const REAL vllk[11]={0.00000000e+00,
123  1e-4,
124  1.77123249e-03,
125  1.00997634e+00,
126  2.89760310e+00,
127  3.61881096e+00,
128  3.93024374e+00,
129  4.16063018e+00,
130  4.34600620e+00,
131  4.50155649e+00,
132  4.63573160e+00};
133  static const REAL vt[11]={ 0. , 0.01, 0.1 , 1.1 , 2.1 , 3.1 , 4.1 , 5.1 , 6.1 , 7.1 , 8.1};
134  static const CubicSpline spline(vt,vllk,11);
135  static const REAL s1=spline(8)-log((REAL)8);
136  if(t<=0) return 5*t*t;
137  if(t<8)return spline(t);
138  return s1+log(t);
139 }
140 
141 
142 }//namespace
void AddSubRefObj(RefinableObj &)
virtual const string & GetClassName() const
Name for this class ("RefinableObj", "Crystal",...).
const CrystVector_REAL & GetPowderPatternObsSigma() const
Get the sigma for each point of the observed powder pattern.
virtual REAL GetLogLikelihood() const
Get -log(likelihood) of the current configuration for the object.
void RemoveSubRefObj(RefinableObj &)
virtual unsigned int GetNbLSQFunction() const
Number of LSQ functions.
virtual const CrystVector_REAL & GetLSQWeight(const unsigned int) const
Get the weight values for the LSQ function.
static REAL BayesianBackgroundLogLikelihood(const REAL t)
Returns the log(likelihood) of a background by marginalizing the effect of Bragg peaks, following the method described by David and Sivia (J.Appl.Cryst.
CrystVector_REAL mBayesianCalc
Bayesian cost (-log(likelihood)) for each point.
CrystVector_REAL mBayesianObs
Obs==0 (desired -log(likelihood))
const CrystVector_REAL & GetPowderPatternObs() const
Get the observed powder pattern.
virtual const CrystVector_REAL & GetLSQCalc(const unsigned int) const
Get the current calculated value for the LSQ function.
Cubic spline interpolation.
Definition: CrystVector.h:564
virtual const CrystVector_REAL & GetLSQObs(const unsigned int) const
Get the observed values for the LSQ function.
The namespace which includes all objects (crystallographic and algorithmic) in ObjCryst++.
Definition: Atom.cpp:47
const PowderPattern & GetParentPowderPattern() const
Get the PowderPattern object which uses this component.
virtual const CrystVector_REAL & GetPowderPatternCalc() const
Get the calculated powder pattern for this component.