Zulip Chat Archive

Stream: new members

Topic: split_ifs


Iocta (Aug 22 2020 at 08:24):

How can I resolve this goal?

import analysis.normed_space.indicator_function
import measure_theory.set_integral
import analysis.specific_limits
import data.real.basic
import data.nat.basic
import algebra.geom_sum
import tactic.fin_cases
import data.set.basic
import data.real.basic
import data.complex.exponential
import measure_theory.probability_mass_function
import set_theory.cardinal_ordinal

noncomputable theory

universe u
variables {α β : Type u}


def expectation (pr : pmf α) (f : α  real) : real :=
∑' a : α, pr a * f a



def real_of_bool (b: bool) : real := if b = true then 1 else 0


def bernoulli_expectation {p : nnreal} (h : p  1) : expectation (pmf.bernoulli p h) real_of_bool = p :=
begin
unfold expectation,
unfold pmf.bernoulli,
-- p: nnreal
-- h: p ≤ 1
-- ⊢ (∑' (a : bool), ↑(⇑(pmf.of_fintype (λ (b : bool), cond b p (1 - p)) _) a) * real_of_bool a) = ↑p
unfold tsum,
split_ifs,
-- 2 goals
-- p: nnreal
-- h: p ≤ 1
-- h_1: summable (λ (a : bool), ↑(⇑(pmf.of_fintype (λ (b : bool), cond b p (1 - p)) _) a) * real_of_bool a)
-- ⊢ classical.some h_1 = ↑p
-- p: nnreal
-- h: p ≤ 1
-- h_1: ¬summable (λ (a : bool), ↑(⇑(pmf.of_fintype (λ (b : bool), cond b p (1 - p)) _) a) * real_of_bool a)
-- ⊢ 0 = ↑p
end

Kenny Lau (Aug 22 2020 at 09:01):

you would benefit from my advice "stop unfolding!"

Kenny Lau (Aug 22 2020 at 09:07):

(it does not apply to expectation or pmf.bernoulli because there are no theorems for them)

Iocta (Aug 22 2020 at 09:11):

I used unfold tsum because I couldn't see what else to do :-)

Kenny Lau (Aug 22 2020 at 09:11):

import analysis.normed_space.indicator_function
import measure_theory.set_integral
import analysis.specific_limits
import data.real.basic
import data.nat.basic
import algebra.geom_sum
import tactic.fin_cases
import data.set.basic
import data.real.basic
import data.complex.exponential
import measure_theory.probability_mass_function
import set_theory.cardinal_ordinal

noncomputable theory

universe u
variables {α β : Type u}

-- we prefer unicode
def expectation (pr : pmf α) (f : α  ) :  :=
∑' a : α, pr a * f a

-- use `cond`
def real_of_bool (b : bool) :  :=
cond b 1 0

def bernoulli_expectation {p : nnreal} (h : p  1) :
  expectation (pmf.bernoulli p h) real_of_bool = p :=
begin
  unfold expectation pmf.bernoulli real_of_bool pmf.of_fintype, -- unfold things without API
  rw [tsum_fintype, fintype.sum_bool],
  change (p * 1 + (1 - p : nnreal) * 0 : ) = p,
  rw [mul_one, mul_zero, add_zero]
end

Kenny Lau (Aug 22 2020 at 09:12):

@Iocta ask yourself: how can I remove the noise to reveal the essence of the next step? Then you would know that taking the tsum of a fintype should be the same as the sum over the fintype

Iocta (Aug 22 2020 at 09:21):

What made you think to go straight to the fintype api?

Iocta (Aug 22 2020 at 09:25):

(instead of sum-related stuff, or bool stuff, or pmf stuff or measure stuff)

Kenny Lau (Aug 22 2020 at 09:30):

I don't really know how to answer that question

Kenny Lau (Aug 22 2020 at 09:30):

perhaps @Johan Commelin could help

Johan Commelin (Aug 22 2020 at 09:30):

Let me read the question (-;

Kenny Lau (Aug 22 2020 at 09:30):

I mean, "my intuition told me so" is neither helpful nor valid: intuition just means something you can't explain

Kenny Lau (Aug 22 2020 at 09:31):

@Johan Commelin what's the first thing that comes to mind when you see the goal (∑' (a : bool), ↑(⇑⟨λ (b : bool), cond b p (1 - p), _⟩ a) * cond a 1 0) = ↑p?

Kenny Lau (Aug 22 2020 at 09:31):

p : nnreal,
h : p  1
 (∑' (a : bool), (⟨λ (b : bool), cond b p (1 - p), _⟩ a) * cond a 1 0) = p

Johan Commelin (Aug 22 2020 at 09:31):

That tsum is aweful

Kenny Lau (Aug 22 2020 at 09:31):

see we have the same intuition

Johan Commelin (Aug 22 2020 at 09:31):

But yes, turning that into a finset.sum is a great start

Kenny Lau (Aug 22 2020 at 09:31):

but how can you explain it?

Kenny Lau (Aug 22 2020 at 09:31):

it's like Magnus Carlsen explaining his move because it "felt intuitive"

Kenny Lau (Aug 22 2020 at 09:32):

sure, it works for him, but it doesn't explain anything

Johan Commelin (Aug 22 2020 at 09:32):

Well, it's an infinite sum, which means you have to care about convergence etc...

Johan Commelin (Aug 22 2020 at 09:32):

So if you sum over a finite type, then certainly you don't want to care about that

Kenny Lau (Aug 22 2020 at 09:32):

the way it works for me, I see the infinite sum, I think to myself, oh that's "just" a finite sum

Kenny Lau (Aug 22 2020 at 09:32):

so why not rewrite it as a finite sum

Kenny Lau (Aug 22 2020 at 09:33):

my intuition tells me that tsum is something I know nothing about and sum is something I know more about

Kenny Lau (Aug 22 2020 at 09:34):

so it's I guess the same mentality as "unfolding definitions"

Kenny Lau (Aug 22 2020 at 09:34):

but not using unfold

Kenny Lau (Aug 22 2020 at 09:34):

because mathematical definition is not the same as Lean definition

Kenny Lau (Aug 22 2020 at 09:34):

in Lean one object has one definition (Kevin would call this implementation)

Kenny Lau (Aug 22 2020 at 09:34):

in math one object can have 37 definitions

Kenny Lau (Aug 22 2020 at 09:35):

using the appropriate API to me feels like choosing the right definition to unfold

Kenny Lau (Aug 22 2020 at 09:35):

@Iocta I hope this is more helpful than "well my intuition told me so"

Iocta (Aug 22 2020 at 09:37):

Yes, it is. Similar to replacing ite with cond

Kenny Lau (Aug 22 2020 at 09:37):

oh that one

Kenny Lau (Aug 22 2020 at 09:38):

if is for Prop

Kenny Lau (Aug 22 2020 at 09:38):

no need to convert it to Prop if there is a version for bool

Iocta (Aug 22 2020 at 09:39):

switching to a less powerful tool without losing info

Kenny Lau (Aug 22 2020 at 09:39):

it isn't less powerful

Iocta (Aug 22 2020 at 09:39):

well, possibly-infinite -> finite, arbitrary prop -> bool

Kenny Lau (Aug 22 2020 at 09:40):

fair enough

Iocta (Aug 22 2020 at 21:03):

What is this error about?

import measure_theory.set_integral
import analysis.specific_limits
import data.real.basic
import data.nat.basic
import algebra.geom_sum
import tactic.fin_cases
import data.set.basic
import data.real.basic
import data.complex.exponential
import measure_theory.probability_mass_function
import set_theory.cardinal_ordinal

noncomputable theory

universe u
variables {α β : Type u}


def expectation (pr : pmf α) (f : α  ) :  :=
∑' a : α, pr a * f a


def binomial_pmf {p: nnreal} (h: p  1) (n : ) : pmf  :=
 λ k, (nat.choose n k) * p ^ k * (1 - p) ^ (n - k) , sorry 


lemma mul_choose (n k : ) : k * (n.choose k) = n * ((n - 1).choose (k - 1)) := sorry

theorem binomial_expectation {p : nnreal} (h: p  1) (n : ) :
expectation (binomial_pmf h n) coe = n * p :=
begin
unfold expectation binomial_pmf pmf.of_fintype,
suffices : (∑'(k : ), (n.choose k) * p ^ k * (1 - p) ^ (n - k) * k) = n * p,
{
--p: nnreal
--h: p ≤ 1
--n: ℕ
--this: (∑' (k : ℕ), ↑(n.choose k) * p ^ k * (1 - p) ^ (n - k) * ↑k) = ↑n * p
--⊢ (∑' (a : ℕ), ↑(⇑⟨λ (k : ℕ), ↑(n.choose k) * p ^ k * (1 - p) ^ (n - k), _⟩ a) * ↑a) = ↑n * ↑p
    sorry,
},
calc (∑'(k : ), (n.choose k) * p ^ k * (1 - p) ^ (n - k) * k) = ∑' k  (finset.range n), (n.choose k) * p ^ k * (1 - p) ^ (n - k) * k: sorry
... = ∑' k  (finset.range n), k * (n.choose k) * p ^ k * (1 - p) ^ (n - k) : sorry
... = ∑' k  (finset.range n), n * ((n - 1).choose (k - 1)) * p ^ k * (1 - p) ^ (n - k) : sorry
... = n * (∑' k  (finset.range n), ((n - 1).choose (k - 1)) * p ^ k * (1 - p) ^ (n - k)) : sorry
... = n * p * (∑' k  (finset.range n) \ {0}, ((n - 1).choose (k - 1)) * p ^ (k -1) * (1 - p) ^ (n - k)) : sorry
... = n * p * (∑' j  (finset.range (n - 1)), ((n - 1).choose j) * p ^ j * (1 - p) ^ (n - 1 - j)) : sorry
-- type mismatch at application
--   ∑' (H : j ∈ finset.range (n - 1)), ↑((n - 1).choose j) * p ^ j * (1 - p) ^ (n - 1 - j)
-- term
--   λ (H : j ∈ finset.range (n - 1)), ↑((n - 1).choose j) * p ^ j * (1 - p) ^ (n - 1 - j)
-- has type
--   j ∈ finset.range (n - 1) → nnreal : Type
-- but is expected to have type
--   ?m_1 → nnreal : Type ?
-- state:
... = n * p : sorry
end

Iocta (Aug 22 2020 at 21:04):

Also, how do I fill that first sorry (the suffices one)?

Iocta (Aug 22 2020 at 21:19):

... of course maybe my suffices statement is wrong :-)

Kevin Buzzard (Aug 22 2020 at 21:24):

I don't know much about probability, but your mul_choose is definitely wrong -- if ever you use natural subtraction you can be pretty sure that something is wrong, because natural subtraction is a pathological function.

Iocta (Aug 22 2020 at 21:48):

@Kevin Buzzard Do you think I should try to reformulate binomial_pmf to not mention n - k?

Kevin Buzzard (Aug 22 2020 at 21:48):

n-k is fine if you know k<=n. But you don't know this in mul_choose

Kevin Buzzard (Aug 22 2020 at 21:49):

I mean, you don't know k>=1 so you'll be in trouble with k-1 if k=0

Iocta (Aug 22 2020 at 21:57):

So do I want to just add an (h: k >= 1) or try to build the >=1 into the terms some other way?

Iocta (Aug 22 2020 at 21:57):

lemma mul_choose (w b: ) (h: w + b  1): w * ((w + b).choose w) = (w + b) * (((w + b - 1).choose (w - 1)) := sorry

getting rid of n doesn't seem to help

Iocta (Aug 22 2020 at 21:58):

(still need the hypothesis)

Iocta (Aug 22 2020 at 22:03):

lemma mul_choose (w_minus_1 b: ): (w_minus_1 + 1) * ((w_minus_1 + 1 + b).choose (w_minus_1 + 1)) = (w_minus_1 + b) * (((w_minus_1 + b ).choose w_minus_1) := sorry

¯\_(ツ)_/¯

Kevin Buzzard (Aug 22 2020 at 22:04):

All I'm saying was that your lemma as it stood was false because k=0 gave counterexamples. What you should do instead is entirely up to you but for sure you can't use a false lemma.

Kevin Buzzard (Aug 22 2020 at 22:05):

And the reason I took one look at it and suspected it might be false was that it used unsafe natural subtraction, subtracting one from things which hadn't been assumed to be >= 1.

Iocta (Aug 22 2020 at 22:06):

Sure that makes sense

Kevin Buzzard (Aug 22 2020 at 22:16):

I think your error is because you're just not using the ∑' syntax correctly. I've never used ∑' before but you seem to have ∑' (k : nat) and ∑' k \in (some finset) which doesn't bode well. What is the syntax of ∑'?

Iocta (Aug 22 2020 at 22:31):

I've used it above in the expectation definition ∑' a : α, pr a * f a and I agree that's different from here with the \in.

Iocta (Aug 22 2020 at 22:33):

So what I mean is (∑ j in (finset.range (n - 1)), _

Iocta (Aug 22 2020 at 22:41):

or so I thought from here

* ` x in s, f x` is notation for `finset.sum s f` (assuming `β` is an `add_comm_monoid`)

but

variable (k: nat)
#eval ( k in ((finset.range 5) \ {0}), k)

doesn't even parse

Dan Stanescu (Aug 22 2020 at 23:06):

This works for me and the result is 10:

#eval ( k in ((finset.range 5) \ {0}), k) -- 10

Dan Stanescu (Aug 22 2020 at 23:07):

Do you have the correct imports?

Iocta (Aug 22 2020 at 23:07):

Which imports do you have?

Kyle Miller (Aug 22 2020 at 23:08):

Make sure you have

open_locale big_operators

to include the notation.

Dan Stanescu (Aug 22 2020 at 23:08):

I have all of these in a file for a different purpose, but you probably don't need the first two:

import data.polynomial
import data.real.basic
import algebra.big_operators

open_locale big_operators

Iocta (Aug 22 2020 at 23:09):

Ah, that's the trick

Iocta (Aug 23 2020 at 19:57):

What's the notation for (α × β) but with countable elements? Is that def myprod (t : ℕ → Type u) : Π (n: nat), t n := _?


Last updated: Dec 20 2023 at 11:08 UTC