Quiz -12
pdf
keyboard_arrow_up
School
Stony Brook University *
*We aren’t endorsed by this school
Course
572
Subject
Industrial Engineering
Date
Dec 6, 2023
Type
Pages
15
Uploaded by GrandIce91895
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
1/15
In [10]:
import
pandas
as
pd
import
numpy
as
np
# Setting a random seed for reproducibility
np
.
random
.
seed
(
789
)
# Generating conversions for Page A (e.g., a 20% conversion rate) and Page B (e.g., a 25% conversion rate)
page_a_results
=
np
.
random
.
binomial
(
1
,
0.20
,
3000
)
page_b_results
=
np
.
random
.
binomial
(
1
,
0.25
,
3000
)
# Creating the dataset
data
=
pd
.
DataFrame
({
'page'
: [
'A'
]
*
3000
+
[
'B'
]
*
3000
,
'converted'
:
np
.
concatenate
([
page_a_results
,
page_b_results
])
})
# Saving the dataset to a CSV file
data
.
to_csv
(
'landing_page_conversion.csv'
,
index
=
False
)
In [11]:
data
=
pd
.
read_csv
(
'landing_page_conversion.csv'
)
In [12]:
data
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
2/15
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6000 entries, 0 to 5999
Data columns (total 2 columns):
#
Column
Non-Null Count
Dtype
---
------
--------------
-----
0
page
6000 non-null
object
1
converted
6000 non-null
int64
dtypes: int64(1), object(1)
memory usage: 93.9+ KB
Out[12]:
In [13]:
data
.
info
()
In [14]:
data
.
head
(
5
)
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
3/15
Out[14]:
In [15]:
data
.
describe
()
Out[15]:
In [17]:
# Check for any missing values
missing_values
=
data
.
isnull
()
.
sum
()
# Address any missing values
if
missing_values
.
any
():
data
=
data
.
dropna
()
In [18]:
# Display the initial statistics of conversions for each page
page_conversions
=
data
.
groupby
(
'page'
)[
'converted'
]
.
agg
([
'count'
,
'mean'
])
print
(
"Initial statistics of conversions for each page:\n"
,
page_conversions
)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
4/15
Initial statistics of conversions for each page:
count
mean
page
A
3000
0.198333
B
3000
0.264333
Posterior distribution parameters for Page A: Alpha = 597 Beta = 2415
Posterior distribution parameters for Page B: Alpha = 795 Beta = 2217
In [22]:
import
matplotlib.pyplot
as
plt
import
scipy.stats
as
stats
# Define the prior belief as a beta distribution
prior_alpha
=
2
prior_beta
=
10
# Update the prior belief based on observed conversion rates for each page
observed_page_a
=
data
[
data
[
'page'
]
==
'A'
][
'converted'
]
observed_page_b
=
data
[
data
[
'page'
]
==
'B'
][
'converted'
]
posterior_alpha_page_a
=
prior_alpha
+
sum
(
observed_page_a
)
posterior_beta_page_a
=
prior_beta
+
len
(
observed_page_a
)
-
sum
(
observed_page_a
)
posterior_alpha_page_b
=
prior_alpha
+
sum
(
observed_page_b
)
posterior_beta_page_b
=
prior_beta
+
len
(
observed_page_b
)
-
sum
(
observed_page_b
)
# Calculate the posterior distribution of the conversion rates for both pages using the beta distribution
x
=
np
.
linspace
(
0
,
1
,
100
)
y_page_a
=
stats
.
beta
.
pdf
(
x
,
posterior_alpha_page_a
,
posterior_beta_page_a
)
y_page_b
=
stats
.
beta
.
pdf
(
x
,
posterior_alpha_page_b
,
posterior_beta_page_b
)
print
(
"Posterior distribution parameters for Page A: Alpha ="
,
posterior_alpha_page_a
,
"Beta ="
,
posterior_be
print
(
"Posterior distribution parameters for Page B: Alpha ="
,
posterior_alpha_page_b
,
"Beta ="
,
posterior_be
In [21]:
# Define the prior belief as a beta distribution
prior_alpha
=
2
prior_beta
=
10
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
5/15
# Update the prior belief based on observed conversion rates for each page
observed_page_a
=
data
[
data
[
'page'
]
==
'A'
][
'converted'
]
observed_page_b
=
data
[
data
[
'page'
]
==
'B'
][
'converted'
]
posterior_alpha_page_a
=
prior_alpha
+
sum
(
observed_page_a
)
posterior_beta_page_a
=
prior_beta
+
len
(
observed_page_a
)
-
sum
(
observed_page_a
)
posterior_alpha_page_b
=
prior_alpha
+
sum
(
observed_page_b
)
posterior_beta_page_b
=
prior_beta
+
len
(
observed_page_b
)
-
sum
(
observed_page_b
)
# Calculate the prior and posterior distributions of the conversion rates for both pages using the beta distri
x
=
np
.
linspace
(
0
,
1
,
100
)
y_prior
=
stats
.
beta
.
pdf
(
x
,
prior_alpha
,
prior_beta
)
y_posterior_page_a
=
stats
.
beta
.
pdf
(
x
,
posterior_alpha_page_a
,
posterior_beta_page_a
)
y_posterior_page_b
=
stats
.
beta
.
pdf
(
x
,
posterior_alpha_page_b
,
posterior_beta_page_b
)
# Find the modes of the posterior distributions
mode_page_a
=
(
posterior_alpha_page_a
-
1
)
/
(
posterior_alpha_page_a
+
posterior_beta_page_a
-
2
)
mode_page_b
=
(
posterior_alpha_page_b
-
1
)
/
(
posterior_alpha_page_b
+
posterior_beta_page_b
-
2
)
# Plot the prior and posterior distributions
plt
.
figure
(
figsize
=
(
12
,
6
))
plt
.
plot
(
x
,
y_prior
,
label
=
'Prior Distribution'
,
color
=
'g'
,
linestyle
=
'--'
)
plt
.
plot
(
x
,
y_posterior_page_a
,
label
=
'Posterior Distribution Page A'
,
color
=
'b'
)
plt
.
plot
(
x
,
y_posterior_page_b
,
label
=
'Posterior Distribution Page B'
,
color
=
'r'
)
plt
.
axvline
(
x
=
mode_page_a
,
color
=
'b'
,
linestyle
=
'--'
)
plt
.
axvline
(
x
=
mode_page_b
,
color
=
'r'
,
linestyle
=
'--'
)
plt
.
text
(
mode_page_a
,
8
,
f'Mode A: {
mode_page_a
:.2f}'
,
color
=
'b'
,
ha
=
'center'
)
plt
.
text
(
mode_page_b
,
8
,
f'Mode B: {
mode_page_b
:.2f}'
,
color
=
'r'
,
ha
=
'center'
)
plt
.
title
(
'Prior and Posterior Distributions of Conversion Rates for Page A and Page B'
)
plt
.
xlabel
(
'Conversion Rate'
)
plt
.
ylabel
(
'Density'
)
plt
.
legend
()
plt
.
show
()
print
(
"Posterior distribution parameters for Page A: Alpha ="
,
posterior_alpha_page_a
,
"Beta ="
,
posterior_be
print
(
"Posterior distribution parameters for Page B: Alpha ="
,
posterior_alpha_page_b
,
"Beta ="
,
posterior_be
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
6/15
Posterior distribution parameters for Page A: Alpha = 597 Beta = 2415
Posterior distribution parameters for Page B: Alpha = 795 Beta = 2217
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
7/15
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
8/15
In [23]:
import
pandas
as
pd
import
numpy
as
np
np
.
random
.
seed
(
456
)
def
generate_transitions
(
n
):
states
=
[
'S'
,
'C'
,
'D'
]
# A mock transition matrix (you can adjust these values)
transition_matrix
=
{
'S'
: {
'S'
:
0.7
,
'C'
:
0.1
,
'D'
:
0.2
},
'C'
: {
'S'
:
0
,
'C'
:
1
,
'D'
:
0
},
'D'
: {
'S'
:
0.4
,
'C'
:
0.3
,
'D'
:
0.3
}
}
current_state
=
np
.
random
.
choice
(
states
)
sequence
=
[
current_state
]
for
_
in
range
(
n
-
1
):
next_state
=
np
.
random
.
choice
(
list
(
transition_matrix
[
current_state
]
.
keys
()),
p
=
list
(
transition_matrix
[
current_state
]
.
values
()))
sequence
.
append
(
next_state
)
current_state
=
next_state
return
sequence
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
9/15
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 5 columns):
#
Column
Non-Null Count
Dtype
---
------
--------------
-----
0
month_1
1000 non-null
object
1
month_2
1000 non-null
object
2
month_3
1000 non-null
object
3
month_4
1000 non-null
object
4
month_5
1000 non-null
object
dtypes: object(5)
memory usage: 39.2+ KB
num_customers
=
1000
data
=
[
generate_transitions
(
5
)
for
_
in
range
(
num_customers
)]
df
=
pd
.
DataFrame
(
data
,
columns
=
[
f'month_{
i
}'
for
i
in
range
(
1
,
6
)])
df
.
to_csv
(
'customer_states.csv'
,
index
=
False
)
In [24]:
data
=
pd
.
read_csv
(
'customer_states.csv'
)
In [25]:
data
.
info
()
In [26]:
data
.
describe
()
Out[26]:
In [27]:
data
.
head
(
5
)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
10/15
Out[27]:
In [28]:
# Check for any missing values
missing_values
=
data
.
isnull
()
.
sum
()
# Handle missing values appropriately
if
missing_values
.
any
():
data
=
data
.
dropna
()
In [29]:
# Display the transition count for consecutive months
transitions
=
data
.
apply
(
lambda
row
: [(
row
[
i
],
row
[
i
+
1
])
for
i
in
range
(
len
(
row
)
-
1
)],
axis
=
1
)
transition_counts
=
pd
.
Series
(
transitions
.
values
.
flatten
())
.
value_counts
()
print
(
"Transition Count for Consecutive Months:"
)
print
(
transition_counts
)
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
11/15
Transition Count for Consecutive Months:
[(C, C), (C, C), (C, C), (C, C)]
335
[(D, C), (C, C), (C, C), (C, C)]
98
[(S, S), (S, S), (S, S), (S, S)]
70
[(D, S), (S, S), (S, S), (S, S)]
55
[(S, S), (S, S), (S, S), (S, D)]
33
[(D, D), (D, C), (C, C), (C, C)]
33
[(S, C), (C, C), (C, C), (C, C)]
31
[(S, D), (D, C), (C, C), (C, C)]
23
[(D, D), (D, S), (S, S), (S, S)]
20
[(S, S), (S, C), (C, C), (C, C)]
18
[(S, S), (S, S), (S, C), (C, C)]
18
[(S, S), (S, S), (S, S), (S, C)]
18
[(D, S), (S, S), (S, C), (C, C)]
17
[(D, S), (S, S), (S, S), (S, D)]
14
[(S, D), (D, S), (S, S), (S, S)]
13
[(S, S), (S, S), (S, D), (D, C)]
11
[(S, S), (S, S), (S, D), (D, S)]
11
[(D, S), (S, C), (C, C), (C, C)]
10
[(S, D), (D, D), (D, S), (S, S)]
10
[(S, S), (S, D), (D, D), (D, D)]
9
[(D, D), (D, D), (D, S), (S, S)]
9
[(D, S), (S, S), (S, D), (D, D)]
8
[(D, S), (S, D), (D, C), (C, C)]
8
[(S, S), (S, D), (D, C), (C, C)]
8
[(D, D), (D, D), (D, D), (D, C)]
7
[(D, S), (S, S), (S, S), (S, C)]
7
[(D, S), (S, S), (S, D), (D, C)]
7
[(D, S), (S, D), (D, S), (S, S)]
7
[(D, D), (D, S), (S, C), (C, C)]
6
[(S, S), (S, S), (S, D), (D, D)]
6
[(D, S), (S, S), (S, D), (D, S)]
6
[(D, D), (D, D), (D, C), (C, C)]
5
[(S, S), (S, D), (D, S), (S, S)]
5
[(S, D), (D, S), (S, S), (S, D)]
4
[(D, D), (D, S), (S, S), (S, D)]
4
[(D, D), (D, S), (S, D), (D, C)]
4
[(S, D), (D, D), (D, C), (C, C)]
4
[(S, D), (D, S), (S, D), (D, C)]
4
[(D, D), (D, D), (D, D), (D, D)]
4
[(D, D), (D, D), (D, S), (S, D)]
3
[(S, D), (D, D), (D, D), (D, D)]
3
[(D, D), (D, D), (D, D), (D, S)]
3
[(S, D), (D, S), (S, S), (S, C)]
3
[(S, D), (D, S), (S, D), (D, S)]
3
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
12/15
[(S, D), (D, S), (S, C), (C, C)]
3
[(S, S), (S, D), (D, D), (D, C)]
3
[(D, D), (D, S), (S, D), (D, D)]
3
[(D, D), (D, S), (S, S), (S, C)]
2
[(D, S), (S, D), (D, D), (D, C)]
2
[(S, D), (D, D), (D, D), (D, C)]
2
[(D, D), (D, S), (S, D), (D, S)]
2
[(S, S), (S, D), (D, D), (D, S)]
2
[(S, D), (D, D), (D, D), (D, S)]
1
[(D, S), (S, D), (D, S), (S, C)]
1
[(D, S), (S, D), (D, D), (D, S)]
1
[(S, D), (D, D), (D, S), (S, D)]
1
[(S, D), (D, D), (D, S), (S, C)]
1
[(D, S), (S, D), (D, D), (D, D)]
1
dtype: int64
In [32]:
import
seaborn
as
sns
import
matplotlib.pyplot
as
plt
# Define the transition probability matrix calculation
def
calculate_transition_matrix
(
data
):
states
=
[
'S'
,
'C'
,
'D'
]
transition_matrix
=
pd
.
DataFrame
(
index
=
states
,
columns
=
states
,
dtype
=
float
)
for
i
in
range
(
len
(
states
)):
for
j
in
range
(
len
(
states
)):
transition_count
=
0
for
k
in
range
(
len
(
data
.
columns
)
-
1
):
transition_count
+=
sum
((
data
.
iloc
[:,
k
]
==
states
[
i
])
&
(
data
.
iloc
[:,
k
+
1
]
==
states
[
j
]))
total_count
=
sum
(
data
.
apply
(
lambda
row
:
sum
(
row
==
states
[
i
]),
axis
=
1
))
if
total_count
!=
0
:
transition_matrix
.
iloc
[
i
,
j
]
=
transition_count
/
total_count
else
:
transition_matrix
.
iloc
[
i
,
j
]
=
0
return
transition_matrix
# Load the dataset customer_states.csv into a Pandas DataFrame
data
=
pd
.
read_csv
(
'customer_states.csv'
)
# Calculate the transition probability matrix
transition_matrix
=
calculate_transition_matrix
(
data
)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
13/15
# Visualize the transition probability matrix using a heatmap
plt
.
figure
(
figsize
=
(
8
,
6
))
sns
.
heatmap
(
transition_matrix
,
annot
=
True
,
cmap
=
'YlGnBu'
,
cbar
=
True
)
plt
.
title
(
'Transition Probability Matrix'
)
plt
.
xlabel
(
'To State'
)
plt
.
ylabel
(
'From State'
)
plt
.
show
()
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
14/15
Steady-State Probabilities:
S: 1.5639809708684383e-14
C: 8.881512708122446e-13
D: 5.6104326048076704e-15
Predicted State Distribution after 10 Months:
S: 0.03352152021388491
C: 0.0
D: 0.025435765813770654
In [33]:
# Calculate the steady-state probabilities
steady_state_probabilities
=
np
.
linalg
.
matrix_power
(
transition_matrix
,
100
)
steady_state_probabilities
=
steady_state_probabilities
[
0
, :]
print
(
"Steady-State Probabilities:"
)
print
(
"S:"
,
steady_state_probabilities
[
0
])
print
(
"C:"
,
steady_state_probabilities
[
1
])
print
(
"D:"
,
steady_state_probabilities
[
2
])
# Predict the state distribution of the cohort after 10 months
initial_state_distribution
=
[
1
,
0
,
0
]
# Assuming all customers start in state 'S'
predicted_state_distribution
=
np
.
dot
(
np
.
linalg
.
matrix_power
(
transition_matrix
,
10
),
initial_state_distributi
print
(
"\nPredicted State Distribution after 10 Months:"
)
print
(
"S:"
,
predicted_state_distribution
[
0
])
print
(
"C:"
,
predicted_state_distribution
[
1
])
print
(
"D:"
,
predicted_state_distribution
[
2
])
10/26/23, 4:04 PM
Quiz -12
localhost:8888/nbconvert/html/Quiz -12.ipynb?download=false
15/15
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help