Skip to content

Instantly share code, notes, and snippets.

@contrasting
contrasting / dd.md
Last active July 26, 2023 11:07
Derivation of difference in differences estimator

Recall the standard difference-in-differences setup:

$$y_i = \gamma_0 + \gamma_1 post_i treat_i + \gamma_2 treat_i + \gamma_3 post_i + e_i$$

This is equivalent to estimating two separate equations for both the control and treatment groups:

$$y_i = \gamma_0 + \gamma_3 post_i + e_i$$

$$y_i = (\gamma_0 + \gamma_2) + (\gamma_1 + \gamma_3) post_i + e_i$$

@contrasting
contrasting / ovb.md
Last active August 11, 2023 16:10
Matrix form derivation of omitted variable bias

Consider the linear regression model:

$$y = X \beta + e$$

Choose beta to minimise the SSE. The least squares estimator for beta is (derivation omitted):

$$\hat{\beta} = (X'X)^{-1}X'y$$

Now suppose the true form includes an omitted variable in the error:

@contrasting
contrasting / nn_lib.py
Created October 16, 2022 14:38
Mini NN Library
import numpy as np
import pickle
def xavier_init(size, gain=1.0):
"""
Xavier initialization of network weights.
"""
low = -gain * np.sqrt(6.0 / np.sum(size))
high = gain * np.sqrt(6.0 / np.sum(size))
@contrasting
contrasting / reuse.md
Last active March 24, 2022 11:26
How to reuse code between widgets in Flutter

How to reuse code between widgets in Flutter

Adapted from some notes I took in https://github.com/contrasting/Clutter.

Contents

  • Why is inheritance not a good idea?
  • Containment pattern
  • Specialization pattern
  • Builder pattern
@contrasting
contrasting / timeformat.dart
Created July 9, 2021 10:42
Format unix time into pretty string, WhatsApp style
final yMMMd = DateFormat.yMMMd();
final MMMd = DateFormat.MMMd();
final E = DateFormat.E().add_d();
final Hm = DateFormat.Hm();
String formatUnixTime(int unixMilli) {
final sentTime = DateTime.fromMillisecondsSinceEpoch(unixMilli);
final currTime = DateTime.now();
DateFormat formatter;
@contrasting
contrasting / getProfile.js
Created March 23, 2021 18:44
function to get user, but feels superfluous
exports.getProfile = functions
.region(REGION)
.https.onCall(async (data, context) => {
await init(context);
// let sampleData = "userId";
if (typeof(data) !== "string") throw new functions.https.HttpsError("invalid-argument");
let me = await User.findOne({ _id: context.auth.uid }).select(selectOn).lean();
@contrasting
contrasting / event_source.dart
Last active March 15, 2021 11:49
How to access EventSource (SSE) API from dart
import 'dart:html';
class EventSourceModel {
static const kEndPoint = 'https://myfirebaseendpoint.com';
EventSource es;
final StreamController<UserMatch> _controller = StreamController<UserMatch>();
Stream<UserMatch> get stream => _controller.stream;
EventSourceModel() {
@contrasting
contrasting / fill.html
Created April 22, 2020 17:18
I don't know html
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
margin: 0;
@contrasting
contrasting / GoUphill.cs
Last active December 29, 2019 19:36
Fun little experiment to rewrite the algorithm for going uphill with recursion rather than iteration
private void GoUphill(VectorP potentialPos, VectorP response, int y, float potentialDist)
{
// found non colliding height
if (response == VectorP.zero)
{
// move to this position
UpdatePosition(potentialPos);
return;
}
@contrasting
contrasting / Backtracking.cs
Created December 28, 2019 16:59
One way of resolving collisions by using backtracking. However, this method suffers from instability - objects are never at rest. Potential solution: http://www.allenchou.net/2014/01/game-physics-stability-slops/
VectorP response = GetCollisionResponse(potentialPos, velocity);
// if there was an actual collision
if (response != VectorP.zero)
{
// collision resolution: search in direction of opposite velocity for potentialPos until there is no collision
VectorP oppositeVelocity = -velocity.Normalise();
for (VectorP testResponse = response; testResponse != VectorP.zero; testResponse = GetCollisionResponse(potentialPos, velocity))
{
// strange loop i know