0% found this document useful (0 votes)
16 views3 pages

Ygtd

The document outlines a Python script that generates all possible combinations of a binary variable 'D' for a dataset containing names and associated values. It calculates the mean differences of another variable 'Y' based on the combinations of 'D' and saves the results to a CSV file. The results include the mean values for both groups, their differences, and a probability derived from the differences.

Uploaded by

zhihanyu3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Ygtd

The document outlines a Python script that generates all possible combinations of a binary variable 'D' for a dataset containing names and associated values. It calculates the mean differences of another variable 'Y' based on the combinations of 'D' and saves the results to a CSV file. The results include the mean values for both groups, their differences, and a probability derived from the differences.

Uploaded by

zhihanyu3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pandas as pd

import itertools

data = {
"Name": ["Andy", "Ben", "Chad", "Daniel", "Edith", "Frank", "George", "Hank"],
"D": [0, 0, 0, 0, 1, 1, 1, 1],
"Y": [10, 5, 16, 3, 5, 7, 8, 10],
"Y0": [10, 5, 16, 3, 5, 7, 8, 10],
"Y1": [10, 5, 16, 3, 5, 7, 8, 10]
}

df = pd.DataFrame(data)

d_combinations = list(set(itertools.permutations([1, 1, 1, 1, 0, 0, 0, 0])))

results = []

for d_perm in d_combinations:


df["D"] = d_perm mean_Y_D1 = df[df["D"] == 1]["Y"].mean()
mean_Y_D0 = df[df["D"] == 0]["Y"].mean()
difference = mean_Y_D1 - mean_Y_D0
prob = difference/70

results.append({
"D_combination": d_perm,
"Mean_Y_D1": mean_Y_D1,
"Mean_Y_D0": mean_Y_D0,
"Difference": difference,
"Prob":prob
})

results_df = pd.DataFrame(results)

pd.set_option("display.max_rows", None)

print("所有可能组合的 Y 值平均数差值:")
print(results_df)
results_df.to_csv("D_combinations_Y_difference.csv", index=False)
print("结果已保存到 'D_combinations_Y_difference.csv'")

组合 Y1 平均 Y0 平均 差值 Prob
0 (0, 0, 1, 0, 1, 1, 1, 0) 9.00 7.00 2.0 0.028571
1 (0, 0, 0, 1, 1, 1, 0, 1) 6.25 9.75 -3.5 -0.050000
2 (0, 1, 1, 0, 1, 0, 0, 1) 9.00 7.00 2.0 0.028571
3 (1, 1, 0, 0, 0, 1, 1, 0) 7.50 8.50 -1.0 -0.014286
4 (1, 0, 0, 0, 0, 1, 1, 1) 8.75 7.25 1.5 0.021429
5 (1, 1, 0, 0, 0, 1, 0, 1) 8.00 8.00 0.0 0.000000
6 (1, 0, 0, 1, 1, 0, 0, 1) 7.00 9.00 -2.0 -0.028571
7 (0, 1, 0, 1, 0, 0, 1, 1) 6.50 9.50 -3.0 -0.042857
8 (0, 0, 1, 1, 0, 1, 1, 0) 8.50 7.50 1.0 0.014286
9 (1, 1, 1, 0, 0, 0, 1, 0) 9.75 6.25 3.5 0.050000
10 (1, 0, 0, 0, 1, 1, 1, 0) 7.50 8.50 -1.0 -0.014286
11 (0, 1, 0, 0, 1, 1, 0, 1) 6.75 9.25 -2.5 -0.035714
12 (0, 1, 0, 0, 1, 1, 1, 0) 6.25 9.75 -3.5 -0.050000
13 (0, 0, 1, 1, 0, 1, 0, 1) 9.00 7.00 2.0 0.028571
14 (1, 0, 1, 0, 1, 0, 1, 0) 9.75 6.25 3.5 0.050000
15 (0, 1, 0, 1, 0, 1, 1, 0) 5.75 10.25 -4.5 -0.064286
16 (0, 0, 1, 1, 0, 0, 1, 1) 9.25 6.75 2.5 0.035714
17 (1, 1, 0, 1, 1, 0, 0, 0) 5.75 10.25 -4.5 -0.064286
18 (1, 0, 1, 1, 1, 0, 0, 0) 8.50 7.50 1.0 0.014286
19 (0, 1, 0, 0, 1, 0, 1, 1) 7.00 9.00 -2.0 -0.028571
20 (1, 0, 0, 1, 0, 1, 0, 1) 7.50 8.50 -1.0 -0.014286
21 (1, 0, 0, 0, 1, 1, 0, 1) 8.00 8.00 0.0 0.000000
22 (1, 0, 0, 1, 0, 1, 1, 0) 7.00 9.00 -2.0 -0.028571
23 (0, 0, 1, 0, 1, 1, 0, 1) 9.50 6.50 3.0 0.042857
24 (0, 0, 0, 1, 0, 1, 1, 1) 7.00 9.00 -2.0 -0.028571
25 (1, 1, 1, 1, 0, 0, 0, 0) 8.50 7.50 1.0 0.014286
26 (0, 1, 1, 1, 1, 0, 0, 0) 7.25 8.75 -1.5 -0.021429
27 (0, 1, 0, 1, 0, 1, 0, 1) 6.25 9.75 -3.5 -0.050000
28 (1, 1, 0, 1, 0, 0, 1, 0) 6.50 9.50 -3.0 -0.042857
29 (0, 1, 0, 1, 1, 0, 1, 0) 5.25 10.75 -5.5 -0.078571
30 (0, 0, 1, 1, 1, 1, 0, 0) 7.75 8.25 -0.5 -0.007143
31 (0, 1, 1, 0, 0, 0, 1, 1) 9.75 6.25 3.5 0.050000
32 (1, 0, 1, 0, 1, 0, 0, 1) 10.25 5.75 4.5 0.064286
33 (1, 0, 1, 1, 0, 1, 0, 0) 9.00 7.00 2.0 0.028571
34 (0, 0, 0, 1, 1, 1, 1, 0) 5.75 10.25 -4.5 -0.064286
35 (0, 1, 0, 1, 1, 1, 0, 0) 5.00 11.00 -6.0 -0.085714
36 (1, 1, 1, 0, 1, 0, 0, 0) 9.00 7.00 2.0 0.028571
37 (0, 0, 0, 0, 1, 1, 1, 1) 7.50 8.50 -1.0 -0.014286
38 (0, 1, 1, 1, 0, 0, 1, 0) 8.00 8.00 0.0 0.000000
39 (0, 0, 1, 1, 1, 0, 0, 1) 8.50 7.50 1.0 0.014286
40 (1, 1, 1, 0, 0, 0, 0, 1) 10.25 5.75 4.5 0.064286
41 (0, 1, 1, 0, 1, 1, 0, 0) 8.25 7.75 0.5 0.007143
42 (1, 0, 1, 0, 0, 0, 1, 1) 11.00 5.00 6.0 0.085714
43 (0, 1, 0, 0, 0, 1, 1, 1) 7.50 8.50 -1.0 -0.014286
44 (1, 1, 0, 1, 0, 0, 0, 1) 7.00 9.00 -2.0 -0.028571
45 (1, 0, 1, 1, 0, 0, 1, 0) 9.25 6.75 2.5 0.035714
46 (0, 1, 0, 1, 1, 0, 0, 1) 5.75 10.25 -4.5 -0.064286
47 (1, 0, 1, 0, 0, 1, 0, 1) 10.75 5.25 5.5 0.078571
48 (1, 1, 1, 0, 0, 1, 0, 0) 9.50 6.50 3.0 0.042857
49 (1, 0, 1, 0, 0, 1, 1, 0) 10.25 5.75 4.5 0.064286
50 (1, 0, 0, 1, 1, 1, 0, 0) 6.25 9.75 -3.5 -0.050000
51 (0, 1, 1, 1, 0, 0, 0, 1) 8.50 7.50 1.0 0.014286
52 (0, 0, 1, 0, 1, 0, 1, 1) 9.75 6.25 3.5 0.050000
53 (0, 1, 1, 1, 0, 1, 0, 0) 7.75 8.25 -0.5 -0.007143
54 (1, 1, 0, 0, 0, 0, 1, 1) 8.25 7.75 0.5 0.007143
55 (1, 0, 1, 1, 0, 0, 0, 1) 9.75 6.25 3.5 0.050000
56 (1, 1, 0, 0, 1, 0, 1, 0) 7.00 9.00 -2.0 -0.028571
57 (1, 0, 0, 1, 0, 0, 1, 1) 7.75 8.25 -0.5 -0.007143
58 (1, 0, 0, 0, 1, 0, 1, 1) 8.25 7.75 0.5 0.007143
59 (1, 0, 1, 0, 1, 1, 0, 0) 9.50 6.50 3.0 0.042857
60 (0, 1, 1, 0, 0, 1, 1, 0) 9.00 7.00 2.0 0.028571
61 (1, 1, 0, 0, 1, 1, 0, 0) 6.75 9.25 -2.5 -0.035714
62 (0, 0, 1, 1, 1, 0, 1, 0) 8.00 8.00 0.0 0.000000
63 (1, 1, 0, 1, 0, 1, 0, 0) 6.25 9.75 -3.5 -0.050000
64 (1, 1, 0, 0, 1, 0, 0, 1) 7.50 8.50 -1.0 -0.014286
65 (0, 1, 1, 0, 0, 1, 0, 1) 9.50 6.50 3.0 0.042857
66 (0, 0, 0, 1, 1, 0, 1, 1) 6.50 9.50 -3.0 -0.042857
67 (0, 0, 1, 0, 0, 1, 1, 1) 10.25 5.75 4.5 0.064286
68 (0, 1, 1, 0, 1, 0, 1, 0) 8.50 7.50 1.0 0.014286
69 (1, 0, 0, 1, 1, 0, 1, 0) 6.50 9.50 -3.0 -0.042857

You might also like