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