src/HOL/Library/Convex.thy
author Andreas Lochbihler
Fri Sep 20 10:09:16 2013 +0200 (2013-09-20)
changeset 53745 788730ab7da4
parent 53676 476ef9b468d2
child 54230 b1d955791529
permissions -rw-r--r--
prefer Code.abort over code_abort
     1 (*  Title:      HOL/Library/Convex.thy
     2     Author:     Armin Heller, TU Muenchen
     3     Author:     Johannes Hoelzl, TU Muenchen
     4 *)
     5 
     6 header {* Convexity in real vector spaces *}
     7 
     8 theory Convex
     9 imports Product_Vector
    10 begin
    11 
    12 subsection {* Convexity. *}
    13 
    14 definition convex :: "'a::real_vector set \<Rightarrow> bool"
    15   where "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
    16 
    17 lemma convexI:
    18   assumes "\<And>x y u v. x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> 0 \<le> u \<Longrightarrow> 0 \<le> v \<Longrightarrow> u + v = 1 \<Longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s"
    19   shows "convex s"
    20   using assms unfolding convex_def by fast
    21 
    22 lemma convexD:
    23   assumes "convex s" and "x \<in> s" and "y \<in> s" and "0 \<le> u" and "0 \<le> v" and "u + v = 1"
    24   shows "u *\<^sub>R x + v *\<^sub>R y \<in> s"
    25   using assms unfolding convex_def by fast
    26 
    27 lemma convex_alt:
    28   "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u. 0 \<le> u \<and> u \<le> 1 \<longrightarrow> ((1 - u) *\<^sub>R x + u *\<^sub>R y) \<in> s)"
    29   (is "_ \<longleftrightarrow> ?alt")
    30 proof
    31   assume alt[rule_format]: ?alt
    32   { fix x y and u v :: real assume mem: "x \<in> s" "y \<in> s"
    33     assume "0 \<le> u" "0 \<le> v"
    34     moreover assume "u + v = 1" then have "u = 1 - v" by auto
    35     ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s" using alt[OF mem] by auto }
    36   then show "convex s" unfolding convex_def by auto
    37 qed (auto simp: convex_def)
    38 
    39 lemma mem_convex:
    40   assumes "convex s" "a \<in> s" "b \<in> s" "0 \<le> u" "u \<le> 1"
    41   shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \<in> s"
    42   using assms unfolding convex_alt by auto
    43 
    44 lemma convex_empty[intro]: "convex {}"
    45   unfolding convex_def by simp
    46 
    47 lemma convex_singleton[intro]: "convex {a}"
    48   unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric])
    49 
    50 lemma convex_UNIV[intro]: "convex UNIV"
    51   unfolding convex_def by auto
    52 
    53 lemma convex_Inter: "(\<forall>s\<in>f. convex s) ==> convex(\<Inter> f)"
    54   unfolding convex_def by auto
    55 
    56 lemma convex_Int: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<inter> t)"
    57   unfolding convex_def by auto
    58 
    59 lemma convex_INT: "\<forall>i\<in>A. convex (B i) \<Longrightarrow> convex (\<Inter>i\<in>A. B i)"
    60   unfolding convex_def by auto
    61 
    62 lemma convex_Times: "convex s \<Longrightarrow> convex t \<Longrightarrow> convex (s \<times> t)"
    63   unfolding convex_def by auto
    64 
    65 lemma convex_halfspace_le: "convex {x. inner a x \<le> b}"
    66   unfolding convex_def
    67   by (auto simp: inner_add intro!: convex_bound_le)
    68 
    69 lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
    70 proof -
    71   have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}" by auto
    72   show ?thesis unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
    73 qed
    74 
    75 lemma convex_hyperplane: "convex {x. inner a x = b}"
    76 proof -
    77   have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}" by auto
    78   show ?thesis using convex_halfspace_le convex_halfspace_ge
    79     by (auto intro!: convex_Int simp: *)
    80 qed
    81 
    82 lemma convex_halfspace_lt: "convex {x. inner a x < b}"
    83   unfolding convex_def
    84   by (auto simp: convex_bound_lt inner_add)
    85 
    86 lemma convex_halfspace_gt: "convex {x. inner a x > b}"
    87    using convex_halfspace_lt[of "-a" "-b"] by auto
    88 
    89 lemma convex_real_interval:
    90   fixes a b :: "real"
    91   shows "convex {a..}" and "convex {..b}"
    92     and "convex {a<..}" and "convex {..<b}"
    93     and "convex {a..b}" and "convex {a<..b}"
    94     and "convex {a..<b}" and "convex {a<..<b}"
    95 proof -
    96   have "{a..} = {x. a \<le> inner 1 x}" by auto
    97   then show 1: "convex {a..}" by (simp only: convex_halfspace_ge)
    98   have "{..b} = {x. inner 1 x \<le> b}" by auto
    99   then show 2: "convex {..b}" by (simp only: convex_halfspace_le)
   100   have "{a<..} = {x. a < inner 1 x}" by auto
   101   then show 3: "convex {a<..}" by (simp only: convex_halfspace_gt)
   102   have "{..<b} = {x. inner 1 x < b}" by auto
   103   then show 4: "convex {..<b}" by (simp only: convex_halfspace_lt)
   104   have "{a..b} = {a..} \<inter> {..b}" by auto
   105   then show "convex {a..b}" by (simp only: convex_Int 1 2)
   106   have "{a<..b} = {a<..} \<inter> {..b}" by auto
   107   then show "convex {a<..b}" by (simp only: convex_Int 3 2)
   108   have "{a..<b} = {a..} \<inter> {..<b}" by auto
   109   then show "convex {a..<b}" by (simp only: convex_Int 1 4)
   110   have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
   111   then show "convex {a<..<b}" by (simp only: convex_Int 3 4)
   112 qed
   113 
   114 
   115 subsection {* Explicit expressions for convexity in terms of arbitrary sums. *}
   116 
   117 lemma convex_setsum:
   118   fixes C :: "'a::real_vector set"
   119   assumes "finite s" and "convex C" and "(\<Sum> i \<in> s. a i) = 1"
   120   assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0" and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   121   shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
   122   using assms
   123 proof (induct s arbitrary:a rule: finite_induct)
   124   case empty
   125   then show ?case by auto
   126 next
   127   case (insert i s) note asms = this
   128   { assume "a i = 1"
   129     then have "(\<Sum> j \<in> s. a j) = 0"
   130       using asms by auto
   131     then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   132       using setsum_nonneg_0[where 'b=real] asms by fastforce
   133     then have ?case using asms by auto }
   134   moreover
   135   { assume asm: "a i \<noteq> 1"
   136     from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   137     have fis: "finite (insert i s)" using asms by auto
   138     then have ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a 1] asms by simp
   139     then have "a i < 1" using asm by auto
   140     then have i0: "1 - a i > 0" by auto
   141     let ?a = "\<lambda>j. a j / (1 - a i)"
   142     { fix j assume "j \<in> s"
   143       then have "?a j \<ge> 0"
   144         using i0 asms divide_nonneg_pos
   145         by fastforce
   146     } note a_nonneg = this
   147     have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   148     then have "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
   149     then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   150     then have a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
   151     with asms have "(\<Sum>j\<in>s. ?a j *\<^sub>R y j) \<in> C" using a_nonneg by fastforce
   152     then have "a i *\<^sub>R y i + (1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   153       using asms yai ai1 by (auto intro: convexD)
   154     then have "a i *\<^sub>R y i + (\<Sum> j \<in> s. (1 - a i) *\<^sub>R (?a j *\<^sub>R y j)) \<in> C"
   155       using scaleR_right.setsum[of "(1 - a i)" "\<lambda> j. ?a j *\<^sub>R y j" s] by auto
   156     then have "a i *\<^sub>R y i + (\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C" using i0 by auto
   157     then have ?case using setsum.insert asms by auto
   158   }
   159   ultimately show ?case by auto
   160 qed
   161 
   162 lemma convex:
   163   "convex s \<longleftrightarrow> (\<forall>(k::nat) u x. (\<forall>i. 1\<le>i \<and> i\<le>k \<longrightarrow> 0 \<le> u i \<and> x i \<in>s) \<and> (setsum u {1..k} = 1)
   164       \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   165 proof safe
   166   fix k :: nat
   167   fix u :: "nat \<Rightarrow> real"
   168   fix x
   169   assume "convex s"
   170     "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
   171     "setsum u {1..k} = 1"
   172   from this convex_setsum[of "{1 .. k}" s]
   173   show "(\<Sum>j\<in>{1 .. k}. u j *\<^sub>R x j) \<in> s" by auto
   174 next
   175   assume asm: "\<forall>k u x. (\<forall> i :: nat. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s) \<and> setsum u {1..k} = 1
   176     \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
   177   { fix \<mu> :: real
   178     fix x y :: 'a
   179     assume xy: "x \<in> s" "y \<in> s"
   180     assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
   181     let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
   182     let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
   183     have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}" by auto
   184     then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1" by simp
   185     then have "setsum ?u {1 .. 2} = 1"
   186       using setsum_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
   187       by auto
   188     with asm[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
   189       using mu xy by auto
   190     have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
   191       using setsum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
   192     from setsum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
   193     have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y" by auto
   194     then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s" using s by (auto simp:add_commute)
   195   }
   196   then show "convex s" unfolding convex_alt by auto
   197 qed
   198 
   199 
   200 lemma convex_explicit:
   201   fixes s :: "'a::real_vector set"
   202   shows "convex s \<longleftrightarrow>
   203     (\<forall>t u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> setsum u t = 1 \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) t \<in> s)"
   204 proof safe
   205   fix t
   206   fix u :: "'a \<Rightarrow> real"
   207   assume "convex s" "finite t"
   208     "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = 1"
   209   then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   210     using convex_setsum[of t s u "\<lambda> x. x"] by auto
   211 next
   212   assume asm0: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x)
   213     \<and> setsum u t = 1 \<longrightarrow> (\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   214   show "convex s"
   215     unfolding convex_alt
   216   proof safe
   217     fix x y
   218     fix \<mu> :: real
   219     assume asm: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
   220     { assume "x \<noteq> y"
   221       then have "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   222         using asm0[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"]
   223           asm by auto }
   224     moreover
   225     { assume "x = y"
   226       then have "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   227         using asm0[rule_format, of "{x, y}" "\<lambda> z. 1"]
   228           asm by (auto simp:field_simps real_vector.scale_left_diff_distrib) }
   229     ultimately show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s" by blast
   230   qed
   231 qed
   232 
   233 lemma convex_finite:
   234   assumes "finite s"
   235   shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1
   236                       \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
   237   unfolding convex_explicit
   238 proof safe
   239   fix t u
   240   assume sum: "\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1 \<longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> s"
   241     and as: "finite t" "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = (1::real)"
   242   have *: "s \<inter> t = t" using as(2) by auto
   243   have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)"
   244     by simp
   245   show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   246    using sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] as *
   247    by (auto simp: assms setsum_cases if_distrib if_distrib_arg)
   248 qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
   249 
   250 definition convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
   251   where "convex_on s f \<longleftrightarrow>
   252     (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y)"
   253 
   254 lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
   255   unfolding convex_on_def by auto
   256 
   257 lemma convex_on_add [intro]:
   258   assumes "convex_on s f" "convex_on s g"
   259   shows "convex_on s (\<lambda>x. f x + g x)"
   260 proof -
   261   { fix x y
   262     assume "x\<in>s" "y\<in>s"
   263     moreover
   264     fix u v :: real
   265     assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   266     ultimately
   267     have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> (u * f x + v * f y) + (u * g x + v * g y)"
   268       using assms unfolding convex_on_def by (auto simp add: add_mono)
   269     then have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> u * (f x + g x) + v * (f y + g y)"
   270       by (simp add: field_simps)
   271   }
   272   then show ?thesis unfolding convex_on_def by auto
   273 qed
   274 
   275 lemma convex_on_cmul [intro]:
   276   assumes "0 \<le> (c::real)" "convex_on s f"
   277   shows "convex_on s (\<lambda>x. c * f x)"
   278 proof-
   279   have *: "\<And>u c fx v fy ::real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
   280     by (simp add: field_simps)
   281   show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
   282     unfolding convex_on_def and * by auto
   283 qed
   284 
   285 lemma convex_lower:
   286   assumes "convex_on s f"  "x\<in>s"  "y \<in> s"  "0 \<le> u"  "0 \<le> v"  "u + v = 1"
   287   shows "f (u *\<^sub>R x + v *\<^sub>R y) \<le> max (f x) (f y)"
   288 proof-
   289   let ?m = "max (f x) (f y)"
   290   have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
   291     using assms(4,5) by (auto simp add: mult_left_mono add_mono)
   292   also have "\<dots> = max (f x) (f y)" using assms(6) unfolding distrib[symmetric] by auto
   293   finally show ?thesis
   294     using assms unfolding convex_on_def by fastforce
   295 qed
   296 
   297 lemma convex_on_dist [intro]:
   298   fixes s :: "'a::real_normed_vector set"
   299   shows "convex_on s (\<lambda>x. dist a x)"
   300 proof (auto simp add: convex_on_def dist_norm)
   301   fix x y
   302   assume "x\<in>s" "y\<in>s"
   303   fix u v :: real
   304   assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   305   have "a = u *\<^sub>R a + v *\<^sub>R a"
   306     unfolding scaleR_left_distrib[symmetric] and `u+v=1` by simp
   307   then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
   308     by (auto simp add: algebra_simps)
   309   show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
   310     unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
   311     using `0 \<le> u` `0 \<le> v` by auto
   312 qed
   313 
   314 
   315 subsection {* Arithmetic operations on sets preserve convexity. *}
   316 
   317 lemma convex_linear_image:
   318   assumes "linear f" and "convex s" shows "convex (f ` s)"
   319 proof -
   320   interpret f: linear f by fact
   321   from `convex s` show "convex (f ` s)"
   322     by (simp add: convex_def f.scaleR [symmetric] f.add [symmetric])
   323 qed
   324 
   325 lemma convex_linear_vimage:
   326   assumes "linear f" and "convex s" shows "convex (f -` s)"
   327 proof -
   328   interpret f: linear f by fact
   329   from `convex s` show "convex (f -` s)"
   330     by (simp add: convex_def f.add f.scaleR)
   331 qed
   332 
   333 lemma convex_scaling:
   334   assumes "convex s" shows "convex ((\<lambda>x. c *\<^sub>R x) ` s)"
   335 proof -
   336   have "linear (\<lambda>x. c *\<^sub>R x)" by (simp add: linearI scaleR_add_right)
   337   then show ?thesis using `convex s` by (rule convex_linear_image)
   338 qed
   339 
   340 lemma convex_negations:
   341   assumes "convex s" shows "convex ((\<lambda>x. - x) ` s)"
   342 proof -
   343   have "linear (\<lambda>x. - x)" by (simp add: linearI)
   344   then show ?thesis using `convex s` by (rule convex_linear_image)
   345 qed
   346 
   347 lemma convex_sums:
   348   assumes "convex s" and "convex t"
   349   shows "convex {x + y| x y. x \<in> s \<and> y \<in> t}"
   350 proof -
   351   have "linear (\<lambda>(x, y). x + y)"
   352     by (auto intro: linearI simp add: scaleR_add_right)
   353   with assms have "convex ((\<lambda>(x, y). x + y) ` (s \<times> t))"
   354     by (intro convex_linear_image convex_Times)
   355   also have "((\<lambda>(x, y). x + y) ` (s \<times> t)) = {x + y| x y. x \<in> s \<and> y \<in> t}"
   356     by auto
   357   finally show ?thesis .
   358 qed
   359 
   360 lemma convex_differences:
   361   assumes "convex s" "convex t"
   362   shows "convex {x - y| x y. x \<in> s \<and> y \<in> t}"
   363 proof -
   364   have "{x - y| x y. x \<in> s \<and> y \<in> t} = {x + y |x y. x \<in> s \<and> y \<in> uminus ` t}"
   365     unfolding diff_def by auto
   366   then show ?thesis
   367     using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
   368 qed
   369 
   370 lemma convex_translation:
   371   assumes "convex s"
   372   shows "convex ((\<lambda>x. a + x) ` s)"
   373 proof -
   374   have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s" by auto
   375   then show ?thesis
   376     using convex_sums[OF convex_singleton[of a] assms] by auto
   377 qed
   378 
   379 lemma convex_affinity:
   380   assumes "convex s"
   381   shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
   382 proof -
   383   have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s" by auto
   384   then show ?thesis
   385     using convex_translation[OF convex_scaling[OF assms], of a c] by auto
   386 qed
   387 
   388 lemma pos_is_convex: "convex {0 :: real <..}"
   389   unfolding convex_alt
   390 proof safe
   391   fix y x \<mu> :: real
   392   assume asms: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   393   { assume "\<mu> = 0"
   394     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y" by simp
   395     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   396   moreover
   397   { assume "\<mu> = 1"
   398     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   399   moreover
   400   { assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
   401     then have "\<mu> > 0" "(1 - \<mu>) > 0" using asms by auto
   402     then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms
   403       by (auto simp add: add_pos_pos mult_pos_pos) }
   404   ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0" using assms by fastforce
   405 qed
   406 
   407 lemma convex_on_setsum:
   408   fixes a :: "'a \<Rightarrow> real"
   409     and y :: "'a \<Rightarrow> 'b::real_vector"
   410     and f :: "'b \<Rightarrow> real"
   411   assumes "finite s" "s \<noteq> {}"
   412     and "convex_on C f"
   413     and "convex C"
   414     and "(\<Sum> i \<in> s. a i) = 1"
   415     and "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   416     and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   417   shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
   418   using assms
   419 proof (induct s arbitrary: a rule: finite_ne_induct)
   420   case (singleton i)
   421   then have ai: "a i = 1" by auto
   422   then show ?case by auto
   423 next
   424   case (insert i s) note asms = this
   425   then have "convex_on C f" by simp
   426   from this[unfolded convex_on_def, rule_format]
   427   have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1
   428       \<Longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   429     by simp
   430   { assume "a i = 1"
   431     then have "(\<Sum> j \<in> s. a j) = 0"
   432       using asms by auto
   433     then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   434       using setsum_nonneg_0[where 'b=real] asms by fastforce
   435     then have ?case using asms by auto }
   436   moreover
   437   { assume asm: "a i \<noteq> 1"
   438     from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   439     have fis: "finite (insert i s)" using asms by auto
   440     then have ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a] asms by simp
   441     then have "a i < 1" using asm by auto
   442     then have i0: "1 - a i > 0" by auto
   443     let ?a = "\<lambda>j. a j / (1 - a i)"
   444     { fix j assume "j \<in> s"
   445       then have "?a j \<ge> 0"
   446         using i0 asms divide_nonneg_pos
   447         by fastforce }
   448     note a_nonneg = this
   449     have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   450     then have "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
   451     then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   452     then have a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
   453     have "convex C" using asms by auto
   454     then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   455       using asms convex_setsum[OF `finite s`
   456         `convex C` a1 a_nonneg] by auto
   457     have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
   458       using a_nonneg a1 asms by blast
   459     have "f (\<Sum> j \<in> insert i s. a j *\<^sub>R y j) = f ((\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   460       using setsum.insert[of s i "\<lambda> j. a j *\<^sub>R y j", OF `finite s` `i \<notin> s`] asms
   461       by (auto simp only:add_commute)
   462     also have "\<dots> = f (((1 - a i) * inverse (1 - a i)) *\<^sub>R (\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   463       using i0 by auto
   464     also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. (a j * inverse (1 - a i)) *\<^sub>R y j) + a i *\<^sub>R y i)"
   465       using scaleR_right.setsum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
   466       by (auto simp:algebra_simps)
   467     also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   468       by (auto simp: divide_inverse)
   469     also have "\<dots> \<le> (1 - a i) *\<^sub>R f ((\<Sum> j \<in> s. ?a j *\<^sub>R y j)) + a i * f (y i)"
   470       using conv[of "y i" "(\<Sum> j \<in> s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1]
   471       by (auto simp add:add_commute)
   472     also have "\<dots> \<le> (1 - a i) * (\<Sum> j \<in> s. ?a j * f (y j)) + a i * f (y i)"
   473       using add_right_mono[OF mult_left_mono[of _ _ "1 - a i",
   474         OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"] by simp
   475     also have "\<dots> = (\<Sum> j \<in> s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)"
   476       unfolding setsum_right_distrib[of "1 - a i" "\<lambda> j. ?a j * f (y j)"] using i0 by auto
   477     also have "\<dots> = (\<Sum> j \<in> s. a j * f (y j)) + a i * f (y i)" using i0 by auto
   478     also have "\<dots> = (\<Sum> j \<in> insert i s. a j * f (y j))" using asms by auto
   479     finally have "f (\<Sum> j \<in> insert i s. a j *\<^sub>R y j) \<le> (\<Sum> j \<in> insert i s. a j * f (y j))"
   480       by simp }
   481   ultimately show ?case by auto
   482 qed
   483 
   484 lemma convex_on_alt:
   485   fixes C :: "'a::real_vector set"
   486   assumes "convex C"
   487   shows "convex_on C f =
   488   (\<forall> x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1
   489       \<longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
   490 proof safe
   491   fix x y
   492   fix \<mu> :: real
   493   assume asms: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
   494   from this[unfolded convex_on_def, rule_format]
   495   have "\<And>u v. \<lbrakk>0 \<le> u; 0 \<le> v; u + v = 1\<rbrakk> \<Longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" by auto
   496   from this[of "\<mu>" "1 - \<mu>", simplified] asms
   497   show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y" by auto
   498 next
   499   assume asm: "\<forall>x\<in>C. \<forall>y\<in>C. \<forall>\<mu>. 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   500   { fix x y
   501     fix u v :: real
   502     assume lasm: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   503     then have[simp]: "1 - u = v" by auto
   504     from asm[rule_format, of x y u]
   505     have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" using lasm by auto
   506   }
   507   then show "convex_on C f" unfolding convex_on_def by auto
   508 qed
   509 
   510 lemma convex_on_diff:
   511   fixes f :: "real \<Rightarrow> real"
   512   assumes f: "convex_on I f" and I: "x\<in>I" "y\<in>I" and t: "x < t" "t < y"
   513   shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   514     "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   515 proof -
   516   def a \<equiv> "(t - y) / (x - y)"
   517   with t have "0 \<le> a" "0 \<le> 1 - a" by (auto simp: field_simps)
   518   with f `x \<in> I` `y \<in> I` have cvx: "f (a * x + (1 - a) * y) \<le> a * f x + (1 - a) * f y"
   519     by (auto simp: convex_on_def)
   520   have "a * x + (1 - a) * y = a * (x - y) + y" by (simp add: field_simps)
   521   also have "\<dots> = t" unfolding a_def using `x < t` `t < y` by simp
   522   finally have "f t \<le> a * f x + (1 - a) * f y" using cvx by simp
   523   also have "\<dots> = a * (f x - f y) + f y" by (simp add: field_simps)
   524   finally have "f t - f y \<le> a * (f x - f y)" by simp
   525   with t show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   526     by (simp add: le_divide_eq divide_le_eq field_simps a_def)
   527   with t show "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   528     by (simp add: le_divide_eq divide_le_eq field_simps)
   529 qed
   530 
   531 lemma pos_convex_function:
   532   fixes f :: "real \<Rightarrow> real"
   533   assumes "convex C"
   534     and leq: "\<And>x y. \<lbrakk>x \<in> C ; y \<in> C\<rbrakk> \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
   535   shows "convex_on C f"
   536   unfolding convex_on_alt[OF assms(1)]
   537   using assms
   538 proof safe
   539   fix x y \<mu> :: real
   540   let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   541   assume asm: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   542   then have "1 - \<mu> \<ge> 0" by auto
   543   then have xpos: "?x \<in> C" using asm unfolding convex_alt by fastforce
   544   have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x)
   545             \<ge> \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
   546     using add_mono[OF mult_left_mono[OF leq[OF xpos asm(2)] `\<mu> \<ge> 0`]
   547       mult_left_mono[OF leq[OF xpos asm(3)] `1 - \<mu> \<ge> 0`]] by auto
   548   then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
   549     by (auto simp add: field_simps)
   550   then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   551     using convex_on_alt by auto
   552 qed
   553 
   554 lemma atMostAtLeast_subset_convex:
   555   fixes C :: "real set"
   556   assumes "convex C"
   557     and "x \<in> C" "y \<in> C" "x < y"
   558   shows "{x .. y} \<subseteq> C"
   559 proof safe
   560   fix z assume zasm: "z \<in> {x .. y}"
   561   { assume asm: "x < z" "z < y"
   562     let ?\<mu> = "(y - z) / (y - x)"
   563     have "0 \<le> ?\<mu>" "?\<mu> \<le> 1" using assms asm by (auto simp add: field_simps)
   564     then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
   565       using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
   566       by (simp add: algebra_simps)
   567     have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
   568       by (auto simp add: field_simps)
   569     also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
   570       using assms unfolding add_divide_distrib by (auto simp: field_simps)
   571     also have "\<dots> = z"
   572       using assms by (auto simp: field_simps)
   573     finally have "z \<in> C"
   574       using comb by auto }
   575   note less = this
   576   show "z \<in> C" using zasm less assms
   577     unfolding atLeastAtMost_iff le_less by auto
   578 qed
   579 
   580 lemma f''_imp_f':
   581   fixes f :: "real \<Rightarrow> real"
   582   assumes "convex C"
   583     and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   584     and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   585     and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   586     and "x \<in> C" "y \<in> C"
   587   shows "f' x * (y - x) \<le> f y - f x"
   588   using assms
   589 proof -
   590   { fix x y :: real
   591     assume asm: "x \<in> C" "y \<in> C" "y > x"
   592     then have ge: "y - x > 0" "y - x \<ge> 0" by auto
   593     from asm have le: "x - y < 0" "x - y \<le> 0" by auto
   594     then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
   595       using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `y \<in> C` `x < y`],
   596         THEN f', THEN MVT2[OF `x < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
   597       by auto
   598     then have "z1 \<in> C" using atMostAtLeast_subset_convex
   599       `convex C` `x \<in> C` `y \<in> C` `x < y` by fastforce
   600     from z1 have z1': "f x - f y = (x - y) * f' z1"
   601       by (simp add:field_simps)
   602     obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2"
   603       using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `z1 \<in> C` `x < z1`],
   604         THEN f'', THEN MVT2[OF `x < z1`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   605       by auto
   606     obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3"
   607       using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `z1 \<in> C` `y \<in> C` `z1 < y`],
   608         THEN f'', THEN MVT2[OF `z1 < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1
   609       by auto
   610     have "f' y - (f x - f y) / (x - y) = f' y - f' z1"
   611       using asm z1' by auto
   612     also have "\<dots> = (y - z1) * f'' z3" using z3 by auto
   613     finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3" by simp
   614     have A': "y - z1 \<ge> 0" using z1 by auto
   615     have "z3 \<in> C" using z3 asm atMostAtLeast_subset_convex
   616       `convex C` `x \<in> C` `z1 \<in> C` `x < z1` by fastforce
   617     then have B': "f'' z3 \<ge> 0" using assms by auto
   618     from A' B' have "(y - z1) * f'' z3 \<ge> 0" using mult_nonneg_nonneg by auto
   619     from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0" by auto
   620     from mult_right_mono_neg[OF this le(2)]
   621     have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
   622       by (simp add: algebra_simps)
   623     then have "f' y * (x - y) - (f x - f y) \<le> 0" using le by auto
   624     then have res: "f' y * (x - y) \<le> f x - f y" by auto
   625     have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
   626       using asm z1 by auto
   627     also have "\<dots> = (z1 - x) * f'' z2" using z2 by auto
   628     finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2" by simp
   629     have A: "z1 - x \<ge> 0" using z1 by auto
   630     have "z2 \<in> C" using z2 z1 asm atMostAtLeast_subset_convex
   631       `convex C` `z1 \<in> C` `y \<in> C` `z1 < y` by fastforce
   632     then have B: "f'' z2 \<ge> 0" using assms by auto
   633     from A B have "(z1 - x) * f'' z2 \<ge> 0" using mult_nonneg_nonneg by auto
   634     from cool this have "(f y - f x) / (y - x) - f' x \<ge> 0" by auto
   635     from mult_right_mono[OF this ge(2)]
   636     have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
   637       by (simp add: algebra_simps)
   638     then have "f y - f x - f' x * (y - x) \<ge> 0" using ge by auto
   639     then have "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   640       using res by auto } note less_imp = this
   641   { fix x y :: real
   642     assume "x \<in> C" "y \<in> C" "x \<noteq> y"
   643     then have"f y - f x \<ge> f' x * (y - x)"
   644     unfolding neq_iff using less_imp by auto } note neq_imp = this
   645   moreover
   646   { fix x y :: real
   647     assume asm: "x \<in> C" "y \<in> C" "x = y"
   648     then have "f y - f x \<ge> f' x * (y - x)" by auto }
   649   ultimately show ?thesis using assms by blast
   650 qed
   651 
   652 lemma f''_ge0_imp_convex:
   653   fixes f :: "real \<Rightarrow> real"
   654   assumes conv: "convex C"
   655     and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   656     and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   657     and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   658   shows "convex_on C f"
   659 using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function by fastforce
   660 
   661 lemma minus_log_convex:
   662   fixes b :: real
   663   assumes "b > 1"
   664   shows "convex_on {0 <..} (\<lambda> x. - log b x)"
   665 proof -
   666   have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)" using DERIV_log by auto
   667   then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
   668     by (auto simp: DERIV_minus)
   669   have "\<And>z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
   670     using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
   671   from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
   672   have "\<And>z :: real. z > 0 \<Longrightarrow>
   673     DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
   674     by auto
   675   then have f''0: "\<And>z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
   676     unfolding inverse_eq_divide by (auto simp add: mult_assoc)
   677   have f''_ge0: "\<And>z :: real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
   678     using `b > 1` by (auto intro!:less_imp_le simp add: divide_pos_pos[of 1] mult_pos_pos)
   679   from f''_ge0_imp_convex[OF pos_is_convex,
   680     unfolded greaterThan_iff, OF f' f''0 f''_ge0]
   681   show ?thesis by auto
   682 qed
   683 
   684 end