omegatest: Use test_scriptindex_error in another case
[xapian.git] / xapian-core / weight / dlhweight.cc
blob86b1357be9d490f746a2a17787f5c8ab5e46c891
1 /** @file
2 * @brief Xapian::DLHWeight class - The DLH weighting scheme of the DFR framework.
3 */
4 /* Copyright (C) 2013, 2014 Aarsh Shah
5 * Copyright (C) 2016,2017,2019 Olly Betts
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <config.h>
24 #include "xapian/weight.h"
26 #include "xapian/error.h"
27 #include "common/log2.h"
28 #include <algorithm>
30 using namespace std;
32 namespace Xapian {
34 DLHWeight *
35 DLHWeight::clone() const
37 return new DLHWeight();
40 void
41 DLHWeight::init(double factor)
43 // Avoid warnings about unused private member.
44 (void)lower_bound;
46 if (factor == 0.0) {
47 // This object is for the term-independent contribution, and that's
48 // always zero for this scheme.
49 return;
52 double wdf_upper = get_wdf_upper_bound();
53 if (wdf_upper == 0) {
54 upper_bound = 0.0;
55 return;
58 const double wdf_lower = 1.0;
59 double len_upper = get_doclength_upper_bound();
60 double len_lower = get_doclength_lower_bound();
62 double F = get_collection_freq();
64 // Calculate constant values to be used in get_sumpart().
65 log_constant = get_total_length() / F;
66 wqf_product_factor = get_wqf() * factor;
68 // Calculate values for the upper bound.
70 // w <= l, so if the allowed ranges overlap, max w/l is 1.0.
71 double max_wdf_over_l = wdf_upper < len_lower ? wdf_upper / len_lower : 1.0;
73 // First term A: w/(w+.5)*log2(w/l*L) where L=total_len/coll_freq
74 // Assume log >= 0:
75 // w/(w+.5) = 1-1/(2w+1) and w >= 1 so max A at w=w_max
76 // log2(w/l*L) maximised when w/l maximised
77 // so max A at w=w_max, l=max(l_min, w_max)
78 // If log < 0 => then A <= 0, so max A <= 0 and we want to minimise the
79 // factor outside the log.
80 double logged_expr = max_wdf_over_l * log_constant;
81 double w_for_A = logged_expr > 1.0 ? wdf_upper : wdf_lower;
82 double A = w_for_A / (w_for_A + 0.5) * log2(logged_expr);
84 // Second term B:
86 // (l-w)*log2(1-w/l)
88 // The log is negative, and w <= l so B <= 0 and its maximum is the value
89 // as close to zero as possible. So smaller (l-w) is better and smaller
90 // w/l is better.
92 // This function is ill defined at w=l, but -> 0 as w -> l.
94 // If w=l is valid (i.e. len_lower > wdf_upper) then B = 0.
95 double B = 0;
96 if (len_lower > wdf_upper) {
97 // If not, then minimising l-w gives us a candidate (i.e. w=wdf_upper
98 // and l=len_lower).
100 // The function is also 0 at w = 0 (there must be a local mimina at
101 // some value of w between 0 and l), so the other candidate is at
102 // w=wdf_lower.
104 // We need to find the optimum value of l in this case, so
105 // differentiate the formula by l:
107 // d/dl: log2(1-w/l) + (l-w)*(1-w/l)/(l*log(2))
108 // = (log(1-w/l) + (1-w/l)²)/log(2)
109 // = (log(x) + x²)/log(2) [x=1-w/l]
111 // which is 0 at approx x=0.65291864
113 // x=1-w/l <=> l*(1-x)=w <=> l=w/(1-x) <=> l ~= 0.34708136*w
115 // but l >= w so we can't attain that (and the log isn't valid there).
117 // Gradient at (without loss of generality) l=2*w is:
118 // (log(0.5) + 0.25)/log(2)
119 // which is < 0 so want to minimise l, i.e. l=len_lower, so the other
120 // candidate is w=wdf_lower and l=len_lower.
122 // So evaluate both candidates and pick the larger:
123 double B1 = (len_lower - wdf_lower) * log2(1.0 - wdf_lower / len_lower);
124 double B2 = (len_lower - wdf_upper) * log2(1.0 - wdf_upper / len_lower);
125 B = max(B1, B2);
128 /* An upper bound of the term used in the third log can be obtained by:
130 * 0.5 * log2(2.0 * M_PI * wdf * (1 - wdf / len))
132 * An upper bound on wdf * (1 - wdf / len) (and hence on the log, since
133 * log is a monotonically increasing function on positive numbers) can
134 * be obtained by plugging in the upper bound of the length and
135 * differentiating the term w.r.t wdf which gives the value of wdf at which
136 * the function attains maximum value - at wdf = len_upper / 2 (or if the
137 * wdf can't be that large, at wdf = wdf_upper): */
138 double wdf_var = min(wdf_upper, len_upper / 2.0);
139 double max_product = wdf_var * (1.0 - wdf_var / len_upper);
140 #if 0
141 /* An upper bound can also be obtained by taking the minimum and maximum
142 * wdf value in the formula as shown (which isn't useful now as it's always
143 * >= the bound above, but could be useful if we tracked bounds on wdf/len):
145 double min_wdf_to_len = wdf_lower / len_upper;
146 double max_product_2 = wdf_upper * (1.0 - min_wdf_to_len);
147 /* Take the minimum of the two upper bounds. */
148 max_product = min(max_product, max_product_2);
149 #endif
150 double C = 0.5 * log2(2.0 * M_PI * max_product) / (wdf_lower + 0.5);
151 upper_bound = A + B + C;
153 if (rare(upper_bound < 0.0))
154 upper_bound = 0.0;
155 else
156 upper_bound *= wqf_product_factor;
159 string
160 DLHWeight::name() const
162 return "Xapian::DLHWeight";
165 string
166 DLHWeight::serialise() const
168 return string();
171 DLHWeight *
172 DLHWeight::unserialise(const string& s) const
174 if (rare(!s.empty()))
175 throw Xapian::SerialisationError("Extra data in DLHWeight::unserialise()");
176 return new DLHWeight();
179 double
180 DLHWeight::get_sumpart(Xapian::termcount wdf, Xapian::termcount len,
181 Xapian::termcount) const
183 if (wdf == 0 || wdf == len) return 0.0;
185 double wdf_to_len = double(wdf) / len;
186 double one_minus_wdf_to_len = 1.0 - wdf_to_len;
188 double wt = wdf * log2(wdf_to_len * log_constant) +
189 (len - wdf) * log2(one_minus_wdf_to_len) +
190 0.5 * log2(2.0 * M_PI * wdf * one_minus_wdf_to_len);
191 if (rare(wt <= 0.0)) return 0.0;
193 return wqf_product_factor * wt / (wdf + 0.5);
196 double
197 DLHWeight::get_maxpart() const
199 return upper_bound;
202 double
203 DLHWeight::get_sumextra(Xapian::termcount, Xapian::termcount) const
205 return 0;
208 double
209 DLHWeight::get_maxextra() const
211 return 0;