You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 6a6ede9
Browse filesBrowse the repository at this point in the historyBrowse files
پیش از شروع این درس، باید جلسه ی قبل را مطالعه کرده باشید.
24
24
25
25
## هدف:رسم یک فرآیند نویز سفید
26
-
27
-
فرض کنید می خواهیم یک فرآیند نویز سفید به صورت <mathxmlns="http://www.w3.org/1998/Math/MathML">
28
-
<msub>
29
-
<mi>ϵ</mi>
30
-
<mn>0</mn>
31
-
</msub>
32
-
<mo>,</mo>
33
-
<msub>
34
-
<mi>ϵ</mi>
35
-
<mn>1</mn>
36
-
</msub>
37
-
<mo>,</mo>
38
-
<mo>…</mo>
39
-
<mo>,</mo>
40
-
<msub>
41
-
<mi>ϵ</mi>
42
-
<mi>T</mi>
43
-
</msub>
44
-
</math> ، شبیه سازی و رسم کنیم، که در آن هر <mathxmlns="http://www.w3.org/1998/Math/MathML">
45
-
<msub>
46
-
<mi>ϵ</mi>
47
-
<mi>t</mi>
48
-
</msub>
49
-
</math> یک مقدار مستقل و از توزیع نرمال استاندارد است.
26
+
27
+
فرض کنید میخواهیم فرآیند نویز سفید $\epsilon_0, \epsilon_1, \ldots, \epsilon_T$ را شبیهسازی و رسم کنیم، که در آن هر نقطه $\epsilon_t$ مستقل و نرمال استاندارد است.
50
28
51
29
به بیان دیگر، می خواهیم نمودارهایی همانند نمودار زیر تولید کنیم:
(در اینجا <mathxmlns="http://www.w3.org/1998/Math/MathML">
56
-
<mi>t</mi>
57
-
</math> محور افقی و <mathxmlns="http://www.w3.org/1998/Math/MathML">
58
-
<msub>
59
-
<mi>ϵ</mi>
60
-
<mi>t</mi>
61
-
</msub>
62
-
</math> محور عمودی است.)
37
+
(در اینجا $t$ روی محور افقی و $\epsilon_t$ روی محور عمودی است.)
63
38
64
39
ما این کار را به چند روش مختلف انجام خواهیم داد و هر بار نکات بیشتری درباره ی پایتون خواهیم آموخت.
65
40
66
41
## روش اول
67
42
43
+
(ourfirstprog)=
68
44
در ادامه چند خط کد آمده که کاری را که تعریف کرده بودیم، انجام می دهد.
69
45
70
46
```{code-cell} ipython
@@ -77,6 +53,7 @@ plt.show()
77
53
```
78
54
بیایید این برنامه را تجزیه و تحلیل کنیم و ببینیم چگونه کار میکند.
79
55
56
+
(import)=
80
57
### Imports
81
58
82
59
دو خط اول کد، قابلیت هایی را از کتابخانه های خارجی وارد می کند.
@@ -194,6 +171,7 @@ plt.show()
194
171
195
172
### یک نسخه با حلقه تکرار For
196
173
174
+
(firstloopprog)=
197
175
این نسخه حلقه های `For` و لیست های پایتون را نشان می دهد.
198
176
199
177
```{code-cell} python3
@@ -217,18 +195,14 @@ plt.show()
217
195
218
196
* عبارت `# empty list` یک توضیح (کامنت) است و توسط مفسر پایتون نادیده گرفته می شود.
219
197
220
-
* سه خط بعدی حلقه `For` هستند که به طور پیوسته یک عدد تصادفی جدید <mathxmlns="http://www.w3.org/1998/Math/MathML">
221
-
<msub>
222
-
<mi>ϵ</mi>
223
-
<mi>t</mi>
224
-
</msub>
225
-
</math> تولید می کنند و آنها را به انتهای لیست `ϵ_values` اضافه می کنند.
198
+
* سه خط بعدی حلقه `for` هستند، که به طور مکرر یک عدد تصادفی جدید $\epsilon_t$ میکشد و آن را به انتهای لیست `ϵ_values` اضافه میکند.
226
199
227
200
* دو خط آخر نمودار را تولید کرده و به کاربر نمایش می دهند.
228
201
229
202
230
203
بیایید برخی از بخش های این کد را با جزئیات بیشتری بررسی کنیم.
231
204
205
+
(lists_ref)=
232
206
### لیست ها
233
207
234
208
دستور `ϵ_values = []` را در نظر بگیرید، این دستور یک لیست خالی ایجاد می کند.
@@ -355,7 +329,7 @@ for variable_name in sequence:
355
329
356
330
* استاندارد پایتون برای تورفتگی، 4 فاصله (space) است و شما هم باید از همین مقدار استقاده کنید.
357
331
358
-
332
+
(whileloopprog)=
359
333
### حلقه های While
360
334
361
335
حلقه `for` رایج ترین تکنیک برای تکرار در پایتون است.
@@ -396,56 +370,11 @@ i == ts_length #the ending condition for the while loop
396
370
397
371
در این بازه زمانی هیچ برداشتی انجام نشده و تاریخ پایان دوره را با $T$ نشان می دهیم.
موجوی حساب از دوره $t$ به <mathxmlns="http://www.w3.org/1998/Math/MathML">
در کد زیر دنباله ی <mathxmlns="http://www.w3.org/1998/Math/MathML">
432
-
<msub>
433
-
<mi>b</mi>
434
-
<mn>0</mn>
435
-
</msub>
436
-
<mo>,</mo>
437
-
<msub>
438
-
<mi>b</mi>
439
-
<mn>1</mn>
440
-
</msub>
441
-
<mo>,</mo>
442
-
<mo>…</mo>
443
-
<mo>,</mo>
444
-
<msub>
445
-
<mi>b</mi>
446
-
<mi>T</mi>
447
-
</msub>
448
-
</math> را تولید و رسم می کنیم.
373
+
موجودی اولیه $b_0$ و نرخ بهره $r$ است.
374
+
375
+
موجودی از دوره $t$ به $t+1$ طبق $b_{t+1} = (1 + r) b_t$ بهروزرسانی میشود.
376
+
377
+
در کد زیر، دنباله $b_0, b_1, \ldots, b_T$ را تولید و رسم میکنیم.
449
378
450
379
برای ذخیره این دنباله، به جای استفاده از یک لیست پایتون، از یک آرایه NumPy استفاده خواهیم کرد.
451
380
@@ -489,14 +418,7 @@ x_0 = 0
489
418
\quad \text{and} \quad t = 0,\ldots,T
490
419
$$
491
420
492
-
فرض می شود که دنباله ی شوک ها <mathxmlns="http://www.w3.org/1998/Math/MathML">
493
-
<mofence="false"stretchy="false">{</mo>
494
-
<msub>
495
-
<mi>ϵ</mi>
496
-
<mi>t</mi>
497
-
</msub>
498
-
<mofence="false"stretchy="false">}</mo>
499
-
</math> مستقل و با توزیع یکسان(IID)، و دارای توزیع نرمال استاندارد باشد.
421
+
فرض میشود دنباله شوکهای $\{\epsilon_t\}$ IID و نرمال استاندارد باشد.
500
422
501
423
در راه حل خود، دستورات import را تنها به موارد زیر محدود و تنها از کتابخانه های زیر استفاده کنید
502
424
@@ -595,14 +517,7 @@ $$
595
517
596
518
همانند قبل از $T=200$، $\alpha = 0.9$ و $\{\epsilon_t\}$ استفاده کنید.
597
519
598
-
به صورت آنلاین جست و جو کنید و یک تابع پیدا کنید که بتوان برای محاسبه قدرمطلق <math xmlns="http://www.w3.org/1998/Math/MathML">
599
-
<mo stretchy="false">|</mo>
600
-
<msub>
601
-
<mi>x</mi>
602
-
<mi>t</mi>
603
-
</msub>
604
-
<mo stretchy="false">|</mo>
605
-
</math> از آن استفاده کرد.
520
+
آنلاین برای تابعی که میتواند برای محاسبه قدر مطلق $|x_t|$ استفاده شود جستجو کنید.
606
521
```
607
522
608
523
```{exercise-end}
@@ -709,9 +624,7 @@ plt.show()
709
624
710
625
این تمرین کمی سخت تر است و نیاز به فکر و برنامه ریزی دارد.
711
626
712
-
تکلیف شما این است که با استفاده از روش [مونته کارلو](https://en.wikipedia.org/wiki/Monte_Carlo_method)، یک تقریب برای عدد <mathxmlns="http://www.w3.org/1998/Math/MathML">
713
-
<mi>π</mi>
714
-
</math> محاسبه کنید.
627
+
وظیفه محاسبه تقریبی برای $\pi$ با استفاده از روش [مونته کارلو](https://en.wikipedia.org/wiki/Monte_Carlo_method) است.
715
628
716
629
به جز موارد زیر، از هیچ کتابخانه ای استفاده نکنید:
717
630
@@ -724,39 +637,9 @@ import numpy as np
724
637
725
638
راهنماهای شما به شرح زیر است:
726
639
727
-
* اگر $U$ یک متغیرتصادفی دوبعدی یکنواخت روی مربع واحد <math xmlns="http://www.w3.org/1998/Math/MathML">
728
-
<mo stretchy="false">(</mo>
729
-
<mn>0</mn>
730
-
<mo>,</mo>
731
-
<mn>1</mn>
732
-
<msup>
733
-
<mo stretchy="false">)</mo>
734
-
<mn>2</mn>
735
-
</msup>
736
-
</math> باشد، آنگاه احتمال اینکه $U$ در زیرمجموعه ای $B$ از <math xmlns="http://www.w3.org/1998/Math/MathML">
737
-
<mo stretchy="false">(</mo>
738
-
<mn>0</mn>
739
-
<mo>,</mo>
740
-
<mn>1</mn>
741
-
<msup>
742
-
<mo stretchy="false">)</mo>
743
-
<mn>2</mn>
744
-
</msup>
745
-
</math> قرار گیرد، برابر با مساحت ناحیه ی $B$ است.
746
-
747
-
* اگر <math xmlns="http://www.w3.org/1998/Math/MathML">
748
-
<msub>
749
-
<mi>U</mi>
750
-
<mn>1</mn>
751
-
</msub>
752
-
<mo>,</mo>
753
-
<mo>…</mo>
754
-
<mo>,</mo>
755
-
<msub>
756
-
<mi>U</mi>
757
-
<mi>n</mi>
758
-
</msub>
759
-
</math> نسخه های مستقل و با توزیع یکسان از $U$ باشند، آنگاه با بزرگ شدن $n$، نسبت نقاطی که در ناحیه $B$ قرار میگیرند، به احتمال قرارگرفتن در $B$ همگرا می شوند.
640
+
* اگر $U$ یک متغیر تصادفی یکنواخت دوبعدی روی مربع واحد $(0, 1)^2$ باشد، احتمال اینکه $U$ در زیرمجموعه $B$ از $(0,1)^2$ قرار بگیرد برابر با مساحت $B$ است.
641
+
642
+
* اگر $U_1,\ldots,U_n$ کپیهای IID از $U$ باشند، با بزرگ شدن $n$، کسری که در $B$ قرار میگیرد، به احتمال قرار گرفتن در $B$ همگرا میشود.
</math> مساحت آن باشد و <mathxmlns="http://www.w3.org/1998/Math/MathML">
778
-
<mi>r</mi>
779
-
<mo>=</mo>
780
-
<mn>1</mn>
781
-
<mrowdata-mjx-texclass="ORD">
782
-
<mo>/</mo>
783
-
</mrow>
784
-
<mn>2</mn>
785
-
</math> شعاع آن است.
786
-
787
-
اگر مقدار <mathxmlns="http://www.w3.org/1998/Math/MathML">
788
-
<mi>π</mi>
789
-
</math> را بدانیم، میتوانیم مساحت <mathxmlns="http://www.w3.org/1998/Math/MathML">
790
-
<mi>A</mi>
791
-
</math> را از طریق رابطه <mathxmlns="http://www.w3.org/1998/Math/MathML">
792
-
<mi>A</mi>
793
-
<mo>=</mo>
794
-
<mi>π</mi>
795
-
<msup>
796
-
<mi>r</mi>
797
-
<mn>2</mn>
798
-
</msup>
799
-
</math> محاسبه کنیم.
800
-
801
-
اما در اینجا هدف محاسبه ی <mathxmlns="http://www.w3.org/1998/Math/MathML">
802
-
<mi>π</mi>
803
-
</math> است، که میتوانیم از رابطه <mathxmlns="http://www.w3.org/1998/Math/MathML">
804
-
<mi>π</mi>
805
-
<mo>=</mo>
806
-
<mi>A</mi>
807
-
<mrowdata-mjx-texclass="ORD">
808
-
<mo>/</mo>
809
-
</mrow>
810
-
<msup>
811
-
<mi>r</mi>
812
-
<mn>2</mn>
813
-
</msup>
814
-
</math> به دست آوریم.
815
-
816
-
خلاصه: اگر بتوانیم مساحت دایره ای با قطر 1 را تخمین بزنیم، آنگاه تقسیم کردن آن بر <mathxmlns="http://www.w3.org/1998/Math/MathML">
817
-
<msup>
818
-
<mi>r</mi>
819
-
<mn>2</mn>
820
-
</msup>
821
-
<mo>=</mo>
822
-
<mostretchy="false">(</mo>
823
-
<mn>1</mn>
824
-
<mrowdata-mjx-texclass="ORD">
825
-
<mo>/</mo>
826
-
</mrow>
827
-
<mn>2</mn>
828
-
<msup>
829
-
<mo stretchy="false">)</mo>
830
-
<mn>2</mn>
831
-
</msup>
832
-
<mo>=</mo>
833
-
<mn>1</mn>
834
-
<mrowdata-mjx-texclass="ORD">
835
-
<mo>/</mo>
836
-
</mrow>
837
-
<mn>4</mn>
838
-
</math> تخمینی برای <mathxmlns="http://www.w3.org/1998/Math/MathML">
839
-
<mi>π</mi>
840
-
</math> به ما می دهد.
656
+
دایرهای با قطر یک را درون یک مربع درنظر بگیرید.
657
+
658
+
فرض کنید $A$ مساحت آن و $r=1/2$ شعاع آن باشد.
659
+
660
+
اگر $\pi$ را بدانیم، میتوانیم $A$ را از طریق $A = \pi r^2$ محاسبه کنیم.
661
+
662
+
اما در اینجا نکته محاسبه $\pi$ است که میتوانیم با $\pi = A / r^2$ انجام دهیم.
663
+
664
+
خلاصه: اگر بتوانیم مساحت دایرهای با قطر 1 را تخمین بزنیم، تقسیم بر $r^2 = (1/2)^2 = 1/4$ تخمینی از $\pi$ میدهد.
841
665
842
666
ما این مساحت را با نمونه گیری از توزیع یکواخت دومتغیره و بررسی کسری از نقاط که درون دایره می افتند، تخمین میزنیم:
0 commit comments