Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Rewrite of WF lecture #156

Merged
merged 3 commits into from
Oct 20, 2018
Merged

Rewrite of WF lecture #156

merged 3 commits into from
Oct 20, 2018

Conversation

Nosferican
Copy link
Collaborator

First major re-write. Feedback is welcomed.

First major re-write. Feedback is welcomed.
@jstac
Copy link
Contributor

jstac commented Oct 5, 2018

@Nosferican I think this looks like a big improvement. I defer to others in terms of knowledge of correct Julia style but to me this looks great. It's clear, readable, concise and neat.

Thanks!

Copy link
Member

@sglyon sglyon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nosferican this looks fantastic!

I have a few questions/comments throughout that I think we should address.

One other question is if we have a build infrastructure in place to build the site for each PR as part of our CI and make the built version visible so reviewers can see the final product?

I've been a bit detached from the build tech for a little while so I'm not sure if I'm asking for something totally unreasonable or perhaps we already have it??

rst_files/wald_friedman.rst Show resolved Hide resolved

plot(plot(reshape(Beta.(1:2:3, 1:2:3), 2, 1), labels = ["f_0", "f_1"], title = "Original Distributions"),
plot(reshape(MixtureModel.(Beta, Ref((p -> (p, p)).(1:2:3)), (p -> [p, one(p) - p]).(0.25:0.25:0.75)), 3, 1),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is clever!... maybe a bit too cleverl

Does the syntax of broadcasting over an anonymous function (e.g. (p -> [p, one(p) - p]).(0.25:0.25:0.75))) have the potential to scare some of our target audience?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can keep the cleverness, but maybe open up the terseness a little. Something like the following? Also fixes an issue where both subplots had the same metadata.

dists = [Beta(1., 1.), Beta(3., 3.)] # Raw distributions. 
mix(dists, coef1) = MixtureModel(Beta, params.(dists), [coef1, 1-coef1]) # To generate our mixture models. 
mixes = mix.(Ref(dists), [0.25, 0.5, 0.75])

plot(
    plot(dists, labels = ["f_0", "f_1"], title = "Original Distributions"),
    plot(mixes, labels = ["1/4-3/4", "1/2-1/2", "3/4-1/4"], title = "Distribution Mixtures"), 
    ylab = "Density", ylim = (0, 2), layout = (2, 1) # Global settings across both plots
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I never like clever. Terse is not necessarily a virtue and don't forget point 1 of https://github.com/QuantEcon/lecture-source-jl/blob/master/style.md#basic-principles

I think @arnavs has provided something which we can teach a little easier.

Copy link
Collaborator Author

@Nosferican Nosferican Oct 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strive to be, hehe. I took from the @arnavs version and made it a bit more slow to help the reader follow.

Run You Clever Boy, and Remember. - Clara Oswald

.. math::

J(p) = J(p_i) + (p - p_i) \frac{J(p_{i+1}) - J(p_i)}{p_{i+1} - p_{i}}
We implement the cost functions for each choice considered in the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a break from writing the style adopted in most of the lectures.

In most places on lectures.quantecon.org we have one sentence paragraphs without punctuation at the end. Here we have replaced that with more conventional paragraphs that have multiple sentences separated by punctuation marks.

I'm not casting a vote here for which I think we should have going forward, my purpose is only to point out that the change occurred

This applies to a few places in the diff, but I'll only mention it once

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Let's stick to one sentence lines with a line break after and no full stop.

That's something we can change later but let's at least be uniform.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I have modified it to be consistent with that part of the style.

@jstac
Copy link
Contributor

jstac commented Oct 5, 2018

@mmcky Can answer your questions on the build infrastructure, @sglyon

@arnavs
Copy link
Member

arnavs commented Oct 5, 2018

@sglyon Specifically to the CI, we're looking into a Docker-based solution. Something like https://docs.travis-ci.com/user/docker/? I think @jlperla wrote an issue on this, but I can't find it.

arnavs
arnavs previously requested changes Oct 5, 2018
rst_files/wald_friedman.rst Show resolved Hide resolved

plot(plot(reshape(Beta.(1:2:3, 1:2:3), 2, 1), labels = ["f_0", "f_1"], title = "Original Distributions"),
plot(reshape(MixtureModel.(Beta, Ref((p -> (p, p)).(1:2:3)), (p -> [p, one(p) - p]).(0.25:0.25:0.75)), 3, 1),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can keep the cleverness, but maybe open up the terseness a little. Something like the following? Also fixes an issue where both subplots had the same metadata.

dists = [Beta(1., 1.), Beta(3., 3.)] # Raw distributions. 
mix(dists, coef1) = MixtureModel(Beta, params.(dists), [coef1, 1-coef1]) # To generate our mixture models. 
mixes = mix.(Ref(dists), [0.25, 0.5, 0.75])

plot(
    plot(dists, labels = ["f_0", "f_1"], title = "Original Distributions"),
    plot(mixes, labels = ["1/4-3/4", "1/2-1/2", "3/4-1/4"], title = "Distribution Mixtures"), 
    ylab = "Density", ylim = (0, 2), layout = (2, 1) # Global settings across both plots
)

EJ = dot(curr_dist, J.(tp1_dist))

return EJ
accept_x0(p, L0) = (one(p) - p) * L0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these functions are excellent. Very close to the bare math.


return correct, p, t
@with_kw struct Problem
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we're not using a NamedTuple? Like:

Problem = @with_kw (d0 = Beta(1,1), d1 = Beta(9,9), L0 = 2, L1 = 2, c = 0.2, p = 0.5, n = 100, return_output = false) # Constructor for problem objects. 
DefaultProblem = Problem()
ModifiedProblem = Problem(d0 = Beta(1, 3))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes: https://github.com/QuantEcon/lecture-source-jl/blob/master/style.md#type-annotations-parameters-and-generic-programming

We want to avoid these kinds of structs at all costs. Not to mention that this struct is not templatized for the distributions, so it would be holding abstract types.

Luckily, named tuples do it all.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I have made the changes to use NamedTuple.

@jlperla
Copy link
Member

jlperla commented Oct 5, 2018

There are improvements here, but I strongly feel we should avoid rewriting these complicated lectures and focus on the many, many simple ones that exist.

The patterns for things like dealing with fixed points and dynamic programming in the simple examples have not been established. My fear is that starting here makes us go from a clean functional-style code that matches the math, and move towards terse, self-contained, and clever code with lots of for and while loops.

So my feeling is: merge this with minor changes, and then go back, starting with the simplest examples and working up.

trials = fill(0, n)
for trial in 1:n
truth = rand(1:2)
d = (d0, d1)[truth]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very terse syntax. I don't think that this is going to be easy for people to read.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used some comments to help read the code better.

p = bayes_update(p, d0, d1)
cost += c
candidate = min(accept_x0(p, L0), accept_x1(p, L1)) + cost
candidate ≥ target && break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J_out[p_ind] = min(p_c_0, p_c_1, p_con)
function decision_rule(d0, d1, L0, L1, c)
function cost(p, d0, d1, L0, L1, c)
c ≥ zero(c) || throw(ArgumentError("Cost must be non-negative"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/QuantEcon/lecture-source-jl/blob/master/style.md#general-control-structures-and-code-organization Avoid the short-circuiting pattern when you just want to do an assert.

These is very tough to read for an introductory user.

decision = 0
break
for (left, right, root) in zip(reverse(left), reverse(right), reverse(roots))
if left ≠ 1 && right == 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ascii for control flow

Copy link
Collaborator Author

@Nosferican Nosferican Oct 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace && with &? Or with !=? is closer to the math.
I saw the style guide had != over , but was in the allowed unicode guide. I don't think there is much sense to use outside of control flow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those the style also mean I should change / for control flow? Those are the other non-ASCII used in control flow in this lectures I believe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that is right. Though I really do love the unicode for ≠ if we are writing up a line of math where it is used for set inclusion, etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For set inclusion it would be or . I don't think is used outside control flow or tests... Very few exceptions is any. Probably best to discuss it later on the style guide, but I would cast my vote towards keeping control flow operations close to the math as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, you are right. Then I don't think there is any reason to use ≠ except when doing DSL-y type stuff

Control flow is part of an algorithm, and not a direct implementation of a mathematical formula in the lecture notes. Making the distinction between the direct implementation of the formulas from the math and the control flow of algorithms is a key goal.

elseif p > ub
decision = 0
break
for (left, right, root) in zip(reverse(left), reverse(right), reverse(roots))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ascii for control flow. But I think that a comment could help a lot to explain what is going on here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is non-ASCII being used for control flow?

@Nosferican Nosferican mentioned this pull request Oct 6, 2018
53 tasks
@Nosferican
Copy link
Collaborator Author

@arnavs Final check and and if all good, good to merge.

@arnavs
Copy link
Member

arnavs commented Oct 18, 2018

@Nosferican Sounds good. Will review and merge after class/meetings.

@Nosferican Nosferican merged commit d819cd3 into master Oct 20, 2018
@arnavs arnavs deleted the WF_Re-write branch December 14, 2018 19:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants