Skip to content

Instantly share code, notes, and snippets.

@micycle1
micycle1 / _.py
Created October 10, 2023 16:33
Bond duration, convexity and return
def modified_duration(y, m):
"""
Approximates the modified duration of a risk-free bond at par value.
:param y: Yield-to-maturity in decimal terms
:param m: Maturity in years
:return: Modified duration of a risk-free bond
"""
return (1-(1/(1+0.5*y)**(2*m)))/(y)
@micycle1
micycle1 / func.py
Created July 4, 2023 15:20
Function to print the date of the first non-null value for each column in a DataFrame, sorting the output by date.
def print_first_non_null_dates(df):
non_null_dates = {}
for column in df.columns:
non_null_values = df[column].dropna()
if not non_null_values.empty:
first_non_null_date = non_null_values.index[0]
non_null_dates[column] = first_non_null_date
sorted_dates = sorted(non_null_dates.items(), key=lambda x: x[1])
@micycle1
micycle1 / howto.md
Last active January 22, 2023 15:19
HQ Album Art from Apple Music
@micycle1
micycle1 / function
Created December 27, 2022 13:34
Java fast fixed-divisor integer division.
/**
* Computes a pair of magic numbers used to optimise integer division by a fixed
* divisor <code>d</code>.
* <p>
* The function is given the maximum value of the dividend <code>nmax</code> and
* the divisor <code>d</code>. It returns a pair of integers: the magic number
* <code>m</code> and a shift amount <code>p</code>.
* <p>
* To divide a dividend x by d, one multiplies x by m and then shifts the (full
* length) product right p bits: <code>x / d === ((x * m) >> p)</code>.
@micycle1
micycle1 / example.java
Created September 1, 2021 17:06
Make java.applet.Applet runnable
public class T2Q extends java.applet.Applet {
public static void main(String[] args) {
T2Q applet = new T2Q();
applet.init();
applet.start();
// Create a window (JFrame) and make applet the content pane.
javax.swing.JFrame window = new javax.swing.JFrame("T2Q");
window.setContentPane(applet);
@micycle1
micycle1 / MinimumEnclosingTriangle.java
Created April 19, 2021 14:20
MinimumEnclosingTriangle
import org.locationtech.jts.geom.Coordinate;
import java.util.ArrayList;
import java.util.List;
/**
* Implementation of linear minimum area enclosing triangle algorithm.
* Computational and Applied Mathematics, 35(2), 423–438.
* doi:10.1007/s40314-014-0198-8
*
@micycle1
micycle1 / EarClippingTriangulator.java
Created March 28, 2021 19:56
GDX EarClippingTriangulator
package com.badlogic.gdx;
/**
* A simple implementation of the ear cutting algorithm to triangulate simple
* polygons without holes. For more information:
* <ul>
* <li><a href=
* "http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/97/Ian/algorithm2.html">http://cgm.cs.mcgill.ca/~godfried/
* teaching/cg-projects/97/Ian/algorithm2.html</a></li>
* <li><a href=
@micycle1
micycle1 / fastCubic
Created February 7, 2021 22:40
A fast solution to cubic equations with three real roots
http://momentsingraphics.de/CubicRoots.html#_Blinn06a
Returns the three real roots of the cubic polynomial (of form 1 + 2x + 3x^2 + 4x^3)
float3 SolveCubic(float4 Coefficient){
// Normalize the polynomial
Coefficient.xyz/=Coefficient.w;
// Divide middle coefficients by three
Coefficient.yz/=3.0f;
// Compute the Hessian and the discrimant
@micycle1
micycle1 / clonePGraphics.java
Created January 4, 2021 19:00
Code to clone PGraphics via reflection
static PGraphics cloneGraphics(PGraphics source) {
final String renderer;
switch (source.parent.sketchRenderer()) {
case "processing.opengl.PGraphics2D":
renderer = P2D;
break;
case "processing.opengl.PGraphics3D":
renderer = P3D;
break;
http://stereopsis.com/2div.html
Ben and 2 Divides
Two years ago, Ben Weiss (the guy who wrote Kai's Power Tools, Goo, Soap, etc.) was telling me about this idea he had to combine two divides. I just didn't pay attention.
Luckily, he told me again.
Ben's idea is really simple, and I use it everywhere now.
2 divides at once
Suppose you want to compute: