Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-2021 The VES code team 3 : (see the PEOPLE-VES file at the root of this folder for a list of names) 4 : 5 : See http://www.ves-code.org for more information. 6 : 7 : This file is part of VES code module. 8 : 9 : The VES code module is free software: you can redistribute it and/or modify 10 : it under the terms of the GNU Lesser General Public License as published by 11 : the Free Software Foundation, either version 3 of the License, or 12 : (at your option) any later version. 13 : 14 : The VES code module is distributed in the hope that it will be useful, 15 : but WITHOUT ANY WARRANTY; without even the implied warranty of 16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 : GNU Lesser General Public License for more details. 18 : 19 : You should have received a copy of the GNU Lesser General Public License 20 : along with the VES code module. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : 23 : #include "TargetDistribution.h" 24 : 25 : #include "core/ActionRegister.h" 26 : 27 : 28 : namespace PLMD { 29 : namespace ves { 30 : 31 : //+PLUMEDOC VES_TARGETDIST TD_CHISQUARED 32 : /* 33 : Chi-squared distribution (static). 34 : 35 : Employ a target distribution given by a 36 : [chi-squared distribution](https://en.wikipedia.org/wiki/Chi-squared_distribution) 37 : that is defined as 38 : 39 : $$ 40 : p(s) = 41 : \frac 42 : {1} 43 : {\sigma \, 2^{\frac{k}{2}} \, \Gamma\left(\frac{k}{2}\right) } 44 : \, \left(\frac{s-a}{\sigma}\right)^{\frac{k}{2}-1} \, \exp\left(- \frac{1}{2} 45 : \left(\frac{s-a}{\sigma}\right) \right), 46 : $$ 47 : 48 : where $a$ is the minimum of the distribution that is defined on the interval $[a,\infty)$, 49 : the parameter $k$ (given as a positive integer larger than 2) determines how far 50 : the peak of the distribution is from the minimum (known as the "degrees of freedom"), 51 : and the parameter $\sigma>0$ determines the broadness of the distribution. 52 : 53 : The minimum $a$ is given using the MINIMUM keyword, the parameter $k$ is given 54 : using the KAPPA keyword, and the parameter $\sigma$ is given using the SIGMA keyword. 55 : 56 : This target distribution action is only defined for one dimension, for multiple dimensions 57 : it should be used in combination with the [TD_PRODUCT_DISTRIBUTION](TD_PRODUCT_DISTRIBUTION.md) action. 58 : 59 : ## Examples 60 : 61 : Chi-squared distribution with $a=-10.0$, $\sigma=2.0$, and $k=2$ 62 : 63 : ```plumed 64 : td: TD_CHISQUARED MINIMUM=-10.0 SIGMA=2.0 KAPPA=2 65 : ``` 66 : 67 : The Chi-squared distribution is only defined for one dimension so for multiple 68 : dimensions we have to use it in combination with the [TD_PRODUCT_DISTRIBUTION](TD_PRODUCT_DISTRIBUTION.md) action as shown in 69 : the following example where we have a Chi-squared distribution for argument 1 70 : and uniform distribution for argument 2 71 : 72 : ```plumed 73 : td_chisq: TD_CHISQUARED MINIMUM=10.0 SIGMA=2.0 KAPPA=2 74 : 75 : td_uni: TD_UNIFORM 76 : 77 : td_pd: TD_PRODUCT_DISTRIBUTION DISTRIBUTIONS=td_chisq,td_uni 78 : ``` 79 : 80 : */ 81 : //+ENDPLUMEDOC 82 : 83 : class TD_ChiSquared: public TargetDistribution { 84 : std::vector<double> minima_; 85 : std::vector<double> sigma_; 86 : std::vector<double> kappa_; 87 : std::vector<double> normalization_; 88 : public: 89 : static void registerKeywords(Keywords&); 90 : explicit TD_ChiSquared(const ActionOptions& ao); 91 : double getValue(const std::vector<double>&) const override; 92 : }; 93 : 94 : 95 : PLUMED_REGISTER_ACTION(TD_ChiSquared,"TD_CHISQUARED") 96 : 97 : 98 11 : void TD_ChiSquared::registerKeywords(Keywords& keys) { 99 11 : TargetDistribution::registerKeywords(keys); 100 11 : keys.add("compulsory","MINIMUM","The minimum of the chi-squared distribution."); 101 11 : keys.add("compulsory","SIGMA","The sigma parameter of the chi-squared distribution given as a positive number."); 102 11 : keys.add("compulsory","KAPPA","The k parameter of the chi-squared distribution given as positive integer larger than 2."); 103 11 : keys.use("WELLTEMPERED_FACTOR"); 104 11 : keys.use("SHIFT_TO_ZERO"); 105 11 : keys.use("NORMALIZE"); 106 11 : } 107 : 108 : 109 9 : TD_ChiSquared::TD_ChiSquared(const ActionOptions& ao): 110 : PLUMED_VES_TARGETDISTRIBUTION_INIT(ao), 111 18 : minima_(0), 112 9 : sigma_(0), 113 9 : kappa_(0), 114 18 : normalization_(0) { 115 9 : parseVector("MINIMUM",minima_); 116 9 : parseVector("SIGMA",sigma_); 117 18 : for(unsigned int k=0; k<sigma_.size(); k++) { 118 9 : if(sigma_[k] < 0.0) { 119 0 : plumed_merror(getName()+": the value given in SIGMA should be positive."); 120 : } 121 : } 122 : 123 9 : std::vector<unsigned int> kappa_int(0); 124 18 : parseVector("KAPPA",kappa_int); 125 9 : if(kappa_int.size()==0) { 126 0 : plumed_merror(getName()+": some problem with KAPPA keyword, should given as positive integer larger than 2"); 127 : } 128 9 : kappa_.resize(kappa_int.size()); 129 18 : for(unsigned int k=0; k<kappa_int.size(); k++) { 130 9 : if(kappa_int[k] < 2) { 131 0 : plumed_merror(getName()+": KAPPA should be an integer 2 or higher"); 132 : } 133 9 : kappa_[k] = static_cast<double>(kappa_int[k]); 134 : } 135 : 136 9 : setDimension(minima_.size()); 137 9 : if(getDimension()>1) { 138 0 : plumed_merror(getName()+": only defined for one dimension, for multiple dimensions it should be used in combination with the TD_PRODUCT_DISTRIBUTION action."); 139 : } 140 9 : if(sigma_.size()!=getDimension()) { 141 0 : plumed_merror(getName()+": the SIGMA keyword does not match the given dimension in MINIMUM"); 142 : } 143 9 : if(kappa_.size()!=getDimension()) { 144 0 : plumed_merror(getName()+": the KAPPA keyword does not match the given dimension in MINIMUM"); 145 : } 146 : 147 9 : normalization_.resize(getDimension()); 148 18 : for(unsigned int k=0; k<getDimension(); k++) { 149 9 : normalization_[k] = 1.0/(pow(2.0,0.5*kappa_[k])*tgamma(0.5*kappa_[k])*sigma_[k]); 150 : } 151 9 : checkRead(); 152 9 : } 153 : 154 : 155 1509 : double TD_ChiSquared::getValue(const std::vector<double>& argument) const { 156 : double value = 1.0; 157 3018 : for(unsigned int k=0; k<argument.size(); k++) { 158 1509 : double arg=(argument[k]-minima_[k])/sigma_[k]; 159 1509 : if(arg<0.0) { 160 0 : plumed_merror(getName()+": the chi-squared istribution is not defined for values less that ones given in MINIMUM"); 161 : } 162 1509 : value *= normalization_[k] * pow(arg,0.5*kappa_[k]-1.0) * exp(-0.5*arg); 163 : } 164 1509 : return value; 165 : } 166 : 167 : 168 : } 169 : }