This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.linear_model import LinearRegression | |
import pickle | |
import random | |
import numpy as np | |
""" | |
Sample data preparation for the partition. | |
-------- | |
This example uses linear function y=mx+b where m is the tenant / company | |
specific bias and b is the error. LinearRegression is then used to find |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* Histogram "bins" numbers in the array X in to group ranges. | |
* Example usage histogram([1,5,2,4,2,5,2,3,1], 2) would return back [5,2,2] where bin ranges are [1-2, 3-4, 5-6] as bin range is 2 | |
* Example usage histogram([1,5,2,4,2,5,2,3,1], 1) would return back [2,3,1,1,2] where bin ranges are [1,2,3,4,5] as bin range is 1 | |
*/ | |
function histogram(X, binRange) { | |
//inclusive of the first number | |
var max = Math.max(...X); | |
var min = Math.min(...X); | |
var len = max - min + 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* This was inspired by Python https://numpy.org/doc/stable/reference/random/generated/numpy.random.choice.html | |
* Example usage choice([1, 5, 2], 2) <- Gives 1,5 and 2 same chance of occurance i.e. 1/3 | |
* Example usage choice([1, 5, 2], 2, [0.05, 0.8, 0.15]) <-- gives 1 5% chance, 5 80% chance and 2 15% chance to show up | |
*/ | |
function choice(events, size, probability) { | |
if(probability != null) { | |
const pSum = probability.reduce((sum, v) => sum + v); | |
if(pSum < 1 - Number.EPSILON || pSum > 1 + Number.EPSILON) { | |
throw Error("Overall probability has to be 1."); |