diff --git a/hw5/hw5.pdf b/hw5/hw5.pdf new file mode 100644 index 0000000..6693c6f Binary files /dev/null and b/hw5/hw5.pdf differ diff --git a/hw5/regularization.py b/hw5/regularization.py new file mode 100644 index 0000000..9c74174 --- /dev/null +++ b/hw5/regularization.py @@ -0,0 +1,93 @@ +import math +from math import sin, pi +from random import random + + +def f(x): + return sin(pi * x) + + +def generate_training_examples(n=2): + xs = [random() * 2 - 1 for _ in range(n)] + return [(x, f(x)) for x in xs] + + +def fit_without_reg(examples): + """Computes values of w0 and w1 that minimize the sum-of-squared-errors cost function + + Args: + - examples: a list of two (x, y) tuples, where x is the feature and y is the label + """ + w0 = 0 + w1 = 0 + + x1, y1 = examples[0] + x2, y2 = examples[1] + + w1 = (y2 - y1) / (x2 - x1) + w0 = y1 - w1 * x1 + + return w0, w1 + + +def fit_with_reg(examples, lambda_hp, learning_rate=0.01, max_iter=1000, tol=1e-6): + """Computes values of w0 and w1 that minimize the regularized sum-of-squared-errors cost function + + Args: + - examples: a list of two (x, y) tuples, where x is the feature and y is the label + - lambda_hp: a float representing the value of the lambda hyperparameter; a larger value means more regularization + """ + w0 = 0 + w1 = 0 + for _ in range(max_iter): + # Compute the sum of squared errors + sse = sum((w0 + w1 * x - y) ** 2 for x, y in examples) + + # Compute the regularization term + reg_term = lambda_hp * (w0 ** 2 + w1 ** 2) + + # Compute the total cost function + cost = sse + reg_term + + # Compute the partial derivatives of the cost function with respect to w0 and w1 + d_w0 = 2 * sum(w0 + w1 * x - y for x, y in examples) + 2 * lambda_hp * w0 + d_w1 = 2 * sum((w0 + w1 * x - y) * x for x, y in examples) + 2 * lambda_hp * w1 + + # Update w0 and w1 using gradient descent + w0_new = w0 - learning_rate * d_w0 + w1_new = w1 - learning_rate * d_w1 + + # Check for convergence + if abs(w0_new - w0) < tol and abs(w1_new - w1) < tol: + break + + w0, w1 = w0_new, w1_new + + return w0, w1 + + +def test_error(w0, w1): + n = 100 + xs = [i/n for i in range(-n, n + 1)] + return sum((w0 + w1 * x - f(x)) ** 2 for x in xs) / len(xs) + +def main(): + print("Starting...") + no_reg_errors=[] + reg_erros = [] + exp_size = 1000 + for _ in range(exp_size): + + train_examples = generate_training_examples(n=2) + + w0, w1 = fit_without_reg(train_examples) + w0_r, w1_r = fit_with_reg(train_examples, lambda_hp=1) + + no_reg_errors.append(test_error(w0, w1)) + reg_erros.append(test_error(w0_r, w1_r)) + + print(f"Avg loss, regularization: {sum(no_reg_errors)/(len(no_reg_errors))}") + print(f"Avg loss, no regularization: {sum(reg_erros)/(len(reg_erros))}") + +if __name__=="__main__": + main() \ No newline at end of file diff --git a/main.aux b/main.aux index 2fb4e7f..436cd99 100644 --- a/main.aux +++ b/main.aux @@ -20,4 +20,6 @@ \@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 } \@writefile{toc}{\contentsline {subsection}{\numberline {4.2}Gradient Descent}{15}{}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {5}Homework 5}{16}{}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1}Regularization}{16}{}\protected@file@percent } \gdef \@abspage@last{17} diff --git a/main.fdb_latexmk b/main.fdb_latexmk index d9c1851..341f2ae 100644 --- a/main.fdb_latexmk +++ b/main.fdb_latexmk @@ -1,8 +1,8 @@ # Fdb version 4 -["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 "" +["pdflatex"] 1715927540.38441 "d:/uzair/projects/sfsu/csc-665-homework/main.tex" "main.pdf" "main" 1715927541.49816 0 + "C:/Users/uzair/AppData/Local/MiKTeX/fonts/map/pdftex/pdftex.map" 1715922414 80909 eab91d9745dd2edfd62a31d53cd5fe15 "" + "C:/Users/uzair/AppData/Local/MiKTeX/fonts/pk/ljfour/jknappen/ec/dpi600/tcrm1000.pk" 1715721639 11548 a903b62e24e4c2fe5a80812a4a494d70 "" + "C:/Users/uzair/AppData/Local/MiKTeX/miktex/data/le/pdftex/pdflatex.fmt" 1715720603 24240354 70429886933c6c598c1dc6fc281f7cf2 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/fonts/tfm/jknappen/ec/tcrm1000.tfm" 993062508 1436 c7f957a372ef2fbe93c0982f96625e12 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/fonts/tfm/public/amsfonts/cmextra/cmex7.tfm" 1233951848 1004 54797486969f23fa377b128694d548df "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/fonts/tfm/public/amsfonts/symbols/msam10.tfm" 1233951854 916 f87d7c45f9c908e672703b83b72241a3 "" @@ -44,25 +44,24 @@ "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/cancel/cancel.sty" 1388370247 7592 dd751af313a16a0308545d5bfd7aaaa2 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/epstopdf-pkg/epstopdf-base.sty" 1623003186 13886 d1306dcf79a944f6988e688c1785f9ce "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/graphics-cfg/graphics.cfg" 1465894292 1224 978390e9c2234eab29404bc21b268d1e "" - "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/graphics-def/pdftex.def" 1663918690 19448 1e988b341dda20961a6b931bcde55519 "" + "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/graphics-def/pdftex.def" 1713599642 19440 9da9dcbb27470349a580fca7372d454b "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/graphics/graphics.sty" 1665067579 18387 8f900a490197ebaf93c02ae9476d4b09 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/graphics/graphicx.sty" 1665067579 8010 a8d949cbdbc5c983593827c9eec252e1 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/graphics/keyval.sty" 1665067579 2671 7e67d78d9b88c845599a85b2d41f2e39 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/graphics/trig.sty" 1665067579 4023 293ea1c16429fc0c4cf605f4da1791a9 "" - "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/l3backend/l3backend-pdftex.def" 1710412547 29918 d08e4795c0778dfb000e82d9225c6ce2 "" + "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/l3backend/l3backend-pdftex.def" 1714423267 29785 470380cd55470b90fdb46d40220ba304 "" "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" 1714460101 23856 14667251c3432ebf46f977146e5a21e8 "" - "hw2/gameTree.png" 1712556571 51736 b13904e696801b947e455a901505f683 "" - "hw3/bayesian_network_1_hw3.png" 1712533911 42320 08df33c7ebceab0a98b148f3e43f6efe "" - "hw3/motorcade.png" 1712535186 144191 cdb52bfe315df9dd25d1d06a6414fa5c "" - "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 "" + "d:/uzair/projects/sfsu/csc-665-homework/main.tex" 1715927539 25340 6bbd1cec5abb985a0a933a737359bbb0 "" + "hw2/gameTree.png" 1715628443 51736 b13904e696801b947e455a901505f683 "" + "hw3/bayesian_network_1_hw3.png" 1715628443 42320 08df33c7ebceab0a98b148f3e43f6efe "" + "hw3/motorcade.png" 1715628443 144191 cdb52bfe315df9dd25d1d06a6414fa5c "" + "hw4/Picture1.png" 1715628443 5619 c6261e04afc55b58bedd9e8fe858bb3b "" + "main.aux" 1715927541 2358 1cb49ec657da2fa194a2062e36b8235c "pdflatex" + "main.tex" 1715927539 25340 6bbd1cec5abb985a0a933a737359bbb0 "" + "titlePage.aux" 1715927540 456 c979711f53deacf2b09031977e556db8 "pdflatex" + "titlePage.tex" 1715628444 407 ccdbd50244b3194dbad72dd1c7995bf0 "" (generated) "main.aux" "main.log" diff --git a/main.fls b/main.fls index 07f4576..f5f007d 100644 --- a/main.fls +++ b/main.fls @@ -112,13 +112,6 @@ 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 diff --git a/main.log b/main.log index 9aae049..6f4c305 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) 29 APR 2024 23:55 +This is pdfTeX, Version 3.141592653-2.6-1.40.26 (MiKTeX 24.4) (preloaded format=pdflatex 2024.5.14) 16 MAY 2024 23:32 entering extended mode restricted \write18 enabled. file:line:error style messages enabled. @@ -6,20 +6,20 @@ entering extended mode **d:/uzair/projects/sfsu/csc-665-homework/main.tex (d:/uzair/projects/sfsu/csc-665-homework/main.tex LaTeX2e <2023-11-01> patch level 1 -L3 programming layer <2024-03-14> +L3 programming layer <2024-04-11> (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/base\article.cls Document Class: article 2023/05/17 v1.4n Standard LaTeX document class (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/base\size10.clo File: size10.clo 2023/05/17 v1.4n Standard LaTeX file (size option) ) -\c@part=\count188 -\c@section=\count189 -\c@subsection=\count190 -\c@subsubsection=\count191 -\c@paragraph=\count192 -\c@subparagraph=\count193 -\c@figure=\count194 -\c@table=\count195 +\c@part=\count189 +\c@section=\count190 +\c@subsection=\count191 +\c@subsubsection=\count192 +\c@paragraph=\count193 +\c@subparagraph=\count194 +\c@figure=\count195 +\c@table=\count196 \abovecaptionskip=\skip48 \belowcaptionskip=\skip49 \bibindent=\dimen140 @@ -40,14 +40,14 @@ Package: amsbsy 1999/11/29 v1.2d Bold Symbols ) (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/amsmath\amsopn.sty Package: amsopn 2022/04/08 v2.04 operator names ) -\inf@bad=\count196 +\inf@bad=\count197 LaTeX Info: Redefining \frac on input line 234. -\uproot@=\count197 -\leftroot@=\count198 +\uproot@=\count198 +\leftroot@=\count199 LaTeX Info: Redefining \overline on input line 399. LaTeX Info: Redefining \colon on input line 410. -\classnum@=\count199 -\DOTSCASE@=\count266 +\classnum@=\count266 +\DOTSCASE@=\count267 LaTeX Info: Redefining \ldots on input line 496. LaTeX Info: Redefining \dots on input line 499. LaTeX Info: Redefining \cdots on input line 620. @@ -60,20 +60,20 @@ LaTeX Info: Redefining \Bigg on input line 725. \big@size=\dimen143 LaTeX Font Info: Redeclaring font encoding OML on input line 743. LaTeX Font Info: Redeclaring font encoding OMS on input line 744. -\macc@depth=\count267 +\macc@depth=\count268 LaTeX Info: Redefining \bmod on input line 905. LaTeX Info: Redefining \pmod on input line 910. LaTeX Info: Redefining \smash on input line 940. LaTeX Info: Redefining \relbar on input line 970. LaTeX Info: Redefining \Relbar on input line 971. -\c@MaxMatrixCols=\count268 +\c@MaxMatrixCols=\count269 \dotsspace@=\muskip16 -\c@parentequation=\count269 -\dspbrk@lvl=\count270 +\c@parentequation=\count270 +\dspbrk@lvl=\count271 \tag@help=\toks18 -\row@=\count271 -\column@=\count272 -\maxfields@=\count273 +\row@=\count272 +\column@=\count273 +\maxfields@=\count274 \andhelp@=\toks19 \eqnshift@=\dimen144 \alignsep@=\dimen145 @@ -94,20 +94,20 @@ Package: keyval 2022/05/29 v1.15 key=value parser (DPC) \KV@toks@=\toks22 ) (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/tools\calc.sty Package: calc 2023/07/08 v4.3 Infix arithmetic (KKT,FJ) -\calc@Acount=\count274 -\calc@Bcount=\count275 +\calc@Acount=\count275 +\calc@Bcount=\count276 \calc@Adimen=\dimen150 \calc@Bdimen=\dimen151 \calc@Askip=\skip53 \calc@Bskip=\skip54 LaTeX Info: Redefining \setlength on input line 80. LaTeX Info: Redefining \addtolength on input line 81. -\calc@Ccount=\count276 +\calc@Ccount=\count277 \calc@Cskip=\skip55 ) (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/mathtools\mhsetup.sty Package: mhsetup 2021/03/18 v1.4 programming setup (MH) ) -\g_MT_multlinerow_int=\count277 +\g_MT_multlinerow_int=\count278 \l_MT_multwidth_dim=\dimen152 \origjot=\skip56 \l_MT_shortvdotswithinadjustabove_dim=\dimen153 @@ -130,8 +130,8 @@ LaTeX Font Info: Redeclaring math symbol \hbar on input line 98. LaTeX Font Info: Overwriting math alphabet `\mathfrak' in version `bold' (Font) U/euf/m/n --> U/euf/b/n on input line 106. )) (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/l3backend\l3backend-pdftex.def -File: l3backend-pdftex.def 2024-03-14 L3 backend support: PDF output (pdfTeX) -\l__color_backend_stack_int=\count278 +File: l3backend-pdftex.def 2024-04-11 L3 backend support: PDF output (pdfTeX) +\l__color_backend_stack_int=\count279 \l__pdf_internal_box=\box54 ) (main.aux (titlePage.aux)) \openout1 = `main.aux'. @@ -161,19 +161,19 @@ File: graphics.cfg 2016/06/04 v1.11 sample graphics configuration ) Package graphics Info: Driver file: pdftex.def on input line 107. (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/graphics-def\pdftex.def -File: pdftex.def 2022/09/22 v1.2b Graphics/color driver for pdftex +File: pdftex.def 2024/04/13 v1.2c Graphics/color driver for pdftex (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/context/base/mkii\supp-pdf.mkii [Loading MPS to PDF converter (version 2006.09.02).] -\scratchcounter=\count279 +\scratchcounter=\count280 \scratchdimen=\dimen160 \scratchbox=\box55 -\nofMPsegments=\count280 -\nofMParguments=\count281 +\nofMPsegments=\count281 +\nofMParguments=\count282 \everyMPshowfont=\toks23 -\MPscratchCnt=\count282 +\MPscratchCnt=\count283 \MPscratchDim=\dimen161 -\MPnumerator=\count283 -\makeMPintoPDFobject=\count284 +\MPnumerator=\count284 +\makeMPintoPDFobject=\count285 \everyMPtoPDFconversion=\toks24 ))) (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/epstopdf-pkg\epstopdf-base.sty Package: epstopdf-base 2020-01-24 v2.11 Base part for package epstopdf @@ -238,39 +238,34 @@ Underfull \hbox (badness 10000) in paragraph at lines 450--451 [] [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)) + [15 <./hw4/Picture1.png>] +Overfull \hbox (2.23077pt too wide) detected at line 531 +[][] + [] + +[16] (main.aux (titlePage.aux)) *********** LaTeX2e <2023-11-01> patch level 1 -L3 programming layer <2024-03-14> +L3 programming layer <2024-04-11> *********** ) Here is how much of TeX's memory you used: - 3263 strings out of 474424 - 51441 string characters out of 5741740 - 1940496 words of memory out of 5000000 - 25593 multiletter control sequences out of 15000+600000 + 3256 strings out of 474370 + 51092 string characters out of 5740535 + 1940754 words of memory out of 5000000 + 25640 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 + 63i,19n,70p,462b,279s stack positions out of 10000i,1000n,20000p,200000b,200000s -Output written on main.pdf (17 pages, 554351 bytes). +Output written on main.pdf (17 pages, 393219 bytes). PDF statistics: - 133 PDF objects out of 1000 (max. 8388607) + 131 PDF objects out of 1000 (max. 8388607) 0 named destinations out of 1000 (max. 500000) - 26 words of extra memory for PDF output out of 10000 (max. 10000000) + 21 words of extra memory for PDF output out of 10000 (max. 10000000) diff --git a/main.pdf b/main.pdf index 235d4a7..13ba962 100644 Binary files a/main.pdf and b/main.pdf differ diff --git a/main.synctex.gz b/main.synctex.gz index aa20181..8fe8fb5 100644 Binary files a/main.synctex.gz and b/main.synctex.gz differ diff --git a/main.tex b/main.tex index 5d788d7..c116e60 100644 --- a/main.tex +++ b/main.tex @@ -422,52 +422,52 @@ See attached submission.py file \subsection{Bayesian Networks} \begin{itemize} \item[a.] Given the following Bayesian network, write down a factorization of the joint probability distribution P(A,B, C,D, E) as a product of local conditional distributions, one for each random variable. - \begin{center} - \includegraphics[scale=0.3]{hw3/bayesian_network_1_hw3.png} - \end{center} + \begin{center} + \includegraphics[scale=0.3]{hw3/bayesian_network_1_hw3.png} + \end{center} - $$ - P(A, B, C, D, E) = \ \boxed{P(A) \cdot P(B) \cdot P(C | A, B) \cdot P(D | B) \cdot P(E | C)} - $$ + $$ + P(A, B, C, D, E) = \ \boxed{P(A) \cdot P(B) \cdot P(C | A, B) \cdot P(D | B) \cdot P(E | C)} + $$ \item[b.] For the same Bayesian network above, list all the random variables that are independent of A, assuming that none of the variables have been observed. - \begin{itemize} - \item B - \end{itemize} + \begin{itemize} + \item B + \end{itemize} \item[c.] For the same Bayesian network above, assume that E is now observed. List all pairs of random variables (not including E) that are conditionally independent given E. - \begin{itemize} - \item (A, B) - \item (A, D) - \item (B, D) - \end{itemize} + \begin{itemize} + \item (A, B) + \item (A, D) + \item (B, D) + \end{itemize} \item[d.] Consider the following model for traffic jams in a small town, which can be caused by either a car accident or by a visit from the president (and the accompanying security motorcade). - \begin{center} - \includegraphics[scale=0.3]{hw3/motorcade.png} - \end{center} + \begin{center} + \includegraphics[scale=0.3]{hw3/motorcade.png} + \end{center} - Compute P(Accident = 1 | Traffic = 1) and P(Accident = 1 | Traffic = 1, President = 1). \\ + Compute P(Accident = 1 | Traffic = 1) and P(Accident = 1 | Traffic = 1, President = 1). \\ - Let: - \begin{itemize} - \item A = event of accident - \item P = event of president's visit - \item T = event of traffic jam - \end{itemize} + Let: + \begin{itemize} + \item A = event of accident + \item P = event of president's visit + \item T = event of traffic jam + \end{itemize} - We're given: - \begin{itemize} - \item P(A) = prior probability of an accident - \item P(P) = prior probability of the president's visit - \item P(T | A) = probability of a traffic jam given an accident - \item P(T | P) = probability of a traffic jam given the president's visit - \end{itemize} + We're given: + \begin{itemize} + \item P(A) = prior probability of an accident + \item P(P) = prior probability of the president's visit + \item P(T | A) = probability of a traffic jam given an accident + \item P(T | P) = probability of a traffic jam given the president's visit + \end{itemize} + + $$ + P(\text{Accident} = 1 | \text{Traffic} = 1) \rightarrow P(A | T) = \frac{P(T | A) \cdot P(A)}{P(T)} + $$ - $$ - P(\text{Accident} = 1 | \text{Traffic} = 1) \rightarrow P(A | T) = \frac{P(T | A) \cdot P(A)}{P(T)} - $$ - \end{itemize} \newpage @@ -479,40 +479,79 @@ See attached submission.py file \begin{itemize} \item[a - c. ] Refer to bn.py - \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. + \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} + \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. + 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 - $$ + \item[b. ] - $$ - \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} - $$ + $$ + \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} + + \begin{center} + \includegraphics[scale=0.3]{hw4/Picture1.png} + \end{center} \end{itemize} -\end{document} \ No newline at end of file +\newpage + +\section{Homework 5} + +\subsection{Regularization} + +\begin{itemize} + \item[a.] + $$ + \begin{aligned} + \frac{d}{dw_0} C(w) & = \sum_{i=1}^{n} 2(y_i - (w_0 + w_1x_i))(-1) = \boxed{-2\sum_{i=1}^{n} (y_i - (w_0 + w_1x_i))} \\ + \frac{d}{dw_1} C(w) & = \sum_{i=1}^{n} 2(y_i - (w_0 + w_1x_i))(-x_i) = \boxed{-2\sum_{i=1}^{n} x_i(y_i - (w_0 + w_1x_i))} + \end{aligned} + $$ + + \item[b. ] + $$ + \begin{aligned} + \frac{d}{dw_0} \tilde{C}(w) &= \sum_{i=1}^{n} 2(y_i - (w_0 + w_1x_i))(-1) + 2\lambda w_0 \\ + &= 2\sum_{i=1}^{n} (w_0 + w_1x_i - y_i) + 2\lambda w_0 ] \\ + &= \boxed{2\sum_{i=1}^{n} (w_0 + w_1x_i - y_i + \lambda w_0)} + \end{aligned} + $$ + + and + + $$ + \begin{aligned} + \frac{d}{dw_1} \tilde{C}(w) &= \sum_{i=1}^{n} 2(y_i - (w_0 + w_1x_i))(-x_i) + 2\lambda w_1 \\ + &= \boxed{2\sum_{i=1}^{n} x_i(y_i - (w_0 + w_1x_i)) + 2\lambda w_1} + \end{aligned} + $$ + + \item[c-d.] Refer to regularization.py + \item[e. ] Avg loss, regularization: 1.715104320688656 \\ + Avg loss, no regularization: 0.4384015750703455 + +\end{itemize} + +\end{document}