Skip to content

Commit 08021bb

Browse files
committed
test: use constructor seeds in model specs
1 parent cce8506 commit 08021bb

3 files changed

Lines changed: 21 additions & 24 deletions

File tree

spec/irt_ruby/rasch_model_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,12 @@
160160
end
161161
end
162162

163-
context "Deterministic seed" do
164-
it "produces consistent results with the same seed" do
165-
srand(123)
166-
model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05)
163+
context "Constructor seed" do
164+
it "produces consistent results with the same seed option" do
165+
model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123)
167166
result1 = model1.fit
168167

169-
srand(123)
170-
model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05)
168+
model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123)
171169
result2 = model2.fit
172170

173171
expect(result1[:abilities]).to eq(result2[:abilities])
@@ -179,10 +177,11 @@
179177
it "handles a moderately large dataset without error" do
180178
n_examinees = 20
181179
n_items = 10
180+
rng = Random.new(123)
182181
big_data = Array.new(n_examinees) do
183-
Array.new(n_items) { rand < 0.5 ? 1 : 0 }
182+
Array.new(n_items) { rng.rand < 0.5 ? 1 : 0 }
184183
end
185-
model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05)
184+
model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05, seed: 123)
186185
expect { model.fit }.not_to raise_error
187186

188187
results = model.fit

spec/irt_ruby/three_parameter_model_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,12 @@
210210
end
211211
end
212212

213-
context "Deterministic seed" do
214-
it "produces consistent results with the same seed" do
215-
srand(123)
216-
model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05)
213+
context "Constructor seed" do
214+
it "produces consistent results with the same seed option" do
215+
model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123)
217216
result1 = model1.fit
218217

219-
srand(123)
220-
model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05)
218+
model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123)
221219
result2 = model2.fit
222220

223221
expect(result1[:abilities]).to eq(result2[:abilities])
@@ -231,11 +229,12 @@
231229
it "handles a moderately large dataset without error" do
232230
n_examinees = 20
233231
n_items = 8
232+
rng = Random.new(123)
234233
big_data = Array.new(n_examinees) do
235-
Array.new(n_items) { rand < 0.5 ? 1 : 0 }
234+
Array.new(n_items) { rng.rand < 0.5 ? 1 : 0 }
236235
end
237236

238-
model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05)
237+
model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05, seed: 123)
239238
expect { model.fit }.not_to raise_error
240239

241240
results = model.fit

spec/irt_ruby/two_parameter_model_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,12 @@
169169
end
170170
end
171171

172-
context "Deterministic seed" do
173-
it "yields consistent results with the same seed" do
174-
srand(123)
175-
model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05)
172+
context "Constructor seed" do
173+
it "yields consistent results with the same seed option" do
174+
model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123)
176175
result1 = model1.fit
177176

178-
srand(123)
179-
model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05)
177+
model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123)
180178
result2 = model2.fit
181179

182180
expect(result1[:abilities]).to eq(result2[:abilities])
@@ -189,11 +187,12 @@
189187
it "handles a moderately sized dataset without error" do
190188
n_examinees = 20
191189
n_items = 8
190+
rng = Random.new(123)
192191
big_data = Array.new(n_examinees) do
193-
Array.new(n_items) { rand < 0.5 ? 1 : 0 }
192+
Array.new(n_items) { rng.rand < 0.5 ? 1 : 0 }
194193
end
195194

196-
model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05)
195+
model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05, seed: 123)
197196
expect { model.fit }.not_to raise_error
198197

199198
results = model.fit

0 commit comments

Comments
 (0)