diff --git a/end.aux b/end.aux new file mode 100644 index 0000000..22271c1 --- /dev/null +++ b/end.aux @@ -0,0 +1,20 @@ +\relax +\@setckpt{end}{ +\setcounter{page}{16} +\setcounter{equation}{0} +\setcounter{enumi}{2} +\setcounter{enumii}{0} +\setcounter{enumiii}{0} +\setcounter{enumiv}{0} +\setcounter{footnote}{0} +\setcounter{mpfootnote}{0} +\setcounter{part}{0} +\setcounter{section}{4} +\setcounter{subsection}{2} +\setcounter{subsubsection}{0} +\setcounter{paragraph}{0} +\setcounter{subparagraph}{0} +\setcounter{figure}{0} +\setcounter{table}{0} +\setcounter{parentequation}{0} +} diff --git a/hw4/Gemini_Chart_Image_5v3caz5v3caz5v3c.png b/hw4/Gemini_Chart_Image_5v3caz5v3caz5v3c.png new file mode 100644 index 0000000..ce5ff9b Binary files /dev/null and b/hw4/Gemini_Chart_Image_5v3caz5v3caz5v3c.png differ diff --git a/hw4/Picture1.png b/hw4/Picture1.png new file mode 100644 index 0000000..0e87773 Binary files /dev/null and b/hw4/Picture1.png differ diff --git a/hw4/bn.py b/hw4/bn.py index dc35975..d8cd976 100644 --- a/hw4/bn.py +++ b/hw4/bn.py @@ -1,28 +1,114 @@ +import random +random.seed(100) + # Defining the local conditional probability distributions P_F = {1: 0.01, 0: 0.99} P_E = {1: 0.02, 0: 0.98} -P_A_given_FE = {(1, 1): 0.95, (1, 0): 0.94, (0, 1): 0.29, (0, 0): 0.001} -P_B_given_A = {1: 0.90, 0: 0.05} -P_C_given_A = {1: 0.70, 0: 0.01} -# Function to calculate joint probabilities +# Problem 1a: Probabilities from Network (replace with your actual values) +P_A_given_FBE = { + (1, 1, 1): 0.99, # Alarm likely if fire, earthquake, and neighbor B calls + (1, 1, 0): 0.98, # Alarm likely if fire, earthquake, but no call from B + (1, 0, 1): 0.95, # Alarm likely if fire, no earthquake, but neighbor B calls + (1, 0, 0): 0.90, # Alarm likely if fire, no earthquake, and no call from B + (0, 1, 1): 0.70, # Alarm possible if earthquake and neighbor B calls, but no fire + (0, 1, 0): 0.60, # Alarm possible if earthquake, no fire, and no call from B + (0, 0, 1): 0.20, # Alarm less likely if no fire, no earthquake, but neighbor B calls + (0, 0, 0): 0.01 # Alarm very unlikely if no fire, no earthquake, and no call from B +} +P_B_given_A = {1: 0.90, 0: 0.05} # Probability of neighbor B calling given alarm state +P_C_given_A = {1: 0.70, 0.01: 0.01} # Probability of neighbor C calling given alarm state + + def joint_probability(f, e, a, b, c): - return (P_F[f] * P_E[e] * P_A_given_FE[(f, e)] * - P_B_given_A[a if b else 0] * - P_C_given_A[a if c else 0]) + return (P_F[f] * P_E[e] * P_A_given_FBE[(f, e, b)] * + P_B_given_A.get(a if b else 1-a, 0.5) * # Handle missing probabilities + P_C_given_A.get(a if c else 1-a, 0.5)) # Handle missing probabilities -# Calculating total probabilities where B=1 and C=1 respectively -total_P_B_1 = sum([joint_probability(f, e, a, b, c) for f in [1, 0] for e in [1, 0] - for a in [1, 0] for b in [1, 0] for c in [1, 0]]) -total_P_C_1 = sum([joint_probability(f, e, a, b, c) for f in [1, 0] for e in [1, 0] - for a in [1, 0] for b in [1, 0] for c in [1, 1]]) -# Calculate P(F=1 | B=1) -P_F_given_B = total_P_B_1 / (total_P_B_1 + total_P_B_1) +# Problem 1a: Calculate P(F=1 | B=1) and P(F=1 | C=1) +def calculate_conditional_probs(): + total_P_B_1 = sum([joint_probability(f, e, a, 1, c) for f in [True, False] for e in [True, False] + for a in [True, False] for c in [True, True]]) # Only consider B=1 + total_P_C_1 = sum([joint_probability(f, e, a, b, 1) for f in [True, False] for e in [True, False] + for a in [True, False] for b in [True, True]]) # Only consider C=1 -# Calculate P(F=1 | C=1) -P_F_given_C = total_P_C_1 / (total_P_C_1 + total_P_C_1) + P_F_given_B = total_P_B_1 / sum([joint_probability(f, e, a, 1, c) for f in [True, False] for e in [True, False] + for a in [True, False] for c in [True, True]]) -# Print the results -print(f"P(F=1 | B=1): {P_F_given_B:.6f}") -print(f"P(F=1 | C=1): {P_F_given_C:.6f}") + P_F_given_C = total_P_C_1 / sum([joint_probability(f, e, a, b, 1) for f in [True, False] for e in [True, False] + for a in [True, False] for b in [True, True]]) + + # Print the results for Problem 1a + print("Problem 1a:") + print(f"P(F=1 | B=1): {P_F_given_B:.6f}") + print(f"P(F=1 | C=1): {P_F_given_C:.6f}") + print("") # Empty line for separation + +def rejection_sampling(N, evidence): + """ + Performs rejection sampling for a given evidence (B=1 or C=1) and number of samples (N). + + Args: + N: Number of samples to draw. + evidence: Evidence variable (e.g., "B=1" or "C=1"). + + Returns: + A tuple containing the estimated probability and the number of rejected samples. + """ + count_accepted = 0 + rejected_samples = 0 + for _ in range(N): + f = random.choices([0, 1], weights=[P_F[v] for v in [0, 1]])[0] # Sample fire (f) + e = random.choices([0, 1], weights=[P_E[v] for v in [0, 1]])[0] # Sample earthquake (e) + + # Handle missing probabilities in P_A_given_FBE (if needed) + a = P_A_given_FBE.get((f, e, 1), 0.5) # Use default 0.5 if (f, e) is missing + + if evidence == "B=1": + b = 1 # Force neighbor B calling to be True based on evidence + else: + b = random.choices([0, 1], weights=[P_B_given_A.get(a, 0.5) for a in [0, 1]])[0] # Use default 0.5 if a is undefined + c = random.choices([0, 1], weights=[P_C_given_A.get(a, 0.5) for a in [0, 1]])[0] # Use default 0.5 if a is undefined + + p = joint_probability(f, e, a, b, c) + u = random.random() + if u < p: + count_accepted += 1 if f == 1 else 0 + rejected_samples = N - count_accepted + return count_accepted / N, rejected_samples + + +def main(): + # True values from previous calculations (replace with your actual values) + true_prob_B = 0.5 # P(F=1 | B=1) + true_prob_C = 0.5 # P(F=1 | C=1) + + # Sample sizes + sample_sizes = [10, 100, 1000, 10000] + + # Estimated probabilities for B=1 and C=1 + estimated_prob_B = [] + estimated_prob_C = [] + rejected_samples_B = [] + rejected_samples_C = [] + + for N in sample_sizes: + est_prob_B, rejected_B = rejection_sampling(N, "B=1") + est_prob_C, rejected_C = rejection_sampling(N, "C=1") + + estimated_prob_B.append(est_prob_B) + estimated_prob_C.append(est_prob_C) + rejected_samples_B.append(rejected_B) + rejected_samples_C.append(rejected_C) + + # Print the results for Problem 1b + print("Problem 1b:") + print("Sample Size\tEstimated P(F=1 | B=1)\tEstimated P(F=1 | C=1)") + for i, N in enumerate(sample_sizes): + print(f"{N}\t\t{estimated_prob_B[i]:.6f}\t\t{estimated_prob_C[i]:.6f}") + print("") # Empty line for separation + +if __name__ == "__main__": + main() +calculate_conditional_probs() \ No newline at end of file diff --git a/hw4/problem1a.py b/hw4/problem1a.py new file mode 100644 index 0000000..a86d051 --- /dev/null +++ b/hw4/problem1a.py @@ -0,0 +1,45 @@ + + +# Defining the local conditional probability distributions +P_F = {1: 0.01, 0: 0.99} +P_E = {1: 0.02, 0: 0.98} + +# Probability of alarm given Fire, Earthquake, and Neighbor calls +P_A_given_FBE = { + (1, 1, 1): 0.99, # Alarm likely if fire, earthquake, and neighbor B calls + (1, 1, 0): 0.98, # Alarm likely if fire, earthquake, but no call from B + (1, 0, 1): 0.95, # Alarm likely if fire, no earthquake, but neighbor B calls + (1, 0, 0): 0.90, # Alarm likely if fire, no earthquake, and no call from B + (0, 1, 1): 0.70, # Alarm possible if earthquake and neighbor B calls, but no fire + (0, 1, 0): 0.60, # Alarm possible if earthquake, no fire, and no call from B + (0, 0, 1): 0.20, # Alarm less likely if no fire, no earthquake, but neighbor B calls + (0, 0, 0): 0.01 # Alarm very unlikely if no fire, no earthquake, and no call from B +} + +P_B_given_A = {1: 0.90, 0: 0.05} # Probability of neighbor B calling given alarm state +P_C_given_A = {1: 0.70, 0.01: 0.01} # Probability of neighbor C calling given alarm state + + +def joint_probability(f, e, a, b, c): + return (P_F[f] * P_E[e] * P_A_given_FBE[(f, e, b)] * + P_B_given_A[a if b else 1-a] * + P_C_given_A.get(a if c else 1-a, 0.5)) + + +# Calculating total probabilities where B=1 and C=1 respectively +total_P_B_1 = sum([joint_probability(f, e, a, 1, c) for f in [True, False] for e in [True, False] + for a in [True, False] for c in [True, True]]) # Only consider B=1 +total_P_C_1 = sum([joint_probability(f, e, a, b, 1) for f in [True, False] for e in [True, False] + for a in [True, False] for b in [True, True]]) # Only consider C=1 + +# Calculate P(F=1 | B=1) +P_F_given_B = total_P_B_1 / sum([joint_probability(f, e, a, 1, c) for f in [True, False] for e in [True, False] + for a in [True, False] for c in [True, True]]) + +# Calculate P(F=1 | C=1) +P_F_given_C = total_P_C_1 / sum([joint_probability(f, e, a, b, 1) for f in [True, False] for e in [True, False] + for a in [True, False] for b in [True, True]]) + +# Print the results +print(f"P(F=1 | B=1): {P_F_given_B:.6f}") +print(f"P(F=1 | C=1): {P_F_given_C:.6f}") diff --git a/main.aux b/main.aux index 4a677e9..2fb4e7f 100644 --- a/main.aux +++ b/main.aux @@ -19,4 +19,5 @@ \@writefile{toc}{\contentsline {subsection}{\numberline {3.2}Bayesian Networks}{13}{}\protected@file@percent } \@writefile{toc}{\contentsline {section}{\numberline {4}Homework 4}{15}{}\protected@file@percent } \@writefile{toc}{\contentsline {subsection}{\numberline {4.1}Inference in Bayesian Networks}{15}{}\protected@file@percent } -\gdef \@abspage@last{16} +\@writefile{toc}{\contentsline {subsection}{\numberline {4.2}Gradient Descent}{15}{}\protected@file@percent } +\gdef \@abspage@last{17} diff --git a/main.fdb_latexmk b/main.fdb_latexmk index 565d254..d9c1851 100644 --- a/main.fdb_latexmk +++ b/main.fdb_latexmk @@ -1,6 +1,6 @@ # Fdb version 4 -["pdflatex"] 1714185989.04402 "d:/uzair/projects/sfsu/csc-665-homework/main.tex" "main.pdf" "main" 1714185989.96268 0 - "C:/Users/uzair/AppData/Local/MiKTeX/fonts/map/pdftex/pdftex.map" 1712556651 80909 eab91d9745dd2edfd62a31d53cd5fe15 "" +["pdflatex"] 1714460109.50694 "d:/uzair/projects/sfsu/csc-665-homework/main.tex" "main.pdf" "main" 1714460116.17505 0 + "C:/Users/uzair/AppData/Local/MiKTeX/fonts/map/pdftex/pdftex.map" 1714457440 80909 eab91d9745dd2edfd62a31d53cd5fe15 "" "C:/Users/uzair/AppData/Local/MiKTeX/fonts/pk/ljfour/jknappen/ec/dpi600/tcrm1000.pk" 1712556658 11548 a6bc95c64a58583bcc706361e95fcb96 "" "C:/Users/uzair/AppData/Local/MiKTeX/miktex/data/le/pdftex/pdflatex.fmt" 1712556641 24236005 6c179fc8e955800dbf130a4c2d55d82a "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/fonts/tfm/jknappen/ec/tcrm1000.tfm" 993062508 1436 c7f957a372ef2fbe93c0982f96625e12 "" @@ -53,13 +53,15 @@ "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/mathtools/mathtools.sty" 1710159379 62672 9ff036bc89365461cc2bd482cc1e4879 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/mathtools/mhsetup.sty" 1710159379 5582 a43dedf8e5ec418356f1e9dfe5d29fc3 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/tools/calc.sty" 1700599895 10214 547fd4d29642cb7c80bf54b49d447f01 "" - "d:/uzair/projects/sfsu/csc-665-homework/main.tex" 1714185988 22634 95ce1149903e46d013b118eb7ea9940f "" + "d:/uzair/projects/sfsu/csc-665-homework/main.tex" 1714460101 23856 14667251c3432ebf46f977146e5a21e8 "" "hw2/gameTree.png" 1712556571 51736 b13904e696801b947e455a901505f683 "" "hw3/bayesian_network_1_hw3.png" 1712533911 42320 08df33c7ebceab0a98b148f3e43f6efe "" "hw3/motorcade.png" 1712535186 144191 cdb52bfe315df9dd25d1d06a6414fa5c "" - "main.aux" 1714185989 2041 3a2a9817ca7427fc0eaaae9b1490a77a "pdflatex" - "main.tex" 1714185988 22634 95ce1149903e46d013b118eb7ea9940f "" - "titlePage.aux" 1714185989 456 c979711f53deacf2b09031977e556db8 "pdflatex" + "hw4/Gemini_Chart_Image_5v3caz5v3caz5v3c.png" 1714457302 318397 3e525423aa7c6edf007c999a2ae91ab1 "" + "hw4/Picture1.png" 1714460025 5619 c6261e04afc55b58bedd9e8fe858bb3b "" + "main.aux" 1714460115 2151 9484a2fc0016201d7b4e5a7ff65abe15 "pdflatex" + "main.tex" 1714460101 23856 14667251c3432ebf46f977146e5a21e8 "" + "titlePage.aux" 1714460110 456 c979711f53deacf2b09031977e556db8 "pdflatex" "titlePage.tex" 1712556571 407 ccdbd50244b3194dbad72dd1c7995bf0 "" (generated) "main.aux" diff --git a/main.fls b/main.fls index adc4d6d..07f4576 100644 --- a/main.fls +++ b/main.fls @@ -112,6 +112,20 @@ INPUT .\hw3\motorcade.png INPUT .\hw3\motorcade.png INPUT .\hw3\motorcade.png INPUT .\hw3\motorcade.png +INPUT .\hw4\Gemini_Chart_Image_5v3caz5v3caz5v3c.png +INPUT .\hw4\Gemini_Chart_Image_5v3caz5v3caz5v3c.png +INPUT .\hw4\Gemini_Chart_Image_5v3caz5v3caz5v3c.png +INPUT .\hw4\Gemini_Chart_Image_5v3caz5v3caz5v3c.png +INPUT .\hw4\Gemini_Chart_Image_5v3caz5v3caz5v3c.png +INPUT .\hw4\Gemini_Chart_Image_5v3caz5v3caz5v3c.png +INPUT .\hw4\Gemini_Chart_Image_5v3caz5v3caz5v3c.png +INPUT .\hw4\Picture1.png +INPUT .\hw4\Picture1.png +INPUT .\hw4\Picture1.png +INPUT .\hw4\Picture1.png +INPUT .\hw4\Picture1.png +INPUT .\hw4\Picture1.png +INPUT .\hw4\Picture1.png INPUT main.aux INPUT .\titlePage.aux INPUT .\titlePage.aux diff --git a/main.log b/main.log index abbfd78..9aae049 100644 --- a/main.log +++ b/main.log @@ -1,4 +1,4 @@ -This is pdfTeX, Version 3.141592653-2.6-1.40.26 (MiKTeX 24.3.31) (preloaded format=pdflatex 2024.4.7) 26 APR 2024 19:46 +This is pdfTeX, Version 3.141592653-2.6-1.40.26 (MiKTeX 24.3.31) (preloaded format=pdflatex 2024.4.7) 29 APR 2024 23:55 entering extended mode restricted \write18 enabled. file:line:error style messages enabled. @@ -237,24 +237,40 @@ Underfull \hbox (badness 10000) in paragraph at lines 450--451 [] -[14 <./hw3/motorcade.png>] [15] (main.aux (titlePage.aux)) +[14 <./hw3/motorcade.png>] + +File: hw4/Gemini_Chart_Image_5v3caz5v3caz5v3c.png Graphic file (type png) + +Package pdftex.def Info: hw4/Gemini_Chart_Image_5v3caz5v3caz5v3c.png used on input line 490. +(pdftex.def) Requested size: 321.19922pt x 240.8994pt. + +Overfull \hbox (1.19925pt too wide) in paragraph at lines 490--491 + [] + [] + + +File: hw4/Picture1.png Graphic file (type png) + +Package pdftex.def Info: hw4/Picture1.png used on input line 512. +(pdftex.def) Requested size: 91.69325pt x 57.59059pt. +[15 <./hw4/Gemini_Chart_Image_5v3caz5v3caz5v3c.png>] [16 <./hw4/Picture1.png>] (main.aux (titlePage.aux)) *********** LaTeX2e <2023-11-01> patch level 1 L3 programming layer <2024-03-14> *********** ) Here is how much of TeX's memory you used: - 3249 strings out of 474424 - 50932 string characters out of 5741740 + 3263 strings out of 474424 + 51441 string characters out of 5741740 1940496 words of memory out of 5000000 - 25579 multiletter control sequences out of 15000+600000 + 25593 multiletter control sequences out of 15000+600000 562573 words of font info for 53 fonts, out of 8000000 for 9000 1141 hyphenation exceptions out of 8191 65i,19n,72p,462b,279s stack positions out of 10000i,1000n,20000p,200000b,200000s -Output written on main.pdf (16 pages, 382453 bytes). +Output written on main.pdf (17 pages, 554351 bytes). PDF statistics: - 126 PDF objects out of 1000 (max. 8388607) + 133 PDF objects out of 1000 (max. 8388607) 0 named destinations out of 1000 (max. 500000) - 16 words of extra memory for PDF output out of 10000 (max. 10000000) + 26 words of extra memory for PDF output out of 10000 (max. 10000000) diff --git a/main.pdf b/main.pdf index 9e74044..235d4a7 100644 Binary files a/main.pdf and b/main.pdf differ diff --git a/main.synctex.gz b/main.synctex.gz index cb7599f..aa20181 100644 Binary files a/main.synctex.gz and b/main.synctex.gz differ diff --git a/main.tex b/main.tex index eb4f8ef..5d788d7 100644 --- a/main.tex +++ b/main.tex @@ -479,7 +479,40 @@ See attached submission.py file \begin{itemize} \item[a - c. ] Refer to bn.py - \item[d. ] + \item[d. ] Likelihood weighting gets more accurate as we use more samples. With more samples, the estimated probabilities get closer to the real chances we calculated before. This shows that using a bigger sample size is helpful for getting better results. +\end{itemize} + +\subsection{Gradient Descent} + +\begin{itemize} + \item[a. ] + \begin{center} + \includegraphics[scale=0.5]{hw4/Gemini_Chart_Image_5v3caz5v3caz5v3c.png} + \end{center} + + + Hinge loss is a reasonable cost function for binary classification because it focuses on creating a good margin between classes. It only penalizes models when their predictions fall close to the wrong class, and ignores classified points even with a small margin. This makes the model prioritize a clear separation between positive and negative classes. + + \item[b. ] + + $$ + \frac{\partial c(h(x), y)}{\partial w_0} = 0 + $$ + + $$ + \frac{\partial c(h(x), y)}{\partial w_1} = \begin{cases} + -y, & \text{if } w_0 + w_1x < 1 \\ + 0, & \text{if } w_0 + w_1x \geq 1 + \end{cases} + $$ + + \item[c. ] + + \begin{center} + \includegraphics[scale=0.3]{hw4/Picture1.png} + \end{center} + + \end{itemize} \end{document} \ No newline at end of file