Package: randist
Distance between Random EventsThis package is mainly for symbolically and numerically calculating the arbitrary order moments, pdf, cdf and their conditional counterparts of the distance between two random events in a given graph. The position of an event in a network is encoded in a tuple(e, p), wheree={u,v}(assumeu < v) is the edge where the event happens andpis the relative location of the event on that edge, that is the length of the segment fromu(the vertex with small index) to the location of the event divided by the length of the edgee. Since both events are random, we use(X, P)and(Y, Q)to denote both events respectively.These formulas can be easily embedded in optimization models. If consider the pmf ofXandYas decision variables, it can be shown that all these formulas are linear functions in these variables. User can use the formula methodsX_coeffandY_coeffto retrieve the corresponding coefficients.InstallationUse pip to install the randist package.pip install randistInputsA data file whose rows are edges of the network with extra properties. There are five columns,i: first vertex of the edgej: second vertex of the edgel: length of the current edgex: the probability of event 1 happens on the current edgey: the probability of event 2 happens on the current edgeThe joint distribution of the relative locations of two events,. This should be provided as aPhiobject,from sympy.abc import p, q  # import symbols
import randist as rt        # import our randist package

phi_pq = 1  # the uniform distribution
phi_pq = 36 * p * (1 - p) * q * (1 - q)  # both are beta functions with parameters alpha = beta = 2

phi = rt.Phi('betapq', phi_pq=phi_pq)  # create a Phi object with a nameIn current implementation, the random variablesXandYare assumed to be independent, but the formulas we developed in our paper do not have this restriction. Also, currently, we assume the joint pdfare same for all pair of edges(e, f), but the formulas in our paper do not have this restriction. We may relax both restrictions in the future version.Also notice the input joint distributionphi_pqdoes not have to be correct outside of the domain[0, 1]^2the unit square in thepq-plane (all values should be zero outside this domain). Our package will verify whether the input expression integral to1in its domain, but will not verify the non-negative requirement.OutputsSeveral statistics about the random distanceDbetween two events:moments of arbitrary ordercdf (point evaluation, or plotting against the distancex)pdf (point evaluation, or plotting against the distancex)conditional moments of arbitrary order  (point evaluation, or plotting against the relative locationpgivenX=e)conditional cdf  (point evaluation or plotting againstxgiven(X, P) = (e, p))conditional pdf  (point evaluation or plotting againstxgiven(X, P) = (e, p))All these statistics can be computed either symbolically or numerically. We will explain their differences later.Main InterfacesUser can achieve most tasks with two interfaces,Formulasanddata_collector. The former gives the freedom of calculating statistics individually, where the latter can collect data in batch.Interface 1:FormulasObjectExampleAn example of using formulas objects to compute statistics:from sympy.abc import p, q  # import symbols
import randist as rt        # import our package

gname = 'g0'  # data file name in the folder ./data
phi_pq = 36 * p * (1-p) * q * (1 - q)
phi = rt.Phi('betapq', phi_pq=phi_pq)  # create a joint pdf with a name

fls = rt.Formulas(gname, phi)                 # create a formulas object

moment = fls.get_formula(rt.Stats.MOMENT)     # get a moment formula object
cdf = fls.get_formula(rt.Stats.CDF)           # get a cdf formula object
pdf = fls.get_formula(rt.Stats.PDF)           # get a pdf formula object
cmoment = fls.get_formula(rt.Stats.CMOMENT)    # get a conditional moment formula object
ccdf = fls.get_formula(rt.Stats.CCDF)         # get a conditional cdf formula object
cpdf = fls.get_formula(rt.Stats.CPDF)         # get a conditional pdf formula object

moment.eval(3)                        # computing the 3rd order moment
moment.eval(2) - moment.eval(1) ** 2  # compute the variance
cdf.eval(9.5)                         # evaluate the cdf at the point x = 9.5
cdf.plot(show=True)                   # save the plot in the ./results folder and show it
pdf.eval(8.1)                         # evaluate the pdf at the point x = 9.5
pdf.plot()                            # save the plot in the ./results folder without showing
cmoment.eval(1, ('1', '2'), 0.5)      # the conditional expectation given (e, p) = (('1', '2'), 0.5)
cmoment.plot(2, ('1', '2'))           # plot the conditional 2nd moment against the value of p
ccdf.eval(('2', '3'), 0.1, 3.5)       # evaluate the conditional cdf at x = 3.5 given (e, p) = (('2', '3'), 0.1)
ccdf.plot(('2', '3'), 0.1)            # plot the conditional cdf given (e, p) = (('2', '3'), 0.1)
cpdf.eval(('2', '3'), 0.1, 3.5)       # same but with conditional pdf
cpdf.plot(('2', '3'), 0.1)            # same but with conditional pdfTheFormulasClassTheFormulasclass has the following parameters,Formulas(gname, phi, fpath='./data/', rational=False, d_jit=False, memorize=True)each parameter is explained below:gname:    data file name without the extension.datphi:      a Phi object for input joint distributionfpath:    the folder where you put the input data filerational: ifTrue, all value are computed in the rational form (slow)d_jit: compute the shortest path length between pair of vertices in aJust In Timefashion. Set this toTrueif the input graph is very large and only conditional statistics are needed.memorize: use memorization to speedup the computation. Set this toFalseonly if the input graph is too large so that the memories in the computer are not enough.Theget_formulaMethodget_formula(stats, symbolic=None)each parameter is explained below:stats: specify which type of formulas you want, all types are in the enum type Stats.symbolic: calculate values numerically or symbolically. The default valueNonemeans auto, so moments and conditional moments will be calculated numerically, and all the rest are calculated symbolically.This method will return a formula object.Comparison between Numeric and Symbolic FormulasSymbolic formula object is slow in generating the formula, but fast in evaluating values once the formula has been generated.Symbolic formula object has two more methods that numerical formulas do not have,formula()which shows the closed-form formula for the corresponding statistics, andsave_formula()that saves the generated formula into file, so that users can load it by the functionload_formulasin the future without generating the formulas from scratch again.Symbolic formula is faster in plotting.One drawback is that the speed of symbolic formulas are getting much more slower when the size of the graph increases.Numeric formulas are fast in evaluating a single value. And it performs much faster than symbolic formulas in both plotting and evaluation when graph is large.Basically, if the network is large, always use numeric formulas. Otherwise, please use the default setting, especially if you want to reuse the formulas in the future.TheFormulaObjectThe main methods of formula objects are:eval(*params, save=True): give corresponding parameters to evaluate the value. The required parameters areMoments:k, the order of moment.CDF:x, the distance.PDF:x, the distance.Conditional Moments:k;e, the edge conditioning on;p, the relative location conditioning on.Conditional CDF:e,p,x.Conditional PDF:e,p,x.plot(*params, step=0.01, save=True, show=False): plotting the formula. The required parameters areMoments: cannot plot.CDF: no required input.PDF: no required input.Conditional Moments:k,e. Plotting overp.Conditional CDF:e,p.Conditional PDF:e,p.X_coeff(k_val=None, p_val=None, x_val=None): consider the formula as a function of the pmf ofX, retrieve the coeffecients. Return a dictionary indexed by the edges.Y_coeff(k_val=None, p_val=None, x_val=None): consider the formula as a function of the pmf ofY, retrieve the coeffecients. Return a dictionary indexed by the edges.Unique methods for symbolic formula objects:formula(self, *params): return the closed form formula. Same required parameters as the plot method.save_formula(): save the formulas in a hidden folder under current working directory. Can use the functionload_formulas()to reload these formulas next time without reading the original graph.Interface 2:data_collectorFunctionBasically, the functiondata_collectoris a wrapper of theFormulasclass. We will demonstrate the usage with an example.Examplefrom sympy.abc import p, q  # import symbols
import randist as rt        # import our randist package

gname = 'g0'                # input graph name
phi = rt.Phi('uniform', 1)  # creating a input joint distribution

ks = [1, 2, 3]              # list of orders for moments
loc1 = (('1', '2'), 0.2)
loc2 = (('1', '3'), 0.5)
loc3 = (('3', '4'), 0)
locs = [loc1, loc2, loc3]   # list of locations

mmtp = {'collect': True, 'symbolic': None, 'valst': ks}          # params for moment
cdfp = {'collect': True, 'symbolic': None}                       # params for cdf

pdfp = {'collect': True, 'symbolic': None}                       # params for pdf
cmmtp ={'collect': True, 'symbolic': None, 'valst': (ks, locs)}  # params for conditional moment
ccdfp = {'collect': True, 'symbolic': None, 'valst': locs}       # params for conditional cdf
cpdfp = {'collect': True, 'symbolic': False, 'valst': locs}      # params for conditional pdf

d_jit = False     # whether compute pairwise shortest distance in a Just In Time fashion
memorize = True   # whether use memorization to speedup the computation

# collect all specified data and save them in the folder ./results
rt.data_collector(gname, phi, mmtp, cdfp, pdfp, cmmtp, ccdfp, cpdfp, d_jit=d_jit, memorize=memorize)Future PlanRemove the current restrictions mentioned before aboutXandY, and.Interactive user interface.The running speed right now is decent for common graph. Further speed improvement can be done by rewriting core functions in C.