Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions simulation/t01/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.11
4 changes: 4 additions & 0 deletions simulation/t01/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Para el 3 ordenarlo de menor a mayor
- Sea una m.a. de 10 elementos, entonces $F_n(x)$
- Para el 3, después de simular la uniforme, evaluar en la función de distribución empírica obtenida previamente,
- Recordando que $F^{-1}(U)\sim F$ con $U\sim U(0,1)$
11 changes: 11 additions & 0 deletions simulation/t01/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python

from yfinance import Ticker

def get_data(tick:str = "^GSPC", years:int = 2, data_path:str = "data/"):
sp500 = Ticker(tick).history(period = f"{years}y")
print(sp500.describe())
sp500.to_csv(data_path + "sp500.csv", encoding='utf-8')

def main():
get_data()
29 changes: 29 additions & 0 deletions simulation/t01/gcl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python
# from plotnine import *
from math import gcd

def gcl(x_n:int , a: int, c:int, m:int)->int:
return (a * x_n + c ) % m

def normalize(vals:list[int])->list[float]:
max_val = max(vals)
return [v/max_val for v in vals]

def is_prime(n:int)-> bool:
for i in range(2, n): #for every value between 1 and n
if n % i == 0: #check if i divides n
return False #if this is true, n is not prime
return True if n > 1 else False #values less than 2 are not prime.

def prim_root(m:int) -> list[int]:
required_set = { n for n in range(1, m) if gcd(n, m) }
return [ g for g in range(1,m) if required_set == { pow(g, powers, m) for powers in range(1, m) } ]

def main():
# print(len(prim_root(3259)))
print(len(prim_root(3229)))
print(len(prim_root(3221)))
print(len(prim_root(3217)))

if __name__ == "__main__":
main()
50 changes: 50 additions & 0 deletions simulation/t01/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python
from gcl import gcl, prim_root, is_prime
from plotnine import (
aes, ggplot, labs,
geom_histogram, geom_point
)
from numpy import var, average

def plot_unif(y: list[float], media_path: str = "media/") -> None:
h = (
ggplot(aes(x = y))
+ geom_histogram(bins = 25)
+ labs(title = "Histograma de gcl")
)
d = (
ggplot(aes(x = range(0, len(y)), y = y))
+ geom_point()
+ labs(title = "Diagrama de dispersión de gcl")
)
d.save(media_path + "dispersion.png")
h.save(media_path + "histogram.png")
d.show()
h.show()
return y

def print_ej1(n:int = 3259, n_sims: int = 1000):
q_is_prime = is_prime(n)
yes_or_no = "sí" if q_is_prime else "no"
primitive_roots = prim_root(n)
print(f"Veamos que {n} {yes_or_no} es número primo")
print(f"Veamos que {n} tiene a {min(primitive_roots)} como su primera raíz primitiva")
if q_is_prime:
print("Un generador congruencial de periodo maximal es:")
print("x_{n+1}=" + str(min(primitive_roots)) + "x_n")
print("Con x_0 = 2")
print(f"Simulando {n_sims} con gcl")
sims = [ 2 ]
for _ in range(n_sims):
sims.append(gcl(sims[-1], min(primitive_roots), 0, n))
print("Medidas de tendencia central de la simulación")
print(f"Media: {average(sims)}")
print(f"varianza: {var(sims)}")
plot_unif(sims)

def main():
print_ej1()


if __name__ == "__main__":
main()
Binary file added simulation/t01/media/dispersion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added simulation/t01/media/histogram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions simulation/t01/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "t01"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
"plotnine>=0.15.3",
"scipy>=1.17.1",
"yfinance>=1.2.0",
]
1,135 changes: 1,135 additions & 0 deletions simulation/t01/uv.lock

Large diffs are not rendered by default.