Skip to content

Instantly share code, notes, and snippets.

View patricoferris's full-sized avatar
🌳

Patrick Ferris patricoferris

🌳
View GitHub Profile
@patricoferris
patricoferris / simple-graphql-post.ml
Last active April 27, 2021 14:37
Simple Cohttp Post
let uri = "https://countries.trevorblades.com/"
let query = {| query {
countries {
name
}
}|}
let send_graphql_query query =
let open Lwt.Infix in
@patricoferris
patricoferris / index.ml
Last active January 23, 2021 14:10
State management with Javascript objects
open Mithril
open Brr
module State = struct
type t = Jv.t
let state = Jv.obj [| ("id", Jv.of_int 0) |]
let get_id () = Jv.get state "id" |> Jv.to_int
open Lwt.Infix
let add_to_table tbl str =
Base.(Hashtbl.set tbl ~key:str ~data:(Hashtbl.find_or_add tbl str ~default:(fun () -> 0) + 1))
let stream_stdin () =
Lwt_stream.from (fun () -> try Lwt.return (Some (input_line stdin)) with End_of_file -> Lwt.return None)
let main c tbl =
Lwt_stream.iter_n ~max_concurrency:c (fun a -> Lwt.return (add_to_table tbl a)) (stream_stdin ());;
(* A fibonacci function *)
let rec fib x = if x = 0 || x = 1 then 1 else fib (x - 1) + fib (x - 2);;
(* A function that allows for partial evaluation *)
let h x = let z = fib x in fun y -> y + z;;
(* fib 30 will only be calculated once *)
List.map (h 30) [1; 2; 3; 4; 5];;
(* Part of the %inline expr_attrs productions *)
| FUN ext_attributes labeled_simple_pattern fun_def
{ let (l,o,p) = $3 in
Pexp_fun(l, o, p, $4), $2 }
(* the fun_def, lident_list and ext_attributes is then defined as the following *)
fun_def:
MINUSGREATER seq_expr
{ $2 }
| mkexp(COLON atomic_type MINUSGREATER seq_expr
(* lowercase definition *)
let lowercase = ['a'-'z' '_']
(* identchar definition *)
let identchar = ['A'-'Z' 'a'-'z' '_' '\'' '0'-'9']
(* Some stuff before *)
| lowercase identchar * as name
{ try Hashtbl.find keyword_table name
with Not_found -> LIDENT name }
# First we calculate mu_s
alpha = 2.
K_xy = rbf_kernel(xs_train, xs_test, alpha)
K_yy = rbf_kernel(xs_test, xs_test, alpha)
K_xx = rbf_kernel(xs_train, xs_train, alpha)
np.random.seed(20)
mu_s = np.matmul(K_xy.T, np.matmul(np.linalg.pinv(K_xx), (ys_train)))
# Second we calculate sigma_s
sigma_s = K_yy - np.matmul(K_xy.T, np.matmul(np.linalg.pinv(K_xx), K_xy))
from sklearn.gaussian_process.kernels import RBF
# First we set up the training data
xs_train = training_points # The points that will reign in our prediction
ys_train = train_temp # The corresponding temperatures for those points
# Then the space for which we are trying to predict
xs_test = np.atleast_2d(np.linspace(0, 20, 1000)).T
# We will use the SKLearn RBF function
# Get the New York temperatures
n = 20
new_york_temp = city_temps.loc[city_temps['City'] == "New York"][-n:]
temps = np.array(new_york_temp["AverageTemperature"])
temps = temps / np.linalg.norm(temps)
# Extract some random training points from the 20 we are using (set random seed for reproducibility)
training_points = np.atleast_2d([1, 5, 7, 10, 14, 16, 19]).T
np.random.seed(20)
train_temp = temps[training_points]
# Generate the linear evenly spaced xs and ys
n = 50
three_xs = np.linspace(-5, 5, n)
three_ys = np.linspace(-5, 5, n)
# Generate the values for both the X and Y (two parts to the Z)
three_zs_a = np.array(list(map(lambda x: guass_func(x, 0, 1), three_xs)))
three_zs_b = np.array(list(map(lambda y: guass_func(y, 0, 2), three_ys)))
# Use the Kronecker product of the two https://docs.scipy.org/doc/numpy/reference/generated/numpy.kron.html