Saturday, June 20, 2020

Amazon Principle Engineer Review

In Amazon, if a feature spans multiple systems and teams; it usually requires a review from a Principal Engineer (PE review).

In one of the internal talks, a Principal Engineer (PE) shared a simple technique they employ to drive the PE review. It was to repeatedly ask "why" like a five year old.

For example the conversation in a PE review can go something like this:

Engineer: We're creating a system to duplicate an item in different marketplaces.
PE: Why?
Engineer: To allow customers to buy the same item in different marketplaces.
PE: Why does it have to be duplicated for that to happen?
Engineer: Because each marketplace has a separate catalog
PE: Why is it separate?
And so on...

This technique forces the engineers to dig deeper into their own design and question the foundations on which it is built.

It is possible we're treating the symptoms of an issue instead of addressing the root cause and by repeatedly asking "why", we may expose some assumptions we've made or issues we did not consider in our initial design.

Good thing about this technique is that you don't have to be a Principal Engineer to use it. Even if we're fresh out of college, we can ask "why" in a design meeting. If it doesn't uncover anything new, it will at least help us understand the system deeply.

“Millions saw the apple fall, Newton was the only one who asked why?” - Bernard M. Baruch

Sunday, June 7, 2020

Bias Variance Tradeoffs - Classic

  • More training examples fixes high variance but not high bias.
  • Fewer features fixes high variance but not high bias.
  • Additional features fixes high bias but not high variance.
  • The addition of polynomial and interaction features fixes high bias but not high variance.
  • When using gradient descent, decreasing lambda can fix high bias and increasing lambda can fix high variance (lambda is the regularization parameter).
  • When using neural networks, small neural networks are more prone to under-fitting and big neural networks are prone to over-fitting. Cross-validation of network size is a way to choose alternatives.


Error Metrics for Skewed Classes

It is sometimes difficult to tell whether a reduction in error is actually an improvement of the algorithm.
  • For example: In predicting a cancer diagnoses where 0.5% of the examples have cancer, we find our learning algorithm has a 1% error. However, if we were to simply classify every single example as a 0, then our error would reduce to 0.5% even though we did not improve the algorithm.
This usually happens with skewed classes; that is, when our class is very rare in the entire data set.
Or to say it another way, when we have lot more examples from one class than from the other class.
For this we can use Precision/Recall.
  • Predicted: 1, Actual: 1 --- True positive
  • Predicted: 0, Actual: 0 --- True negative
  • Predicted: 0, Actual, 1 --- False negative
  • Predicted: 1, Actual: 0 --- False positive
Precision: of all patients we predicted where y=1, what fraction actually has cancer?
True PositivesTotal number of predicted positives=True PositivesTrue Positives+False positives
Recall: Of all the patients that actually have cancer, what fraction did we correctly detect as having cancer?
\dfrac{\text{True Positives}}{\text{Total number of actual positives}}= \dfrac{\text{True Positives}}{\text{True Positives}+\text{False negatives}}
These two metrics give us a better sense of how our classifier is doing. We want both precision and recall to be high.
In the example at the beginning of the section, if we classify all patients as 0, then our recall will be \dfrac{0}{0 + f} = 0, so despite having a lower error percentage, we can quickly see it has worse recall.
Accuracy = \frac {true positive + true negative} {total population}
Note 1: if an algorithm predicts only negatives like it does in one of exercises, the precision is not defined, it is impossible to divide by 0. F1 score will not be defined too.

Trading Off Precision and Recall

We might want a confident prediction of two classes using logistic regression. One way is to increase our threshold:
  • Predict 1 if: h_\theta(x) \geq 0.7
  • Predict 0 if: h_\theta(x) < 0.7
This way, we only predict cancer if the patient has a 70% chance.
Doing this, we will have higher precision but lower recall (refer to the definitions in the previous section).
In the opposite example, we can lower our threshold:
  • Predict 1 if: h_\theta(x) \geq 0.3
  • Predict 0 if: h_\theta(x) < 0.3
That way, we get a very safe prediction. This will cause higher recall but lower precision.
The greater the threshold, the greater the precision and the lower the recall.
The lower the threshold, the greater the recall and the lower the precision.
In order to turn these two metrics into one single number, we can take the F value.
One way is to take the average:
\dfrac{P+R}{2}
This does not work well. If we predict all y=0 then that will bring the average up despite having 0 recall. If we predict all examples as y=1, then the very high recall will bring up the average despite having 0 precision.
A better way is to compute the F Score (or F1 score):
\text{F Score} = 2\dfrac{PR}{P + R}
In order for the F Score to be large, both precision and recall must be large.
We want to train precision and recall on the cross validation set so as not to bias our test set.


References

Saturday, June 6, 2020

Stock market is not the Economy : Unemployment numbers



Few things to note from the table

  • During the 2008 financial crisis, unemployment rate went from 4.9% to 5.6% during Jan-June, 2008. S&P500 was at 1278 on June 27, 2009, down 14% from ~1500 peak
  • Unemployment rate increased more rapidly during the later half of 2008 to end at 7.2% in December. H2, 2008 saw the fall of Lehman. S&P500 was at 887 on Dec 19, 2008 down 30% from June, 2008 and down 40% from ~1500 peak.
  • Unemployment bottomed at 10% in October 2009
  • Stock market bottomed at 683 in March, 2009 when the unemployment was at 8.7%
Stock market reflects the rate of change of bad news. 
  • Rate of change of unemployment in H1, 2008 = -.15% per month
  • Rate of change of unemployment in H2, 2008 = -.23% per month
  • Rate of change of unemployment in H1, 2009 = -.28% per month
  • Rate of change of unemployment in H2, 2009 = -.06% per month
The rate of change of unemployment numbers clearly show that the rate was highest in H1, 2009 which co-incides with the market bottom. The absolute unemployment percentage kept getting worst in H2, 2009, but the rate of change of bad news kept decreasing. So the market had already started increasing when the economy still kept shedding jobs. The economy hit bottom in terms of unemployment in October 2009 when the S&P500 was around ~1100 which was about 57% above the lows in March 2009 and still 33% below the ~1500 market top.

The stock market is a forward looking vehicle. During a recession, in order to look for the bottom, look for the point where the market starts ignoring the bad news.


Reference

Thursday, May 28, 2020

Attention Explained


Screenshot of Andrew Ng's explanation of attention model from the deeplearning.ai course




The problem with regular encoder decoder architectures arise when we have long sentences because RNNs dont do well in these scenarios. For eg : while translating a long sentence humans probably dont read the whole sentence and then translate it. The human mind probably reads parts of the sentence and then processes the translation for that part. This leads us to attention models. While translating a word, it weighs in the inputs to the word differently. 


References

Encoder Decoder Explained

Neural Machine Translation

x1,x2,....,xTx - input sentence
y1,y2,....yTy - output sentence
Tx!=Ty, which means the length of the input sentence can be different from the output sentence


The problem with regular encoder decoder architectures arise when we have long sentences because RNNs dont do well in these scenarios. For eg : while translating a long sentence we probably dont read the whole sentence and then translate it. The human mind probably reads parts of the sentence and then processes the translation. This leads us to attention models. While translating a word, it weighs in the inputs to the word differently. We will cover attention models in a separate post. We will explore another encoder decoder architecture where the input is an image, hence the encoder produces an image encoding. 



Image Caption Generation

Encoder : Alexnet or any other Computer Vision model can generate the image encoding
Decoder : RNN like architecture can decode the encoding to create the image caption
y1, y2..., yT - image encoding


Books I am reading