ROL
ROL_QuantileQuadrangle.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_SMOOTHCVARQUAD_HPP
45 #define ROL_SMOOTHCVARQUAD_HPP
46 
47 #include "ROL_ExpectationQuad.hpp"
48 #include "ROL_PlusFunction.hpp"
49 
50 namespace ROL {
51 
52 template<class Real>
53 class QuantileQuadrangle : public ExpectationQuad<Real> {
54 private:
55 
56  Teuchos::RCP<PlusFunction<Real> > pf_;
57 
58  Real prob_;
59  Real eps_;
60 
61 public:
62 
63  QuantileQuadrangle(Real prob, Real eps, Teuchos::RCP<PlusFunction<Real> > &pf )
64  : ExpectationQuad<Real>(), pf_(pf) {
65  prob_ = ((prob >= 0.0) ? ((prob <= 1.0) ? prob : 0.5) : 0.5);
66  eps_ = ((eps > 0.0) ? eps : 1.0);
67  }
68 
69  QuantileQuadrangle(Teuchos::ParameterList &parlist) : ExpectationQuad<Real>() {
70  Teuchos::ParameterList& list
71  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Quantile-Based Quadrangle");
72  // Check CVaR inputs
73  Real prob = list.get("Confidence Level",0.5);
74  prob_ = ((prob >= 0.0) ? ((prob <= 1.0) ? prob : 0.5) : 0.5);
75  // Build plus function
76  pf_ = Teuchos::rcp( new PlusFunction<Real>(list) );
77  Real eps = list.get("Smoothing Parameter",1.);
78  eps_ = ((eps > 0.) ? eps : 1.);
79  }
80 
81  Real error(Real x, int deriv = 0) {
82  Real err = (prob_/(1.0-prob_))*pf_->evaluate(x,deriv)
83  + ((deriv%2) ? -1.0 : 1.0)*pf_->evaluate(-x,deriv);
84  return err;
85  }
86 
87  Real regret(Real x, int deriv = 0) {
88  Real X = ((deriv==0) ? x : ((deriv==1) ? 1.0 : 0.0));
89  Real reg = error(x,deriv) + X;
90  return reg;
91  }
92 
93  void checkRegret(void) {
95  // Check v'(eps)
96  Real x = eps_;
97  Real vx = 0.0, vy = 0.0;
98  Real dv = regret(x,1);
99  Real t = 1.0;
100  Real diff = 0.0;
101  Real err = 0.0;
102  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(eps) is correct? \n";
103  std::cout << std::right << std::setw(20) << "t"
104  << std::setw(20) << "v'(x)"
105  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
106  << std::setw(20) << "Error"
107  << "\n";
108  for (int i = 0; i < 13; i++) {
109  vy = regret(x+t,0);
110  vx = regret(x-t,0);
111  diff = (vy-vx)/(2.0*t);
112  err = std::abs(diff-dv);
113  std::cout << std::scientific << std::setprecision(11) << std::right
114  << std::setw(20) << t
115  << std::setw(20) << dv
116  << std::setw(20) << diff
117  << std::setw(20) << err
118  << "\n";
119  t *= 0.1;
120  }
121  std::cout << "\n";
122  // check v''(eps)
123  vx = 0.0;
124  vy = 0.0;
125  dv = regret(x,2);
126  t = 1.0;
127  diff = 0.0;
128  err = 0.0;
129  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(eps) is correct? \n";
130  std::cout << std::right << std::setw(20) << "t"
131  << std::setw(20) << "v''(x)"
132  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
133  << std::setw(20) << "Error"
134  << "\n";
135  for (int i = 0; i < 13; i++) {
136  vy = regret(x+t,1);
137  vx = regret(x-t,1);
138  diff = (vy-vx)/(2.0*t);
139  err = std::abs(diff-dv);
140  std::cout << std::scientific << std::setprecision(11) << std::right
141  << std::setw(20) << t
142  << std::setw(20) << dv
143  << std::setw(20) << diff
144  << std::setw(20) << err
145  << "\n";
146  t *= 0.1;
147  }
148  std::cout << "\n";
149  // Check v'(0)
150  x = 0.0;
151  vx = 0.0;
152  vy = 0.0;
153  dv = regret(x,1);
154  t = 1.0;
155  diff = 0.0;
156  err = 0.0;
157  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(0) is correct? \n";
158  std::cout << std::right << std::setw(20) << "t"
159  << std::setw(20) << "v'(x)"
160  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
161  << std::setw(20) << "Error"
162  << "\n";
163  for (int i = 0; i < 13; i++) {
164  vy = regret(x+t,0);
165  vx = regret(x-t,0);
166  diff = (vy-vx)/(2.0*t);
167  err = std::abs(diff-dv);
168  std::cout << std::scientific << std::setprecision(11) << std::right
169  << std::setw(20) << t
170  << std::setw(20) << dv
171  << std::setw(20) << diff
172  << std::setw(20) << err
173  << "\n";
174  t *= 0.1;
175  }
176  std::cout << "\n";
177  // check v''(eps)
178  vx = 0.0;
179  vy = 0.0;
180  dv = regret(x,2);
181  t = 1.0;
182  diff = 0.0;
183  err = 0.0;
184  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(0) is correct? \n";
185  std::cout << std::right << std::setw(20) << "t"
186  << std::setw(20) << "v''(x)"
187  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
188  << std::setw(20) << "Error"
189  << "\n";
190  for (int i = 0; i < 13; i++) {
191  vy = regret(x+t,1);
192  vx = regret(x-t,1);
193  diff = (vy-vx)/(2.0*t);
194  err = std::abs(diff-dv);
195  std::cout << std::scientific << std::setprecision(11) << std::right
196  << std::setw(20) << t
197  << std::setw(20) << dv
198  << std::setw(20) << diff
199  << std::setw(20) << err
200  << "\n";
201  t *= 0.1;
202  }
203  std::cout << "\n";
204  // Check v'(0)
205  x = -eps_;
206  vx = 0.0;
207  vy = 0.0;
208  dv = regret(x,1);
209  t = 1.0;
210  diff = 0.0;
211  err = 0.0;
212  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-eps) is correct? \n";
213  std::cout << std::right << std::setw(20) << "t"
214  << std::setw(20) << "v'(x)"
215  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
216  << std::setw(20) << "Error"
217  << "\n";
218  for (int i = 0; i < 13; i++) {
219  vy = regret(x+t,0);
220  vx = regret(x-t,0);
221  diff = (vy-vx)/(2.0*t);
222  err = std::abs(diff-dv);
223  std::cout << std::scientific << std::setprecision(11) << std::right
224  << std::setw(20) << t
225  << std::setw(20) << dv
226  << std::setw(20) << diff
227  << std::setw(20) << err
228  << "\n";
229  t *= 0.1;
230  }
231  std::cout << "\n";
232  // check v''(eps)
233  vx = 0.0;
234  vy = 0.0;
235  dv = regret(x,2);
236  t = 1.0;
237  diff = 0.0;
238  err = 0.0;
239  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(-eps) is correct? \n";
240  std::cout << std::right << std::setw(20) << "t"
241  << std::setw(20) << "v''(x)"
242  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
243  << std::setw(20) << "Error"
244  << "\n";
245  for (int i = 0; i < 13; i++) {
246  vy = regret(x+t,1);
247  vx = regret(x-t,1);
248  diff = (vy-vx)/(2.0*t);
249  err = std::abs(diff-dv);
250  std::cout << std::scientific << std::setprecision(11) << std::right
251  << std::setw(20) << t
252  << std::setw(20) << dv
253  << std::setw(20) << diff
254  << std::setw(20) << err
255  << "\n";
256  t *= 0.1;
257  }
258  std::cout << "\n";
259  }
260 
261 };
262 
263 }
264 #endif
QuantileQuadrangle(Teuchos::ParameterList &parlist)
virtual void checkRegret(void)
Real regret(Real x, int deriv=0)
Real error(Real x, int deriv=0)
Teuchos::RCP< PlusFunction< Real > > pf_
QuantileQuadrangle(Real prob, Real eps, Teuchos::RCP< PlusFunction< Real > > &pf)