Skip to content

Instantly share code, notes, and snippets.

@Michael-J-Ward
Michael-J-Ward / agg_example.txt
Last active February 6, 2016 19:13
mongoDB aggregation example
> db.students.find().limit(2).pretty()
{
"_id" : 2,
"name" : "Corliss Zuk",
"scores" : [
{
"score" : 67.03077096065002,
"type" : "exam"
},
{
@Michael-J-Ward
Michael-J-Ward / central_limit_demo.py
Last active April 24, 2016 19:41
Demonstrating Central Limit theorem
# Demonstrating the Central Limit Theorem for MITs Probabilistic Systems course
import numpy as np
import matplotlib.pyplot as plt
def genTest(distribution,**kwargs):
"""Returns a score generating function from the given distribution"""
def test(num_students):
kwargs['size'] = num_students
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
Cluster Week Overload
0 1 1 429
1 1 1 -247
2 1 1 -655
3 1 1 682
4 1 1 -178
5 1 1 -791
6 1 1 262
7 1 1 -46
8 1 1 -358
@Michael-J-Ward
Michael-J-Ward / FunSetSuite.scala
Created June 17, 2016 18:29
test suite for week2 of Functional Programming in scala
package funsets
import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
/**
* This class is a test suite for the methods in object FunSets. To run
class Magic_Formula(CustomFactor):
inputs = [mstar.income_statement.ebit, mstar.valuation.enterprise_value,
mstar.operation_ratios.roic]
outputs = ['earnings_yield', 'roic']
window_length = 1
def compute(self, today, assets, out, ebit, ev, roic):
out.earnings_yield[:] = ebit[-1] / ev[-1]
out.roic[:] = roic[-1]
@Michael-J-Ward
Michael-J-Ward / polymorphic_serializer.py
Created August 4, 2017 16:42
Implementation of Polymorphic serializer
"""
PolymorphicSerializer Implementation taken from this stack overflow answer:
https://stackoverflow.com/a/44727343/1948982
Copied here to imortalize it to remove the risk of the answer dissappearing
"""
class PolymorphicSerializer(serializers.Serializer):
"""
Serializer to handle multiple subclasses of another class
import zlib
import itertools
def get_signed_value(unsigned_value):
bitsize = 32
return unsigned_value if unsigned_value < (1 << bitsize-1) else unsigned_value - (1 << bitsize)
def create_checksum_string(bids, asks):
bids = (f"{price}:{qty}" for price, qty, _, _ in bids)
asks = (f"{price}:{qty}" for price, qty, _, _ in asks)
import base64
import datetime
import hmac
def get_timestamp() -> str:
"""
The OKEX api requires the timestamp to be in exactly this format
example: '2018-10-23T17:27:40.340Z'
step_0_depth = {"ch":"market.BTC_CQ.depth.step0","ts":1567795124843,"tick":{"mrid":17445416471,"id":1567795124,"bids":[[10514.47,6565],[10514.46,80],[10514.02,1],[10514,53],[10513.6,10],[10513.59,5],[10513.58,153],[10513.34,9],[10512.99,11],[10512.71,8],[10512.7,217],[10512.69,233],[10512.65,40],[10512.64,2],[10512.6,20],[10512.48,8],[10512.3,3],[10511.59,4],[10511.21,2],[10510.97,23],[10510.64,32],[10510.55,11],[10510,7],[10509.88,5],[10509.63,8],[10509.54,17],[10509.26,80],[10508.49,42],[10508.23,15],[10507.76,355],[10507.74,160],[10507.7,133],[10507.51,31],[10507.37,145],[10506.7,132],[10506.6,218],[10506.18,80],[10506.17,80],[10506,869],[10505.78,200],[10505.73,27],[10505.66,20],[10505.62,27],[10505.16,82],[10505,22],[10504.79,54],[10504.67,190],[10504.51,15],[10504.45,54],[10504.35,10],[10504.23,70],[10504.04,5],[10503.66,20],[10503.6,40],[10503.43,210],[10503.36,2],[10503.25,78],[10503.01,1],[10502.65,233],[10502.5,81],[10502.07,200],[10501.85,378],[10501.84,6],[10501.82,5],[10501.81,50],[10501.59,260],
@Michael-J-Ward
Michael-J-Ward / modified_monty.py
Last active September 22, 2019 18:38
Monte Carlo'ing a modified monty python variant
"""Monte Carlo'ing Hillogram's Monty Hall Variant
Hillelogram on twitter came up with a surprising Monty Hall Variant: https://twitter.com/hillelogram/status/1175125663247998978
[BEGIN EDIT]
@hillelogram gives credit to this paper https://pubs.aeaweb.org/doi/pdfplus/10.1257/jep.33.3.144
/u/edderiofer on reddit pointed out that this is in fact the "Monty Crawl" variant from here: http://www.probability.ca/jeff/writing/montyfall.pdf