karagoz_data <- rnorm(n, mu, sd)
mean_karagoz <- mean(karagoz_data)
variance_karagoz <- var(karagoz_data)
sd_karagoz <- sd(karagoz_data)
median_karagoz <- median(karagoz_data)
## Seed number: 2406
## Sample size (n): 1745
## Initial mean (mu): 9.869444
## Initial standard deviation (sd): 4.10746
## Mean of karagoz_data: 9.914283
## Variance of karagoz_data: 16.92875
## Standard deviation of karagoz_data: 4.114456
## Median of karagoz_data: 9.926196

sd_2 <- sd * 2.5
karagoz_data_2 <- rnorm(n, mu, sd_2)
hist(karagoz_data_2, 
     main = "Histogram of karagoz_data_2", 
     xlab = "Values", 
     ylab = "Frequency", 
     col = rgb(1, 0, 0, alpha = 0.3),  # Şeffaf kırmızı
     border = "grey")

## Warning in transformation$transform(x): NaNs produced
## Warning in scale_y_continuous(trans = "log10"): log-10 transformation
## introduced infinite values.
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_line()`).

## Warning in transformation$transform(x): NaNs produced
## Warning in scale_y_continuous(trans = "log10"): log-10 transformation
## introduced infinite values.
## Warning: Removed 14 rows containing missing values or values outside the scale range
## (`geom_line()`).

## Warning in transformation$transform(x): NaNs produced
## Warning in scale_y_continuous(trans = "log10"): log-10 transformation
## introduced infinite values.
## Warning: Removed 190 rows containing missing values or values outside the scale range
## (`geom_line()`).

## [1] "MSFT"
## [1] "MSFT" "GOOG"
##            MSFT.Close
## 1986-03-13   0.097222
## 1986-03-14   0.100694
## 1986-03-17   0.102431
## 1986-03-18   0.099826
## 1986-03-19   0.098090
## 1986-03-20   0.095486
## 1986-03-21   0.092882
## 1986-03-24   0.090278
## 1986-03-25   0.092014
## 1986-03-26   0.094618
##        ...           
## 2024-10-18 418.160004
## 2024-10-21 418.779999
## 2024-10-22 427.510010
## 2024-10-23 424.600006
## 2024-10-24 424.730011
## 2024-10-25 428.149994
## 2024-10-28 426.589996
## 2024-10-29 431.950012
## 2024-10-30 432.529999
## 2024-10-31 406.350006
##            GOOG.Close
## 2004-08-19   2.499133
## 2004-08-20   2.697639
## 2004-08-23   2.724787
## 2004-08-24   2.611960
## 2004-08-25   2.640104
## 2004-08-26   2.687676
## 2004-08-27   2.643840
## 2004-08-30   2.540727
## 2004-08-31   2.549693
## 2004-09-01   2.496891
##        ...           
## 2024-10-18 165.050003
## 2024-10-21 165.800003
## 2024-10-22 166.820007
## 2024-10-23 164.479996
## 2024-10-24 164.529999
## 2024-10-25 166.990005
## 2024-10-28 168.339996
## 2024-10-29 171.139999
## 2024-10-30 176.139999
## 2024-10-31 172.690002

##             MSFT.Open  MSFT.High   MSFT.Low MSFT.Close MSFT.Volume
## 1986-03-14   0.088542   0.102431   0.088542   0.100694  1339948800
## 1986-03-21   0.100694   0.103299   0.091146   0.092882   367257600
## 1986-03-27   0.092882   0.096354   0.089410   0.096354   136972800
## 1986-04-04   0.096354   0.098958   0.093750   0.096354   100598400
## 1986-04-11   0.096354   0.101563   0.092882   0.099826    70070400
## 1986-04-18   0.099826   0.105035   0.097222   0.101563    96998400
## 1986-04-25   0.101563   0.121962   0.098958   0.117188   202233600
## 1986-05-02   0.117188   0.118924   0.108507   0.110243   164707200
## 1986-05-09   0.110243   0.111979   0.108507   0.110243    27763200
## 1986-05-16   0.110243   0.114583   0.110243   0.111979    39369600
##        ...                                                        
## 2024-08-30 416.369995 422.049988 407.309998 417.140015    82881900
## 2024-09-06 417.910004 419.880005 400.799988 401.700012    69254400
## 2024-09-13 407.239990 431.829987 402.149994 430.589996    87449700
## 2024-09-20 430.600006 441.850006 428.220001 435.269989   128480600
## 2024-09-27 434.279999 436.459991 426.100006 428.019989    74929200
## 2024-10-04 428.209991 430.420013 414.290009 416.059998    85338600
## 2024-10-11 416.000000 420.380005 408.170013 416.320007    83116700
## 2024-10-18 417.769989 424.040009 410.480011 418.160004    83027500
## 2024-10-25 416.119995 432.519989 413.750000 428.149994    89823400
## 2024-10-31 431.660004 438.500000 406.299988 406.350006   116157100
##            MSFT.Adjusted
## 1986-03-14    0.06208729
## 1986-03-21    0.05727049
## 1986-03-27    0.05941130
## 1986-04-04    0.05941130
## 1986-04-11    0.06155211
## 1986-04-18    0.06262314
## 1986-04-25    0.07225738
## 1986-05-02    0.06797516
## 1986-05-09    0.06797516
## 1986-05-16    0.06904556
##        ...              
## 2024-08-30  417.14001465
## 2024-09-06  401.70001221
## 2024-09-13  430.58999634
## 2024-09-20  435.26998901
## 2024-09-27  428.01998901
## 2024-10-04  416.05999756
## 2024-10-11  416.32000732
## 2024-10-18  418.16000366
## 2024-10-25  428.14999390
## 2024-10-31  406.35000610
##             GOOG.Open  GOOG.High   GOOG.Low GOOG.Close GOOG.Volume
## 2004-08-20   2.490664   2.716817   2.390042   2.697639  1356284704
## 2004-08-27   2.758411   2.826406   2.579581   2.643840  1125298143
## 2004-09-03   2.622171   2.627402   2.464263   2.490913   794237881
## 2004-09-10   2.515820   2.654052   2.480951   2.623417   474118476
## 2004-09-17   2.655795   2.926282   2.651561   2.926282   967075300
## 2004-09-24   2.912832   3.090914   2.908349   2.984563   866054062
## 2004-10-01   2.977838   3.362895   2.934003   3.302123  1675817921
## 2004-10-08   3.368624   3.483941   3.293655   3.430392  1337566807
## 2004-10-15   3.412210   3.623917   3.322546   3.589296  1317046178
## 2004-10-22   3.566631   4.487430   3.476967   4.294653  3169567955
##        ...                                                        
## 2024-08-30 168.154999 169.380005 161.981995 165.110001    76549800
## 2024-09-06 163.315002 163.380005 151.934998 152.130005    83082400
## 2024-09-13 153.630005 159.274994 148.199997 158.369995   105209000
## 2024-09-20 158.330002 164.990005 157.610001 164.639999   106889800
## 2024-09-27 165.339996 166.970001 162.029999 165.289993    79869200
## 2024-10-04 164.779999 170.440002 164.639999 168.559998    67871000
## 2024-10-11 169.139999 169.899994 161.119995 164.520004    69271500
## 2024-10-18 164.910004 169.089996 164.369995 165.050003    62984300
## 2024-10-25 164.580002 167.600006 162.770004 166.990005    63427700
## 2024-10-31 170.589996 183.789993 165.789993 172.690002   132212200
##            GOOG.Adjusted
## 2004-08-20      2.691030
## 2004-08-27      2.637363
## 2004-09-03      2.484811
## 2004-09-10      2.616990
## 2004-09-17      2.919113
## 2004-09-24      2.977252
## 2004-10-01      3.294034
## 2004-10-08      3.421988
## 2004-10-15      3.580503
## 2004-10-22      4.284132
##        ...              
## 2024-08-30    164.892944
## 2024-09-06    151.930008
## 2024-09-13    158.369995
## 2024-09-20    164.639999
## 2024-09-27    165.289993
## 2024-10-04    168.559998
## 2024-10-11    164.520004
## 2024-10-18    165.050003
## 2024-10-25    166.990005
## 2024-10-31    172.690002
##            MSFT.Close
## 1986-03-14   0.100694
## 1986-03-21   0.092882
## 1986-03-27   0.096354
## 1986-04-04   0.096354
## 1986-04-11   0.099826
## 1986-04-18   0.101563
## 1986-04-25   0.117188
## 1986-05-02   0.110243
## 1986-05-09   0.110243
## 1986-05-16   0.111979
##        ...           
## 2024-08-30 417.140015
## 2024-09-06 401.700012
## 2024-09-13 430.589996
## 2024-09-20 435.269989
## 2024-09-27 428.019989
## 2024-10-04 416.059998
## 2024-10-11 416.320007
## 2024-10-18 418.160004
## 2024-10-25 428.149994
## 2024-10-31 406.350006
##            GOOG.Close
## 2004-08-20   2.697639
## 2004-08-27   2.643840
## 2004-09-03   2.490913
## 2004-09-10   2.623417
## 2004-09-17   2.926282
## 2004-09-24   2.984563
## 2004-10-01   3.302123
## 2004-10-08   3.430392
## 2004-10-15   3.589296
## 2004-10-22   4.294653
##        ...           
## 2024-08-30 165.110001
## 2024-09-06 152.130005
## 2024-09-13 158.369995
## 2024-09-20 164.639999
## 2024-09-27 165.289993
## 2024-10-04 168.559998
## 2024-10-11 164.520004
## 2024-10-18 165.050003
## 2024-10-25 166.990005
## 2024-10-31 172.690002

##        max        min     range1     range2    average         sd         cv 
## 467.559998   0.092882   0.092882 467.559998  61.053136  94.844066   1.553468
##         max         min      range1      range2     average          sd 
## 191.9600067   2.4909129   2.4909129 191.9600067  46.8914693  45.2541468 
##          cv 
##   0.9650827

##                  [,1]
## MSFT.Close -0.0233091
##                   [,1]
## GOOG.Close -0.01405285
## [1] 0.8912318
##             [,1] [,2]
##  [1,] 0.02713557    0
##  [2,] 0.04046248    0
##  [3,] 0.05623570    0
##  [4,] 0.07433529    0
##  [5,] 0.09462383    0
##  [6,] 0.11694818    0
##  [7,] 0.14114138    0
##  [8,] 0.16702479    0
##  [9,] 0.19441028    0
## [10,] 0.22310251    0
## [11,] 0.25290132    0
## [12,] 0.28360401    0
## [13,] 0.31500767    0
## [14,] 0.34691142    0
## [15,] 0.37911853    0
## [16,] 0.41143838    0
## [17,] 0.44368834    0
## [18,] 0.47569531    0
## [19,] 0.50729723    0
## [20,] 0.53834426    0
## [21,] 0.56869970    0
## [22,] 0.59824082    0
## [23,] 0.62685928    0
## [24,] 0.65446147    0
## [25,] 0.68096854    0
## [26,] 0.70631624    0
## [27,] 0.73045463    0
## [28,] 0.75334753    0
## [29,] 0.77497185    0
## [30,] 0.79531686    0
## [31,] 0.81438324    0
## [32,] 0.83218211    0
## [33,] 0.84873401    0
## [34,] 0.86406782    0
## [35,] 0.87821966    0
## [36,] 0.89123181    0
## [37,] 0.90315161    0
## [38,] 0.91403047    0
## [39,] 0.92392286    0
## [40,] 0.93288537    0
## [41,] 0.94097590    0
## [42,] 0.94825284    0
## [43,] 0.95477440    0
## [44,] 0.96059797    0
## [45,] 0.96577961    0
## [46,] 0.97037358    0

## [1] 1 1 0 0
## [1] 1
## [1] 1
##   [1] 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1
##  [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [75] 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##   [1] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
##   [8] 1.0000000 0.8888889 0.9000000 0.9090909 0.9166667 0.9230769 0.9285714
##  [15] 0.9333333 0.9375000 0.9411765 0.9444444 0.9473684 0.9500000 0.9523810
##  [22] 0.9545455 0.9565217 0.9583333 0.9600000 0.9615385 0.9629630 0.9285714
##  [29] 0.9310345 0.9333333 0.9354839 0.9375000 0.9393939 0.9411765 0.9428571
##  [36] 0.9444444 0.9459459 0.9473684 0.9487179 0.9500000 0.9512195 0.9523810
##  [43] 0.9534884 0.9545455 0.9555556 0.9565217 0.9574468 0.9583333 0.9591837
##  [50] 0.9600000 0.9607843 0.9615385 0.9433962 0.9444444 0.9454545 0.9464286
##  [57] 0.9473684 0.9310345 0.9322034 0.9333333 0.9344262 0.9354839 0.9365079
##  [64] 0.9375000 0.9384615 0.9393939 0.9402985 0.9411765 0.9420290 0.9428571
##  [71] 0.9436620 0.9444444 0.9452055 0.9459459 0.9466667 0.9473684 0.9480519
##  [78] 0.9487179 0.9493671 0.9500000 0.9506173 0.9512195 0.9518072 0.9404762
##  [85] 0.9411765 0.9418605 0.9425287 0.9431818 0.9438202 0.9444444 0.9450549
##  [92] 0.9456522 0.9462366 0.9468085 0.9473684 0.9479167 0.9484536 0.9489796
##  [99] 0.9494949 0.9500000

##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [1] 1 0 0 0
## [1] 1 1 1 0
## [1] 0 1 0 0
## [1] 1 1 0 1
## [1] 1 1 0 1
## [1] 1 1 0 0
## [1] 1 1 1 1
## [1] 1 1 1 0
## [1] 1 0 1 1
## [1] 0 0 0 0
## [1] 1 1 0 1
## [1] 0 1 0 1
## [1] 0 1 1 1
## [1] 0 0 0 0
## [1] 0 0 0 0
## [1] 1 1 0 1
## [1] 0 1 1 1
## [1] 1 0 0 0
## [1] 1 0 0 0
## [1] 0 0 0 0
##   A   B   C 
## 0.0 0.2 0.8
##    A    B    C 
## 0.43 0.19 0.38
##     A     B     C 
## 0.428 0.184 0.388
##       A       B       C 
## 0.40047 0.20165 0.39788
## A B C 
## 1 0 0
##    A    B    C 
## 0.77 0.09 0.14
##     A     B     C 
## 0.802 0.088 0.110
##       A       B       C 
## 0.80094 0.09996 0.09910

From these two functions we can see that there is a difference.

This simulation can help analyze how changes in candidate support affect voting outcomes, understand voter behavior, and explore the effects of polarization in elections.

Shortened Interaction with AI:

Me: I need to conduct an experiment using R Markdown for my assignment. What are the basic steps? AI: The experiment involves asking 5 individuals about their political candidate choices, determined by specified probabilities (pA, pB, pC). Start by selecting these probabilities and simulating responses.

Me: How do I organize the code and outputs in the R Markdown document? AI: Write your code in chunks. Use echo=FALSE to hide the code in the HTML output if you want. Here’s an example chunk: #```{r candidate_choices, echo=FALSE} # Simulating candidate choices choices <- sample(c(“A”, “B”, “C”), n, replace = TRUE, prob = c(pA, pB, pC)) print(table(choices))

Me: How can I hide text and comments in the HTML output? AI: Use include=FALSE to prevent any output from appearing in the final document. Student: I’m getting an error in my code. What should I do? AI: Ensure that any variables, like prob_matrix, are properly defined before you use them.

Me: How can I make my histogram transparent? AI: To make the bars of your histogram transparent in R, you can use the rgb() function. The rgb() function takes the red, green, and blue (R, G, B) components to define colors and allows you to adjust transparency with the last parameter, alpha.

Me: Is it normal for the graphs of weekly closing prices and normal closing prices to look exactly the same or have I made a mistake? Daily and Weekly Closing Prices Match

AI:If you took your weekly closing prices as the closing price on Fridays, and your daily data also includes Fridays, these two datasets would likely look almost identical in the graph since both represent the closing price at the end of the week. Moving Average: If you calculate a weekly average from your daily data, daily and weekly prices may show similarities. For instance, if weekly prices are taken as the average or sum of daily prices, similar patterns on both graphs would be expected. Dataset Size: If your dataset is small and only contains a few days, the difference between daily and weekly prices might be less noticeable. To Verify: Check Different Days: Look at the closing prices for both daily and weekly data separately. Observe the differences in closing prices on different days. Review Closing Price Calculations: Check the methods and code used to calculate closing prices to ensure there are no calculation errors. Extend the Dataset: If the dataset is small, try gathering data over a longer time period (e.g., a few years). Having more data may make differences more apparent. Conclusion: If none of these factors explain your situation, reviewing your code could be beneficial to check for any potential errors.

Me:Now help me to calculate a rolling window (8 weeks) of standard deviations of each stock, i.e first observation for this series is the standard deviation which is calculated using the first eight weeks, the second one is calculated using the next 8 weeks starting at the second week (hence the name rolling window). AI: Here’s how to calculate the 8-week rolling standard deviation for each stock, plot these along with the returns in a single graph, and find the correlation between the returns and rolling standard deviations. (Explains the code).

Me: “Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf” What can do for that? AI: The warnings you’re seeing (no non-missing arguments to min and max; returning Inf) indicate that the prob_matrix contains only NA values. This typically happens when the function p returns NA for all combinations of n and x in your loop. The root cause here is that the placeholder NA in p for cases where x > 2 means no probability values were calculated for those cases. You’ll want to ensure that p can compute probabilities for all values in x_values.