-
Notifications
You must be signed in to change notification settings - Fork 53
Conversation
First major re-write. Feedback is welcomed.
@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! |
There was a problem hiding this 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
Outdated
|
||
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), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@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. |
rst_files/wald_friedman.rst
Outdated
|
||
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), |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
rst_files/wald_friedman.rst
Outdated
|
||
return correct, p, t | ||
@with_kw struct Problem |
There was a problem hiding this comment.
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))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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
.
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] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
rst_files/wald_friedman.rst
Outdated
p = bayes_update(p, d0, d1) | ||
cost += c | ||
candidate = min(accept_x0(p, L0), accept_x1(p, L1)) + cost | ||
candidate ≥ target && break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rst_files/wald_friedman.rst
Outdated
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")) |
There was a problem hiding this comment.
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.
rst_files/wald_friedman.rst
Outdated
decision = 0 | ||
break | ||
for (left, right, root) in zip(reverse(left), reverse(right), reverse(roots)) | ||
if left ≠ 1 && right == 1 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
rst_files/wald_friedman.rst
Outdated
elseif p > ub | ||
decision = 0 | ||
break | ||
for (left, right, root) in zip(reverse(left), reverse(right), reverse(roots)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
@arnavs Final check and and if all good, good to merge. |
@Nosferican Sounds good. Will review and merge after class/meetings. |
First major re-write. Feedback is welcomed.