ROL
ROL_LogQuantileQuadrangle.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_LOGQUANTILEQUAD_HPP
45 #define ROL_LOGQUANTILEQUAD_HPP
46 
47 #include "ROL_ExpectationQuad.hpp"
48 #include "ROL_PlusFunction.hpp"
49 
50 namespace ROL {
51 
52 template<class Real>
53 class LogQuantileQuadrangle : public ExpectationQuad<Real> {
54 private:
55 
56  Teuchos::RCP<PlusFunction<Real> > pf_;
57 
58  Real prob_;
59  Real scale_;
60  Real eps_;
61 
62 public:
63 
64  LogQuantileQuadrangle(Real prob, Real scale, Real eps, Teuchos::RCP<PlusFunction<Real> > &pf )
65  : ExpectationQuad<Real>(), pf_(pf) {
66  prob_ = ((prob >= 0.0) ? ((prob <= 1.0) ? prob : 0.5) : 0.5);
67  scale_ = ((scale >= 1.0) ? scale : 1.0);
68  eps_ = ((eps > 0.0) ? eps : 1.0);
69  }
70 
71  LogQuantileQuadrangle(Teuchos::ParameterList &parlist) : ExpectationQuad<Real>() {
72  Teuchos::ParameterList& list
73  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Log-Quantile Quadrangle");
74  // Check CVaR inputs
75  Real prob = list.get("Confidence Level",0.5);
76  prob_ = ((prob >= 0.0) ? ((prob <= 1.0) ? prob : 0.5) : 0.5);
77  Real scale = list.get("Growth Constant",1.0);
78  scale_ = ((scale >= 1.0) ? scale : 1.0);
79  // Build plus function
80  pf_ = Teuchos::rcp( new PlusFunction<Real>(list) );
81  Real eps = list.get("Smoothing Parameter",1.);
82  eps_ = ((eps > 0.) ? eps : 1.);
83  }
84 
85  Real error(Real x, int deriv = 0) {
86  TEUCHOS_TEST_FOR_EXCEPTION( (deriv > 2), std::invalid_argument,
87  ">>> ERROR (ROL::LogQuantileQuadrangle::error): deriv greater than 2!");
88  TEUCHOS_TEST_FOR_EXCEPTION( (deriv < 0), std::invalid_argument,
89  ">>> ERROR (ROL::LogQuantileQuadrangle::error): deriv less than 0!");
90 
91  Real X = ((deriv == 0) ? x : ((deriv == 1) ? 1.0 : 0.0));
92  return regret(x,deriv) - X;
93  }
94 
95  Real regret(Real x, int deriv = 0) {
96  TEUCHOS_TEST_FOR_EXCEPTION( (deriv > 2), std::invalid_argument,
97  ">>> ERROR (ROL::LogQuantileQuadrangle::regret): deriv greater than 2!");
98  TEUCHOS_TEST_FOR_EXCEPTION( (deriv < 0), std::invalid_argument,
99  ">>> ERROR (ROL::LogQuantileQuadrangle::regret): deriv less than 0!");
100 
101  Real arg = std::exp(scale_*x);
102  Real sarg = scale_*arg;
103  Real reg = 1.0/(1.0-prob_) * (pf_->evaluate(arg-1.0,deriv) *
104  ((deriv == 0) ? 1.0 : ((deriv == 1) ? sarg : sarg*sarg))
105  + ((deriv == 2) ? pf_->evaluate(arg-1.0,deriv-1)*scale_*sarg : 0.0));
106  return reg;
107  }
108 
109  void checkRegret(void) {
111  // Check v'(eps)
112  Real x = eps_;
113  Real vx = 0.0, vy = 0.0;
114  Real dv = regret(x,1);
115  Real t = 1.0;
116  Real diff = 0.0;
117  Real err = 0.0;
118  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(eps) is correct? \n";
119  std::cout << std::right << std::setw(20) << "t"
120  << std::setw(20) << "v'(x)"
121  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
122  << std::setw(20) << "Error"
123  << "\n";
124  for (int i = 0; i < 13; i++) {
125  vy = regret(x+t,0);
126  vx = regret(x-t,0);
127  diff = (vy-vx)/(2.0*t);
128  err = std::abs(diff-dv);
129  std::cout << std::scientific << std::setprecision(11) << std::right
130  << std::setw(20) << t
131  << std::setw(20) << dv
132  << std::setw(20) << diff
133  << std::setw(20) << err
134  << "\n";
135  t *= 0.1;
136  }
137  std::cout << "\n";
138  // check v''(eps)
139  vx = 0.0;
140  vy = 0.0;
141  dv = regret(x,2);
142  t = 1.0;
143  diff = 0.0;
144  err = 0.0;
145  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(eps) is correct? \n";
146  std::cout << std::right << std::setw(20) << "t"
147  << std::setw(20) << "v''(x)"
148  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
149  << std::setw(20) << "Error"
150  << "\n";
151  for (int i = 0; i < 13; i++) {
152  vy = regret(x+t,1);
153  vx = regret(x-t,1);
154  diff = (vy-vx)/(2.0*t);
155  err = std::abs(diff-dv);
156  std::cout << std::scientific << std::setprecision(11) << std::right
157  << std::setw(20) << t
158  << std::setw(20) << dv
159  << std::setw(20) << diff
160  << std::setw(20) << err
161  << "\n";
162  t *= 0.1;
163  }
164  std::cout << "\n";
165  // Check v'(0)
166  x = 0.0;
167  vx = 0.0;
168  vy = 0.0;
169  dv = regret(x,1);
170  t = 1.0;
171  diff = 0.0;
172  err = 0.0;
173  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(0) is correct? \n";
174  std::cout << std::right << std::setw(20) << "t"
175  << std::setw(20) << "v'(x)"
176  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
177  << std::setw(20) << "Error"
178  << "\n";
179  for (int i = 0; i < 13; i++) {
180  vy = regret(x+t,0);
181  vx = regret(x-t,0);
182  diff = (vy-vx)/(2.0*t);
183  err = std::abs(diff-dv);
184  std::cout << std::scientific << std::setprecision(11) << std::right
185  << std::setw(20) << t
186  << std::setw(20) << dv
187  << std::setw(20) << diff
188  << std::setw(20) << err
189  << "\n";
190  t *= 0.1;
191  }
192  std::cout << "\n";
193  // check v''(eps)
194  vx = 0.0;
195  vy = 0.0;
196  dv = regret(x,2);
197  t = 1.0;
198  diff = 0.0;
199  err = 0.0;
200  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(0) is correct? \n";
201  std::cout << std::right << std::setw(20) << "t"
202  << std::setw(20) << "v''(x)"
203  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
204  << std::setw(20) << "Error"
205  << "\n";
206  for (int i = 0; i < 13; i++) {
207  vy = regret(x+t,1);
208  vx = regret(x-t,1);
209  diff = (vy-vx)/(2.0*t);
210  err = std::abs(diff-dv);
211  std::cout << std::scientific << std::setprecision(11) << std::right
212  << std::setw(20) << t
213  << std::setw(20) << dv
214  << std::setw(20) << diff
215  << std::setw(20) << err
216  << "\n";
217  t *= 0.1;
218  }
219  std::cout << "\n";
220  // Check v'(0)
221  x = -eps_;
222  vx = 0.0;
223  vy = 0.0;
224  dv = regret(x,1);
225  t = 1.0;
226  diff = 0.0;
227  err = 0.0;
228  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-eps) is correct? \n";
229  std::cout << std::right << std::setw(20) << "t"
230  << std::setw(20) << "v'(x)"
231  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
232  << std::setw(20) << "Error"
233  << "\n";
234  for (int i = 0; i < 13; i++) {
235  vy = regret(x+t,0);
236  vx = regret(x-t,0);
237  diff = (vy-vx)/(2.0*t);
238  err = std::abs(diff-dv);
239  std::cout << std::scientific << std::setprecision(11) << std::right
240  << std::setw(20) << t
241  << std::setw(20) << dv
242  << std::setw(20) << diff
243  << std::setw(20) << err
244  << "\n";
245  t *= 0.1;
246  }
247  std::cout << "\n";
248  // check v''(eps)
249  vx = 0.0;
250  vy = 0.0;
251  dv = regret(x,2);
252  t = 1.0;
253  diff = 0.0;
254  err = 0.0;
255  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(-eps) is correct? \n";
256  std::cout << std::right << std::setw(20) << "t"
257  << std::setw(20) << "v''(x)"
258  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
259  << std::setw(20) << "Error"
260  << "\n";
261  for (int i = 0; i < 13; i++) {
262  vy = regret(x+t,1);
263  vx = regret(x-t,1);
264  diff = (vy-vx)/(2.0*t);
265  err = std::abs(diff-dv);
266  std::cout << std::scientific << std::setprecision(11) << std::right
267  << std::setw(20) << t
268  << std::setw(20) << dv
269  << std::setw(20) << diff
270  << std::setw(20) << err
271  << "\n";
272  t *= 0.1;
273  }
274  std::cout << "\n";
275  }
276 
277 };
278 
279 }
280 #endif
virtual void checkRegret(void)
Teuchos::RCP< PlusFunction< Real > > pf_
LogQuantileQuadrangle(Teuchos::ParameterList &parlist)
LogQuantileQuadrangle(Real prob, Real scale, Real eps, Teuchos::RCP< PlusFunction< Real > > &pf)
Real regret(Real x, int deriv=0)