-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdirt.py
More file actions
53 lines (41 loc) · 1.9 KB
/
dirt.py
File metadata and controls
53 lines (41 loc) · 1.9 KB
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
import numpy as np
import pandas as pd
from girth import twopl_jml, ability_map
def exp_part(diff: float, disc: float, ability: float) -> float:
return np.exp(-disc*(ability-diff))
def irf(diff: float, disc: float, ability: float):
"""Item Response (Characteristic) Function
Compute the probability that a subject with given ability will answer an item
with difficulty `diff` and discrimination `disc` correctly
"""
return 1 / (1 + exp_part(diff, disc, ability))
def iic(ability, diff, disc, scale=True):
"""Item Information Function"""
#D is a normalization scaling fator
D = 1.70
prob = irf(ability=ability, diff=diff, disc=disc)
info = (disc ** 2) * prob * (1 - prob)
if scale:
info *= (D ** 2)
return info
def se_est(ability,disc,diff):
prob = irf(ability=ability, disc=disc, diff=diff)
return 1/np.sqrt((disc**2)*prob*(1-prob))
def construct_lsat7_dataset(path_to_csv: str = "/home/daavid/lsat7.csv") -> pd.DataFrame:
lsat7 = pd.read_csv(path_to_csv)
lsat7 = lsat7.drop(columns="Unnamed: 0")
lsat7 = lsat7.rename(columns={col: col.replace(".", "_") for col in lsat7.columns})
lsat7.index.repeat(lsat7.freq)
expanded = pd.DataFrame(np.repeat(lsat7.values, lsat7.freq, axis=0), columns=lsat7.columns).drop(columns="freq")
return expanded
def fit_2pl(dataset: pd.DataFrame,transpose=True):
"""twopl_jml expects the array of responses to be in the form of items x subjects.
Args:
dataset: an array of responses in the form of items x subjects
transpose: transpose an array in the form of subject x items into one of items x subjects
"""
if transpose:
dataset = dataset.transpose()
item_params = twopl_jml(dataset)
abilities = ability_map(dataset=dataset.to_numpy(), difficulty = item_params["Difficulty"], discrimination=item_params["Discrimination"])
return item_params, abilities