Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To solve a numerical probability/statistics problem with Sage, the laziest (and usually best) solution is to use the innumerable R functions dedicated to such problems. In the present case :

sage: foo=r.runif(20)._sage_() ; foo # A random vector
[0.0390772928949445,
 0.654059152351692,
 0.339493476552889,
 0.51896350691095,
 0.556925253942609,
 0.978059642249718,
 0.14837371581234,
 0.391689434181899,
 0.229549546260387,
 0.0452805508393794,
 0.742159304674715,
 0.836738985031843,
 0.663826596457511,
 0.0411422045435756,
 0.649067221442237,
 0.67311915429309,
 0.976733086397871,
 0.433464009081945,
 0.182737077819183,
 0.854663047939539]
sage: r.quantile(foo, 1/4)._sage_()['DATA']
0.217846429150086

HTH,

To solve a numerical probability/statistics problem with Sage, the laziest (and usually best) solution is to use the innumerable R functions dedicated to such problems. In the present case :

sage: foo=r.runif(20)._sage_() ; foo # A random vector
[0.0390772928949445,
 0.654059152351692,
 0.339493476552889,
 0.51896350691095,
 0.556925253942609,
 0.978059642249718,
 0.14837371581234,
 0.391689434181899,
 0.229549546260387,
 0.0452805508393794,
 0.742159304674715,
 0.836738985031843,
 0.663826596457511,
 0.0411422045435756,
 0.649067221442237,
 0.67311915429309,
 0.976733086397871,
 0.433464009081945,
 0.182737077819183,
 0.854663047939539]
sage: r.quantile(foo, 1/4)._sage_()['DATA']
0.217846429150086

To get a DICTIONARY of a list of quantiles :

sage: dict(zip((Q:=r.quantile(foo, [1/4, 3/4])._sage_())['_Names'], Q['DATA']))
{'25%': 0.278735583007801, '75%': 0.775309360586107}
sage: dict(zip((Q:=[1/4, 3/4]), r.quantile(foo, Q)._sage_()['DATA']))
{1/4: 0.278735583007801, 3/4: 0.775309360586107}

To get a (numerical) function interpolating the empirical repartition :

sage: bar=spline(zip((Q:=srange(0, 1, 1/10)), r.quantile(foo, Q)._sage_()['DATA']))
sage: bar(1/4) # Not in the generating quantile list
0.3366159958860885

R also has some functions building interpolation (R) functions, but wrapping them to be usable from Sage isn't trivial...

Note : See the documentation of r.quantile for the options available to build the quantiles (nontrivial either...).

HTH,