Standardized Precipitation Index with gamma function

Inspired from Climate Engine platform, I want to calculate Standardize Precipitation Index based on this paper, where each rainfall datasets are fitted to the gamma function to define the relationship of probability to rainfall.

I have try to write a code in GEE to calculate 3-month SPI, so far using simple equation where SPI = (current rainfall - LongTermAverage) / StDev spi = rainfall_now.subtract(ltamean).divide(ltastd). I have difficulties on how to fitted the data to gamma function in GEE, is there any reference or example about this?

Map.setCenter(109.738, -7.347, 7);

// We will compare for a 1-month period of CHIRPS data
var start = 31+28;

// Long term average (LTA) can be generated directly from the pre-calculated Rainfall
.filter(ee.Filter.dayOfYear(start, start + 31))
.filterDate('1981-01-01', '2019-12-31');

var ltamean = lta.select('precipitation').mean();
var ltastd = lta.reduce(ee.Reducer.stdDev());

// same for this year
.filter(ee.Filter.dayOfYear(start, start + 31))
.filterDate('2020-01-01', '2020-05-01');

var rainfall_now = latest.select('precipitation').mean();

// add the various layers and layer combination

// Calculating rainfall anomaly and standardized precipitation index
var anomaly = rainfall_now.divide(ltamean).multiply(100);
var spi = rainfall_now.subtract(ltamean).divide(ltastd);

// Define a colour table
var red2blue = ['a60027','de3e2c','fa8b50','ffd485','e1e1e1','d3edf5','90c4de','5183bd','313694']
var rainfall_viz = {min:0, max:25, palette: ['B8810B', 'CB8C08', 'FFE47E','FFF2C1', 'DAF7A6', 'D2FFFF', '2DE7E7', '00B9D6', '008BD6', '006399']};
var rainanom_viz = {min:0, max:200, palette: red2blue};
var spicolor = ['760005','ec0013','ffa938','fdd28a','fefe53','ffffff','a2fd6e','00b44a','008180','2a23eb','a21fec'];
var spi_viz = {min:-2, max:2, palette: spicolor};

// Add the image to the map using both the color ramp and interval schemes.