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

r""" 

Random variables and probability spaces 

 

This introduces a class of random variables, with the focus on 

discrete random variables (i.e. on a discrete probability space). 

This avoids the problem of defining a measure space and measurable 

functions. 

""" 

 

#***************************************************************************** 

# Copyright (C) 2007 David Kohel <kohel@maths.usyd.edu.au> 

# 

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

# 

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

#***************************************************************************** 

 

from sage.structure.parent_base import ParentWithBase 

from sage.functions.log import log 

from sage.functions.all import sqrt 

from sage.rings.real_mpfr import (RealField, is_RealField) 

from sage.rings.rational_field import is_RationalField 

from sage.sets.set import Set 

 

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

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

 

def is_ProbabilitySpace(S): 

return isinstance(S, ProbabilitySpace_generic) 

 

def is_DiscreteProbabilitySpace(S): 

return isinstance(S, DiscreteProbabilitySpace) 

 

def is_RandomVariable(X): 

return isinstance(X, RandomVariable_generic) 

 

def is_DiscreteRandomVariable(X): 

return isinstance(X, DiscreteRandomVariable) 

 

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

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

 

# We could inherit from a functions class here but use ParentWithBase 

 

class RandomVariable_generic(ParentWithBase): 

""" 

A random variable. 

""" 

def __init__(self, X, RR): 

if not is_ProbabilitySpace(X): 

raise TypeError("Argument X (= %s) must be a probability space" % X) 

ParentWithBase.__init__(self, X) 

self._codomain = RR 

 

def probability_space(self): 

return self.base() 

 

def domain(self): 

return self.base() 

 

def codomain(self): 

return self._codomain 

 

def field(self): 

return self._codomain 

 

class DiscreteRandomVariable(RandomVariable_generic): 

""" 

A random variable on a discrete probability space. 

""" 

def __init__(self, X, f, codomain = None, check = False): 

r""" 

Create free binary string monoid on `n` generators. 

 

INPUT: x: A probability space f: A dictionary such that X[x] = 

value for x in X is the discrete function on X 

""" 

if not is_DiscreteProbabilitySpace(X): 

raise TypeError("Argument X (= %s) must be a discrete probability space" % X) 

if check: 

raise NotImplementedError("Not implemented") 

if codomain is None: 

RR = RealField() 

else: 

RR = codomain 

RandomVariable_generic.__init__(self, X, RR) 

self._function = f 

 

def __call__(self,x): 

""" 

Return the value of the random variable at x. 

""" 

RR = self.field() 

try: 

return RR(self._function[x]) 

except KeyError: 

# Need some condition for x being a valid domain element: 

# raise IndexError, "Argument x (= %s) is not a valid domain element." % x 

return RR(0) 

 

def __repr__(self): 

return "Discrete random variable defined by %s" % self._function 

 

def function(self): 

""" 

The function defining the random variable. 

""" 

return self._function 

 

def expectation(self): 

r""" 

The expectation of the discrete random variable, namely 

`\sum_{x \in S} p(x) X[x]`, where `X` = self and 

`S` is the probability space of `X`. 

""" 

E = 0 

Omega = self.probability_space() 

for x in self._function.keys(): 

E += Omega(x) * self(x) 

return E 

 

def translation_expectation(self, map): 

r""" 

The expectation of the discrete random variable, namely 

`\sum_{x \in S} p(x) X[e(x)]`, where `X` = self, 

`S` is the probability space of `X`, and 

`e` = map. 

""" 

E = 0 

Omega = self.probability_space() 

for x in Omega._function.keys(): 

E += Omega(x) * self(map(x)) 

return E 

 

def variance(self): 

r""" 

The variance of the discrete random variable. 

 

Let `S` be the probability space of `X` = self, 

with probability function `p`, and `E(X)` be the 

expectation of `X`. Then the variance of `X` is: 

 

.. MATH:: 

 

\mathrm{var}(X) = E((X-E(x))^2) = \sum_{x \in S} p(x) (X(x) - E(x))^2 

""" 

Omega = self.probability_space() 

mu = self.expectation() 

var = 0 

for x in self._function.keys(): 

var += Omega(x) * (self(x) - mu)**2 

return var 

 

def translation_variance(self, map): 

r""" 

The variance of the discrete random variable `X \circ e`, 

where `X` = self, and `e` = map. 

 

Let `S` be the probability space of `X` = self, 

with probability function `p`, and `E(X)` be the 

expectation of `X`. Then the variance of `X` is: 

 

.. MATH:: 

 

\mathrm{var}(X) = E((X-E(x))^2) = \sum_{x \in S} p(x) (X(x) - E(x))^2 

""" 

Omega = self.probability_space() 

mu = self.translation_expectation(map) 

var = 0 

for x in Omega._function.keys(): 

var += Omega(x) * (self(map(x)) - mu)**2 

return var 

 

def covariance(self, other): 

r""" 

The covariance of the discrete random variable X = self with Y = 

other. 

 

Let `S` be the probability space of `X` = self, 

with probability function `p`, and `E(X)` be the 

expectation of `X`. Then the variance of `X` is: 

 

.. MATH:: 

 

\text{cov}(X,Y) = E((X-E(X)\cdot (Y-E(Y)) = \sum_{x \in S} p(x) (X(x) - E(X))(Y(x) - E(Y)) 

""" 

Omega = self.probability_space() 

if Omega != other.probability_space(): 

raise ValueError("Argument other (= %s) must be defined on the same probability space." % other) 

muX = self.expectation() 

muY = other.expectation() 

cov = 0 

for x in self._function.keys(): 

cov += Omega(x)*(self(x) - muX)*(other(x) - muY) 

return cov 

 

def translation_covariance(self, other, map): 

r""" 

The covariance of the probability space X = self with image of Y = 

other under the given map of the probability space. 

 

Let `S` be the probability space of `X` = self, 

with probability function `p`, and `E(X)` be the 

expectation of `X`. Then the variance of `X` is: 

 

.. MATH:: 

 

\text{cov}(X,Y) = E((X-E(X)\cdot (Y-E(Y)) = \sum_{x \in S} p(x) (X(x) - E(X))(Y(x) - E(Y)) 

""" 

Omega = self.probability_space() 

if Omega != other.probability_space(): 

raise ValueError("Argument other (= %s) must be defined on the same probability space." % other) 

muX = self.expectation() 

muY = other.translation_expectation(map) 

cov = 0 

for x in Omega._function.keys(): 

cov += Omega(x)*(self(x) - muX)*(other(map(x)) - muY) 

return cov 

 

def standard_deviation(self): 

r""" 

The standard deviation of the discrete random variable. 

 

Let `S` be the probability space of `X` = self, 

with probability function `p`, and `E(X)` be the 

expectation of `X`. Then the standard deviation of 

`X` is defined to be 

 

.. MATH:: 

 

\sigma(X) = \sqrt{ \sum_{x \in S} p(x) (X(x) - E(x))^2} 

""" 

return sqrt(self.variance()) 

 

def translation_standard_deviation(self, map): 

r""" 

The standard deviation of the translated discrete random variable 

`X \circ e`, where `X` = self and `e` = 

map. 

 

Let `S` be the probability space of `X` = self, 

with probability function `p`, and `E(X)` be the 

expectation of `X`. Then the standard deviation of 

`X` is defined to be 

 

.. MATH:: 

 

\sigma(X) = \sqrt{ \sum_{x \in S} p(x) (X(x) - E(x))^2} 

""" 

return sqrt(self.translation_variance(map)) 

 

def correlation(self, other): 

""" 

The correlation of the probability space X = self with Y = other. 

""" 

cov = self.covariance(other) 

sigX = self.standard_deviation() 

sigY = other.standard_deviation() 

if sigX == 0 or sigY == 0: 

raise ValueError("Correlation not defined if standard deviations are not both nonzero.") 

return cov/(sigX*sigY) 

 

def translation_correlation(self, other, map): 

""" 

The correlation of the probability space X = self with image of Y = 

other under map. 

""" 

cov = self.translation_covariance(other, map) 

sigX = self.standard_deviation() 

sigY = other.translation_standard_deviation(map) 

if sigX == 0 or sigY == 0: 

raise ValueError("Correlation not defined if standard deviations are not both nonzero.") 

return cov/(sigX*sigY) 

 

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

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

 

class ProbabilitySpace_generic(RandomVariable_generic): 

r""" 

A probability space. 

""" 

def __init__(self, domain, RR): 

""" 

A generic probability space on given domain space and codomain 

ring. 

""" 

if isinstance(domain, list): 

domain = tuple(domain) 

if not isinstance(domain, tuple): 

raise TypeError("Argument domain (= %s) must be a list, tuple, or set containing." % domain) 

self._domain = domain 

RandomVariable_generic.__init__(self, self, RR) 

 

def domain(self): 

return self._domain 

 

class DiscreteProbabilitySpace(ProbabilitySpace_generic,DiscreteRandomVariable): 

r""" 

The discrete probability space 

""" 

def __init__(self, X, P, codomain = None, check = False): 

r""" 

Create the discrete probability space with probabilities on the 

space X given by the dictionary P with values in the field 

real_field. 

 

EXAMPLES:: 

 

sage: S = [ i for i in range(16) ] 

sage: P = {} 

sage: for i in range(15): P[i] = 2^(-i-1) 

sage: P[15] = 2^-16 

sage: X = DiscreteProbabilitySpace(S,P) 

sage: X.domain() 

(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15) 

sage: X.set() 

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 

sage: X.entropy().n() 

1.99972534179688 

 

A probability space can be defined on any list of elements:: 

 

sage: AZ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 

sage: S = [ AZ[i] for i in range(26) ] 

sage: P = { 'A':1/2, 'B':1/4, 'C':1/4 } 

sage: X = DiscreteProbabilitySpace(S,P) 

sage: X 

Discrete probability space defined by {'A': 1/2, 'C': 1/4, 'B': 1/4} 

sage: X.entropy().n() 

1.50000000000000 

""" 

if codomain is None: 

codomain = RealField() 

if not is_RealField(codomain) and not is_RationalField(codomain): 

raise TypeError("Argument codomain (= %s) must be the reals or rationals" % codomain) 

if check: 

one = sum([ P[x] for x in P.keys() ]) 

if is_RationalField(codomain): 

if not one == 1: 

raise TypeError("Argument P (= %s) does not define a probability function") 

else: 

if not Abs(one-1) < 2^(-codomain.precision()+1): 

raise TypeError("Argument P (= %s) does not define a probability function") 

ProbabilitySpace_generic.__init__(self, X, codomain) 

DiscreteRandomVariable.__init__(self, self, P, codomain, check) 

 

def __repr__(self): 

return "Discrete probability space defined by %s" % self.function() 

 

def set(self): 

r""" 

The set of values of the probability space taking possibly nonzero 

probability (a subset of the domain). 

""" 

return Set(self.function().keys()) 

 

def entropy(self): 

""" 

The entropy of the probability space. 

""" 

def neg_xlog2x(p): 

if p == 0: 

return 0 

else: 

return -p*log(p,2) 

p = self.function() 

return sum([ neg_xlog2x(p[x]) for x in p.keys() ])