Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

""" 

Basic Statistics 

 

This file contains basic descriptive functions. Included are the mean, 

median, mode, moving average, standard deviation, and the variance. 

When calling a function on data, there are checks for functions already 

defined for that data type. 

 

The ``mean`` function returns the arithmetic mean (the sum of all the members 

of a list, divided by the number of members). Further revisions may include 

the geometric and harmonic mean. The ``median`` function returns the number 

separating the higher half of a sample from the lower half. The ``mode`` 

returns the most common occuring member of a sample, plus the number of times 

it occurs. If entries occur equally common, the smallest of a list of the most 

common entries is returned. The ``moving_average`` is a finite impulse 

response filter, creating a series of averages using a user-defined number of 

subsets of the full data set. The ``std`` and the ``variance`` return a 

measurement of how far data points tend to be from the arithmetic mean. 

 

Functions are available in the namespace ``stats``, i.e. you can use them by 

typing ``stats.mean``, ``stats.median``, etc. 

 

REMARK: If all the data you are working with are floating point 

numbers, you may find ``finance.TimeSeries`` helpful, since it is 

extremely fast and offers many of the same descriptive statistics as 

in the module. 

 

AUTHOR: 

 

- Andrew Hou (11/06/2009) 

 

""" 

###################################################################### 

# Copyright (C) 2009, Andrew Hou <amhou@uw.edu> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# The full text of the GPL is available at: 

# http://www.gnu.org/licenses/ 

###################################################################### 

from six import integer_types 

 

from sage.rings.integer_ring import ZZ 

from sage.symbolic.constants import NaN 

from sage.functions.other import sqrt 

 

def mean(v): 

""" 

Return the mean of the elements of `v`. 

 

We define the mean of the empty list to be the (symbolic) NaN, 

following the convention of MATLAB, Scipy, and R. 

 

INPUT: 

 

- `v` -- a list of numbers 

 

OUTPUT: 

 

- a number 

 

EXAMPLES:: 

 

sage: mean([pi, e]) 

1/2*pi + 1/2*e 

sage: mean([]) 

NaN 

sage: mean([I, sqrt(2), 3/5]) 

1/3*sqrt(2) + 1/3*I + 1/5 

sage: mean([RIF(1.0103,1.0103), RIF(2)]) 

1.5051500000000000? 

sage: mean(range(4)) 

3/2 

sage: v = finance.TimeSeries([1..100]) 

sage: mean(v) 

50.5 

""" 

if hasattr(v, 'mean'): return v.mean() 

if len(v) == 0: 

return NaN 

s = sum(v) 

if isinstance(s, integer_types): 

# python integers are stupid. 

return s/ZZ(len(v)) 

return s/len(v) 

 

 

def mode(v): 

""" 

Return the mode of `v`. 

 

The mode is the list of the most frequently occuring 

elements in `v`. If `n` is the most times that any element occurs 

in `v`, then the mode is the list of elements of `v` that 

occur `n` times. The list is sorted if possible. 

 

.. NOTE:: 

 

The elements of `v` must be hashable. 

 

INPUT: 

 

- `v` -- a list 

 

OUTPUT: 

 

- a list (sorted if possible) 

 

EXAMPLES:: 

 

sage: v = [1,2,4,1,6,2,6,7,1] 

sage: mode(v) 

[1] 

sage: v.count(1) 

3 

sage: mode([]) 

[] 

 

sage: mode([1,2,3,4,5]) 

[1, 2, 3, 4, 5] 

sage: mode([3,1,2,1,2,3]) 

[1, 2, 3] 

sage: mode([0, 2, 7, 7, 13, 20, 2, 13]) 

[2, 7, 13] 

 

sage: mode(['sage', 'four', 'I', 'three', 'sage', 'pi']) 

['sage'] 

 

sage: class MyClass: 

....: def mode(self): 

....: return [1] 

sage: stats.mode(MyClass()) 

[1] 

""" 

if hasattr(v, 'mode'): 

return v.mode() 

 

if not v: 

return v 

 

freq = {} 

for i in v: 

if i in freq: 

freq[i] += 1 

else: 

freq[i] = 1 

 

n = max(freq.values()) 

try: 

return sorted(u for u, f in freq.items() if f == n) 

except TypeError: 

return [u for u, f in freq.items() if f == n] 

 

 

def std(v, bias=False): 

""" 

Returns the standard deviation of the elements of `v` 

 

We define the standard deviation of the empty list to be NaN, 

following the convention of MATLAB, Scipy, and R. 

 

INPUT: 

 

- `v` -- a list of numbers 

 

- ``bias`` -- bool (default: False); if False, divide by 

len(v) - 1 instead of len(v) 

to give a less biased estimator (sample) for the 

standard deviation. 

 

OUTPUT: 

 

- a number 

 

EXAMPLES:: 

 

sage: std([1..6], bias=True) 

1/2*sqrt(35/3) 

sage: std([1..6], bias=False) 

sqrt(7/2) 

sage: std([e, pi]) 

sqrt(1/2)*abs(pi - e) 

sage: std([]) 

NaN 

sage: std([I, sqrt(2), 3/5]) 

sqrt(1/450*(10*sqrt(2) - 5*I - 3)^2 + 1/450*(5*sqrt(2) - 10*I + 3)^2 + 1/450*(5*sqrt(2) + 5*I - 6)^2) 

sage: std([RIF(1.0103, 1.0103), RIF(2)]) 

0.6998235813403261? 

sage: import numpy 

sage: x = numpy.array([1,2,3,4,5]) 

sage: std(x, bias=False) 

1.5811388300841898 

sage: x = finance.TimeSeries([1..100]) 

sage: std(x) 

29.011491975882016 

""" 

 

# NOTE: in R bias = False by default, and in Scipy bias=True by 

# default, and R is more popular. 

 

if hasattr(v, 'standard_deviation'): return v.standard_deviation(bias=bias) 

 

import numpy 

 

x = 0 

if isinstance(v, numpy.ndarray): 

# accounts for numpy arrays 

if bias: 

return v.std() 

else: 

return v.std(ddof=1) 

 

if len(v) == 0: 

# standard deviation of empty set defined as NaN 

return NaN 

 

return sqrt(variance(v, bias=bias)) 

 

def variance(v, bias=False): 

""" 

Returns the variance of the elements of `v` 

 

We define the variance of the empty list to be NaN, 

following the convention of MATLAB, Scipy, and R. 

 

INPUT: 

 

- `v` -- a list of numbers 

 

- ``bias`` -- bool (default: False); if False, divide by 

len(v) - 1 instead of len(v) 

to give a less biased estimator (sample) for the 

standard deviation. 

 

OUTPUT: 

 

- a number 

 

 

EXAMPLES:: 

 

sage: variance([1..6]) 

7/2 

sage: variance([1..6], bias=True) 

35/12 

sage: variance([e, pi]) 

1/2*(pi - e)^2 

sage: variance([]) 

NaN 

sage: variance([I, sqrt(2), 3/5]) 

1/450*(10*sqrt(2) - 5*I - 3)^2 + 1/450*(5*sqrt(2) - 10*I + 3)^2 + 1/450*(5*sqrt(2) + 5*I - 6)^2 

sage: variance([RIF(1.0103, 1.0103), RIF(2)]) 

0.4897530450000000? 

sage: import numpy 

sage: x = numpy.array([1,2,3,4,5]) 

sage: variance(x, bias=False) 

2.5 

sage: x = finance.TimeSeries([1..100]) 

sage: variance(x) 

841.6666666666666 

sage: variance(x, bias=True) 

833.25 

sage: class MyClass: 

....: def variance(self, bias = False): 

....: return 1 

sage: stats.variance(MyClass()) 

1 

sage: class SillyPythonList: 

....: def __init__(self): 

....: self.__list = [2L,4L] 

....: def __len__(self): 

....: return len(self.__list) 

....: def __iter__(self): 

....: return self.__list.__iter__() 

....: def mean(self): 

....: return 3L 

sage: R = SillyPythonList() 

sage: variance(R) 

2 

sage: variance(R, bias=True) 

1 

 

 

TESTS: 

 

The performance issue from :trac:`10019` is solved:: 

 

sage: variance([1] * 2^18) 

0 

""" 

if hasattr(v, 'variance'): 

return v.variance(bias=bias) 

import numpy 

 

x = 0 

if isinstance(v, numpy.ndarray): 

# accounts for numpy arrays 

if bias is True: 

return v.var() 

elif bias is False: 

return v.var(ddof=1) 

if len(v) == 0: 

# variance of empty set defined as NaN 

return NaN 

 

mu = mean(v) 

for vi in v: 

x += (vi - mu)**2 

if bias: 

# population variance 

if isinstance(x, integer_types): 

return x/ZZ(len(v)) 

return x/len(v) 

else: 

# sample variance 

if isinstance(x, integer_types): 

return x/ZZ(len(v)-1) 

return x/(len(v)-1) 

 

 

def median(v): 

""" 

Return the median (middle value) of the elements of `v` 

 

If `v` is empty, we define the median to be NaN, which is 

consistent with NumPy (note that R returns NULL). 

If `v` is comprised of strings, TypeError occurs. 

For elements other than numbers, the median is a result of ``sorted()``. 

 

INPUT: 

 

- `v` -- a list 

 

OUTPUT: 

 

- median element of `v` 

 

EXAMPLES:: 

 

sage: median([1,2,3,4,5]) 

3 

sage: median([e, pi]) 

1/2*pi + 1/2*e 

sage: median(['sage', 'linux', 'python']) 

'python' 

sage: median([]) 

NaN 

sage: class MyClass: 

....: def median(self): 

....: return 1 

sage: stats.median(MyClass()) 

1 

""" 

if hasattr(v, 'median'): return v.median() 

 

if len(v) == 0: 

# Median of empty set defined as NaN 

return NaN 

values = sorted(v) 

if len(values) % 2 == 1: 

return values[((len(values))+1)//2-1] 

else: 

lower = values[(len(values)+1)//2-1] 

upper = values[len(values)//2] 

return (lower + upper)/ZZ(2) 

 

def moving_average(v, n): 

""" 

Provides the moving average of a list `v` 

 

The moving average of a list is often used to smooth out noisy data. 

 

If `v` is empty, we define the entries of the moving average to be NaN. 

 

INPUT: 

 

- `v` -- a list 

 

- `n` -- the number of values used in computing each average. 

 

OUTPUT: 

 

- a list of length ``len(v)-n+1``, since we do not fabric any values 

 

EXAMPLES:: 

 

sage: moving_average([1..10], 1) 

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

sage: moving_average([1..10], 4) 

[5/2, 7/2, 9/2, 11/2, 13/2, 15/2, 17/2] 

sage: moving_average([], 1) 

[] 

sage: moving_average([pi, e, I, sqrt(2), 3/5], 2) 

[1/2*pi + 1/2*e, 1/2*e + 1/2*I, 1/2*sqrt(2) + 1/2*I, 1/2*sqrt(2) + 3/10] 

 

We check if the input is a time series, and if so use the 

optimized ``simple_moving_average`` method, but with (slightly 

different) meaning as defined above (the point is that the 

``simple_moving_average`` on time series returns `n` values:: 

 

sage: a = finance.TimeSeries([1..10]) 

sage: stats.moving_average(a, 3) 

[2.0000, 3.0000, 4.0000, 5.0000, 6.0000, 7.0000, 8.0000, 9.0000] 

sage: stats.moving_average(list(a), 3) 

[2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] 

 

""" 

if len(v) == 0: 

return v 

from sage.finance.time_series import TimeSeries 

if isinstance(v, TimeSeries): 

return v.simple_moving_average(n)[n-1:] 

n = int(n) 

if n <= 0: 

raise ValueError("n must be positive") 

nn = ZZ(n) 

s = sum(v[:n]) 

ans = [s/nn] 

for i in range(n,len(v)): 

# add in the i-th value in v to our running sum, 

# and remove the value n places back. 

s += v[i] - v[i-n] 

ans.append(s/nn) 

return ans