ROL
ROL_MoreauYosidaCVaR.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_MOREAUYOSIDACVAR_HPP
45 #define ROL_MOREAUYOSIDACVAR_HPP
46 
47 #include "ROL_ExpectationQuad.hpp"
48 
102 namespace ROL {
103 
104 template<class Real>
105 class MoreauYosidaCVaR : public ExpectationQuad<Real> {
106 private:
107 
108  Real prob_;
109  Real eps_;
110 
111  Real omp_;
112  Real ub_;
113 
114  void checkInputs(void) const {
115  Real zero(0), one(1);
116  TEUCHOS_TEST_FOR_EXCEPTION((prob_ <= zero) || (prob_ >= one), std::invalid_argument,
117  ">>> ERROR (ROL::MoreauYosidaCVaR): Confidence level must be between 0 and 1!");
118  TEUCHOS_TEST_FOR_EXCEPTION((eps_ <= zero), std::invalid_argument,
119  ">>> ERROR (ROL::MoreauYosidaCVaR): Smoothing parameter must be positive!");
120  }
121 
122  void setParameters(void) {
123  Real one(1);
124  omp_ = one-prob_;
125  ub_ = eps_/omp_;
126  }
127 
128 public:
134  MoreauYosidaCVaR(Real prob, Real eps )
135  : ExpectationQuad<Real>(), prob_(prob), eps_(eps) {
136  checkInputs();
137  setParameters();
138  }
139 
149  MoreauYosidaCVaR(Teuchos::ParameterList &parlist)
150  : ExpectationQuad<Real>() {
151  Teuchos::ParameterList& list
152  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Moreau-Yosida CVaR");
153  prob_ = list.get<Real>("Confidence Level");
154  eps_ = list.get<Real>("Smoothing Parameter");
155  checkInputs();
156  setParameters();
157  }
158 
159  Real error(Real x, int deriv = 0) {
160  Real zero(0), one(1);
161  Real X = ((deriv==0) ? x : ((deriv==1) ? one : zero));
162  return regret(x,deriv) - X;
163  }
164 
165  Real regret(Real x, int deriv = 0) {
166  Real zero(0), half(0.5), one(1), reg(0);
167  int region = ((x <= 0) ? -1 : ((x >= ub_) ? 1 : 0));
168  if ( region == 0 ) {
169  reg = ((deriv == 0) ? half*x*x : ((deriv == 1) ? x : one));
170  reg /= eps_;
171  }
172  else if ( region == 1 ) {
173  reg = ((deriv == 0) ? (x-half*ub_) : ((deriv == 1) ? one : zero));
174  reg /= omp_;
175  }
176  return reg;
177  }
178 
179  void checkRegret(void) {
181  Real zero(0), one(1), two(2), p1(0.1);
182  // Check v'(eps)
183  Real x = eps_;
184  Real vx = zero, vy = zero;
185  Real dv = regret(x,1);
186  Real t = one;
187  Real diff = zero;
188  Real err = zero;
189  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(eps) is correct? \n";
190  std::cout << std::right << std::setw(20) << "t"
191  << std::setw(20) << "v'(x)"
192  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
193  << std::setw(20) << "Error"
194  << "\n";
195  for (int i = 0; i < 13; i++) {
196  vy = regret(x+t,0);
197  vx = regret(x-t,0);
198  diff = (vy-vx)/(two*t);
199  err = std::abs(diff-dv);
200  std::cout << std::scientific << std::setprecision(11) << std::right
201  << std::setw(20) << t
202  << std::setw(20) << dv
203  << std::setw(20) << diff
204  << std::setw(20) << err
205  << "\n";
206  t *= p1;
207  }
208  std::cout << "\n";
209  // check v''(eps)
210  vx = zero;
211  vy = zero;
212  dv = regret(x,2);
213  t = one;
214  diff = zero;
215  err = zero;
216  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(eps) is correct? \n";
217  std::cout << std::right << std::setw(20) << "t"
218  << std::setw(20) << "v''(x)"
219  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
220  << std::setw(20) << "Error"
221  << "\n";
222  for (int i = 0; i < 13; i++) {
223  vy = regret(x+t,1);
224  vx = regret(x-t,1);
225  diff = (vy-vx)/(two*t);
226  err = std::abs(diff-dv);
227  std::cout << std::scientific << std::setprecision(11) << std::right
228  << std::setw(20) << t
229  << std::setw(20) << dv
230  << std::setw(20) << diff
231  << std::setw(20) << err
232  << "\n";
233  t *= p1;
234  }
235  std::cout << "\n";
236  // Check v'(0)
237  x = zero;
238  vx = zero;
239  vy = zero;
240  dv = regret(x,1);
241  t = one;
242  diff = zero;
243  err = zero;
244  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(0) is correct? \n";
245  std::cout << std::right << std::setw(20) << "t"
246  << std::setw(20) << "v'(x)"
247  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
248  << std::setw(20) << "Error"
249  << "\n";
250  for (int i = 0; i < 13; i++) {
251  vy = regret(x+t,0);
252  vx = regret(x-t,0);
253  diff = (vy-vx)/(two*t);
254  err = std::abs(diff-dv);
255  std::cout << std::scientific << std::setprecision(11) << std::right
256  << std::setw(20) << t
257  << std::setw(20) << dv
258  << std::setw(20) << diff
259  << std::setw(20) << err
260  << "\n";
261  t *= p1;
262  }
263  std::cout << "\n";
264  // check v''(eps)
265  vx = zero;
266  vy = zero;
267  dv = regret(x,2);
268  t = one;
269  diff = zero;
270  err = zero;
271  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(0) is correct? \n";
272  std::cout << std::right << std::setw(20) << "t"
273  << std::setw(20) << "v''(x)"
274  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
275  << std::setw(20) << "Error"
276  << "\n";
277  for (int i = 0; i < 13; i++) {
278  vy = regret(x+t,1);
279  vx = regret(x-t,1);
280  diff = (vy-vx)/(two*t);
281  err = std::abs(diff-dv);
282  std::cout << std::scientific << std::setprecision(11) << std::right
283  << std::setw(20) << t
284  << std::setw(20) << dv
285  << std::setw(20) << diff
286  << std::setw(20) << err
287  << "\n";
288  t *= p1;
289  }
290  std::cout << "\n";
291  // Check v'(0)
292  x = -eps_;
293  vx = zero;
294  vy = zero;
295  dv = regret(x,1);
296  t = one;
297  diff = zero;
298  err = zero;
299  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-eps) is correct? \n";
300  std::cout << std::right << std::setw(20) << "t"
301  << std::setw(20) << "v'(x)"
302  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
303  << std::setw(20) << "Error"
304  << "\n";
305  for (int i = 0; i < 13; i++) {
306  vy = regret(x+t,0);
307  vx = regret(x-t,0);
308  diff = (vy-vx)/(two*t);
309  err = std::abs(diff-dv);
310  std::cout << std::scientific << std::setprecision(11) << std::right
311  << std::setw(20) << t
312  << std::setw(20) << dv
313  << std::setw(20) << diff
314  << std::setw(20) << err
315  << "\n";
316  t *= p1;
317  }
318  std::cout << "\n";
319  // check v''(eps)
320  vx = zero;
321  vy = zero;
322  dv = regret(x,2);
323  t = one;
324  diff = zero;
325  err = zero;
326  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(-eps) is correct? \n";
327  std::cout << std::right << std::setw(20) << "t"
328  << std::setw(20) << "v''(x)"
329  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
330  << std::setw(20) << "Error"
331  << "\n";
332  for (int i = 0; i < 13; i++) {
333  vy = regret(x+t,1);
334  vx = regret(x-t,1);
335  diff = (vy-vx)/(two*t);
336  err = std::abs(diff-dv);
337  std::cout << std::scientific << std::setprecision(11) << std::right
338  << std::setw(20) << t
339  << std::setw(20) << dv
340  << std::setw(20) << diff
341  << std::setw(20) << err
342  << "\n";
343  t *= p1;
344  }
345  std::cout << "\n";
346  }
347 
348 };
349 
350 }
351 #endif
Provides a general interface for risk measures generated through the expectation risk quadrangle...
virtual void checkRegret(void)
Run default derivative tests for the scalar regret function.
MoreauYosidaCVaR(Teuchos::ParameterList &parlist)
Constructor.
Provides an interface for a smooth approximation of the conditional value-at-risk.
void checkRegret(void)
Run default derivative tests for the scalar regret function.
MoreauYosidaCVaR(Real prob, Real eps)
Constructor.
Real error(Real x, int deriv=0)
Real regret(Real x, int deriv=0)
Evaluate the scalar regret function at x.