Debian Codegraph @

wxgeometrie/modules/statistiques/experience.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##------------------------------------------#######
4 # Experience #
5 ##------------------------------------------#######
6 # WxGeometrie
7 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
8 # Copyright (C) 2005-2013 Nicolas Pourcelot
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24 from PyQt5.QtWidgets import (QSpinBox, QCheckBox, QPushButton,
25 QVBoxLayout, QLabel, QHBoxLayout, QLineEdit)
26 from numpy.random import rand
27 import math
28 from numpy import sum
29 # NB: numpy.sum est 100 fois plus rapide que __builtin__.sum !
30
31 from ...GUI.qtlib import MyMiniFrame
32 from ...pylib import msplit
33
34 ent = int
35
36 def alea(n = None):
37 """Nombre entier aléatoire compris entre 0 et n-1.
38 Si n = None, nombre décimal entre 0 et 1."""
39 if n == None:
40 return rand()
41 return int(n*rand())
42
43 def de(k = 1):
44 return sum(1+(6*rand(k)).astype(int))
45
46
47 def sondage(pourcentage = 50, k = 1000):
48 pourcentage /= 100.
49 return 100.*sum(rand(k)<pourcentage)/k
50
51 DIC = {'rand': rand, 'random': rand, 'ent': ent, 'alea': alea}
52 DIC.update(math.__dict__)
53
54
55
56
57 class ExperienceFrame(MyMiniFrame):
58 def __init__(self, parent):
59
60 MyMiniFrame.__init__(self, parent, "Simulation d'une expérience")
61 self.parent = parent
62
63 sizer = QVBoxLayout()
64 exp = QHBoxLayout()
65 exp.addWidget(QLabel("Experience:"))
66 self.experience = QLineEdit()
67 self.experience.setMinimumWidth(120)
68 self.experience.returnPressed.connect(self.actualiser)
69 exp.addWidget(self.experience)
70 sizer.addLayout(exp)
71
72 nbr = QHBoxLayout()
73 nbr.addWidget(QLabel("Nombre d'expériences:"))
74 sc = self.sc = QSpinBox()
75 sc.setRange(1, 100000)
76 sc.setValue(5)
77 sc.valueChanged.connect(self.actualiser)
78 nbr.addWidget(sc)
79 sizer.addLayout(nbr)
80
81 val = QHBoxLayout()
82 val.addWidget(QLabel("Valeurs possibles:"))
83 self.valeurs = QLineEdit()
84 self.valeurs.setMinimumWidth(120)
85 self.valeurs.returnPressed.connect(self.actualiser)
86 val.addWidget(self.valeurs)
87 sizer.addLayout(val)
88
89 self.cb = QCheckBox("Lancer une animation:")
90 sizer.addWidget(self.cb)
91 self.cb.stateChanged.connect(self.actualiser)
92
93 boutons = QHBoxLayout()
94 fermer = QPushButton("Fermer")
95 boutons.addWidget(fermer)
96 lancer = QPushButton("Lancer l'experience")
97 boutons.addWidget(lancer)
98 fermer.clicked.connect(self.close)
99 lancer.clicked.connect(self.actualiser)
100
101 sizer.addLayout(boutons)
102 self.setLayout(sizer)
103
104
105 def actualiser(self, event = None):
106 n = self.sc.value()
107 exp = self.experience.text()
108 vals = msplit(self.valeurs.text(), (" ", ",", ";"))
109 print(alea)
110 if exp:
111 self.parent.experience(exp, n, [eval(val, DIC) for val in vals if val])
112
113
114
115
116
117 class LancerDes(MyMiniFrame):
118 def __init__(self, parent):
119
120 MyMiniFrame.__init__(self, parent, "Simulation de lancers de dés")
121 self.parent = parent
122
123 sizer = QVBoxLayout()
124 sizer.addWidget(QLabel("On simule le lancer d'un ou plusieurs dés"))
125 sizer.addWidget(QLabel("à 6 faces, et on étudie la somme des points."))
126 exp = QHBoxLayout()
127 exp.addWidget(QLabel("Nombre de dés:"))
128 ex = self.experience = QSpinBox()
129 ex.setRange(1, 100000)
130 ex.setValue(1)
131 ex.valueChanged.connect(self.actualiser)
132 exp.addWidget(self.experience)
133 sizer.addLayout(exp)
134
135 nbr = QHBoxLayout()
136 nbr.addWidget(QLabel("Nombre de lancers:"))
137 sc = self.sc = QSpinBox()
138 sc.setRange(1, 100000)
139 sc.setValue(1)
140 self.sc.valueChanged.connect(self.actualiser)
141 nbr.addWidget(sc)
142 sizer.addLayout(nbr)
143
144 self.cb = QCheckBox("Conserver les valeurs")
145 sizer.addWidget(self.cb)
146
147
148 boutons = QHBoxLayout()
149 fermer = QPushButton("Fermer")
150 boutons.addWidget(fermer)
151 lancer = QPushButton("Lancer l'expérience")
152 boutons.addWidget(lancer)
153 fermer.clicked.connect(self.close)
154 lancer.clicked.connect(self.actualiser)
155
156 sizer.addLayout(boutons)
157 self.setLayout(sizer)
158
159
160 def actualiser(self, event = None):
161 if not self.cb.isChecked():
162 self.parent.actualiser(False)
163 self.parent.graph = 'batons'
164 n = self.sc.value()
165 des = self.experience.value()
166 for val in range(des, 6*des + 1):
167 self.parent.ajouter_valeur(val, 0)
168 self.parent.ajouter_valeurs(*[de(des) for i in range(n)])
169 self.parent.calculer()
170 self.parent.legende_x = "points obtenus"
171 self.parent.legende_y = "nombre de lancers"
172 self.parent.affiche()
173
174
175
176
177
178 class Sondage(MyMiniFrame):
179 def __init__(self, parent):
180
181 MyMiniFrame.__init__(self, parent, "Simulation d'un sondage")
182 self.parent = parent
183
184 sizer = QVBoxLayout()
185 sizer.addWidget(QLabel("On simule un sondage simple (réponse par oui ou non)."))
186 sizer.addWidget(QLabel("Exemple: \"préférez-vous le candidat A au candidat B ?\""))
187 exp = QHBoxLayout()
188 exp.addWidget(QLabel("Pourcentage de réponses affirmatives sur l'ensemble de la population:"))
189 ex = self.experience = QSpinBox()
190 ex.setRange(0, 100)
191 ex.setValue(50)
192 ex.valueChanged.connect(self.actualiser)
193 exp.addWidget(self.experience)
194 sizer.addLayout(exp)
195
196 nbr = QHBoxLayout()
197 nbr.addWidget(QLabel("Taille de l'echantillon:"))
198 sc = self.sc1 = QSpinBox()
199 sc.setRange(1, 100000)
200 sc.setValue(1000)
201 sc.valueChanged.connect(self.actualiser)
202 nbr.addWidget(sc)
203 sizer.addLayout(nbr)
204
205 self.cb = QCheckBox("Afficher l'intervalle de fluctuation")
206 sizer.addWidget(self.cb)
207 self.cb.stateChanged.connect(self.EvtCheckBox)
208
209
210 nbr = QHBoxLayout()
211 nbr.addWidget(QLabel("Nombre de sondages:"))
212 sc = self.sc2 = QSpinBox()
213 sc.setRange(1, 100000)
214 sc.setValue(1)
215 sc.valueChanged.connect(self.actualiser)
216 nbr.addWidget(sc)
217 sizer.addLayout(nbr)
218
219
220 boutons = QHBoxLayout()
221 fermer = QPushButton("Fermer")
222 boutons.addWidget(fermer)
223 lancer = QPushButton("Lancer l'experience")
224 boutons.addWidget(lancer)
225 lancer.setDefault(True)
226 fermer.clicked.connect(self.close)
227 lancer.clicked.connect(self.actualiser)
228
229 sizer.addLayout(boutons)
230 self.setLayout(sizer)
231
232
233 def actualiser(self, event = None):
234 self.parent.actualiser(False)
235 self.parent.graph = 'batons'
236 echantillon = self.sc1.value()
237 self.parent.intervalle_fluctuation = (echantillon if self.cb.isChecked() else None)
238 n = self.sc2.value()
239 esperance = self.experience.value()
240 self.parent.ajouter_valeurs(*[sondage(esperance, echantillon) for i in range(n)])
241 self.parent.calculer()
242 self.parent.legende_x = "résultat des sondages (en %)"
243 self.parent.legende_y = "nombre de sondages"
244 self.parent.affiche()
245
246
247 def EvtCheckBox(self, event):
248 echantillon = self.sc1.value()
249 self.parent.intervalle_fluctuation = (echantillon if self.cb.isChecked() else None)
250 self.parent.affiche()
251
252 def closeEvent(self, event):
253 self.parent.intervalle_fluctuation = None
254