Skip to content

Instantly share code, notes, and snippets.

import numpy as np
import scipy.stats as stats
def Cucconi (x, y):
# Calculates the test statistic for the Cucconi two-sample location-scale test
# assume x is smallest, swap as needed
m = len(x)
@jeancroy
jeancroy / distcorr.py
Created November 8, 2020 04:56 — forked from wladston/distcorr.py
Distance correlation with p-value
from scipy.spatial.distance import pdist, squareform
import numpy as np
import copy
def distcorr(Xval, Yval, pval=True, nruns=500):
""" Compute the distance correlation function, returning the p-value.
Based on Satra/distcorr.py (gist aa3d19a12b74e9ab7941)
>>> a = [1,2,3,4,5]
Basic Statistic
P(Success) = 1/N
Add a corrector, Make it difference between theorical probability and observation
P(Success) = 1/N + Corrector
P(Success) = 1/N + (1/N - SucessCount / TotalCount )
Adjust for initial phase:
Prevent Division per zero & reduce the strength for small count
// Sample from a normal distribution, truncated between -1 and 1
double MultiObjectiveCompactDifferentialEvolution::sampleValue(double mu, double sigma) {
if (sigma < 1e-8) // Should avoid infinity error below
return mu;
try {
double sqrt2Sigma = SQRT_2 * sigma;
double erfMuNeg = boost::math::erf((mu - 1) / sqrt2Sigma);
double erfMuPlus = boost::math::erf((mu + 1) / sqrt2Sigma);
function IndexStore() {
// Main structure
// Format is dict key => array of item indices
this.indicesForKey = {};
// List of indexed items.
this.items = [];
// Helper to prevent duplicate on items.
@jeancroy
jeancroy / float2rat.js
Last active December 21, 2017 20:44 — forked from joni/float2rat.js
A JavaScript function to find short rational number representations of floating point numbers, with the help of some continued fractions.
// See http://jonisalonen.com/2012/converting-decimal-numbers-to-ratios/
// Also https://en.wikipedia.org/wiki/Diophantine_approximation
// or french https://fr.wikipedia.org/wiki/Fraction_continue_et_approximation_diophantienne
function float2rat(x) {
var tolerance = 1.0E-6;
var max_iter = 100; // Limit on the amount of work we want to do
var max_den = 1000; // When is a fraction not really more helpfull than decimal ?
// Explore very small and very large, positive and negative numbers.
//
// Spend about 50% of time in + and 50% in - region. (Or fix m to 1 fo positive only)
// Spend about 50% of time in +- 10^N and 50% in +- 10^-N region.
// Spend about 50% of time between +-[1/1000, 1000] range and 50% of time outside of it, without limit.
//
// More precisely 50% of values are in [3^-s, 3^s] with "s" is the power of "q".
// As a guideline q^6: 1/1000..1000, q^4: 1/100..100, q^2: 1/10..10
// One may notice than 3^6=729<1000,
// In practice what this mean is that 1000 is in the 60% most likely outcomes instead of 50%.
// Convert uniform random [0,1] to normal random ~ N(0,1)
// norm_rnd = Probit( random() )
//
// Probit(p) ~= logit(p)*sqrt(pi/8)
// logit(p) = log( p / (1-p) )
//
Function RandomNormal(){
var c = 0.626657068657750125603941 // sqrt(pi/8)
@jeancroy
jeancroy / SelectInverseRank.js
Last active July 19, 2023 17:52
Random selection form a list with power law probability (closed form solution) .
//
// Select the n-th item with probability `1/n`.
// Return an 0 based index (`i = n-1`)
//
// If parameter `a` is given, we select from probability `1/(n^a)`
// In particular, if a negative value of `a` is given, we select with probability `n^y, y = -a`;
//
// The code use the inverse of the cumulative probability function, to map the uniform distribution to the power distribution.
//
// Caveat:
public static class SendGridDeliveryServices
{
private static string _sendGridApiKey = "...";
public static bool SendMailToCustomerServices(string name, string email, string subject, string body)
{
var from = new EmailAddress("system@xyz.com", name);
var reply = new EmailAddress(email, name);