<jupyter_start><jupyter_text>2.1<jupyter_code>from math import *
import numpy as np
import random
import matplotlib.pyplot as plt
import scipy.integrate as integrate

plt.style.use('ggplot')

theta = 0.2
lamda_val = 1.0/theta 
sample_count = 1000
degree_of_freedom = 6

def generateRandomNumber():
    random_float = random.uniform(0, 1)
    while random_float == 0 or random_float == 1:
        random_float = random.uniform(0, 1)
    return random_float

def generateExpo(random_number):
    return log(1 - random_number) * (-1.0 / lamda_val)

random_number_mapping = {}

for i in range(sample_count):
    random_number = generateRandomNumber()
    random_number_mapping[random_number] = genexp(random_number)

random_number_list = list(random_number_mapping.keys())
random_number_list.sort()

random_number_expo_list = list(random_number_mapping.values())
binwidth = (max(random_number_expo_list) - min(random_number_expo_list))/degree_of_freedom

lower_limits = []
upper_limits = []

for i in range(degree_of_freedom):
    lower_limits.append(min(random_number_expo_list)+(i*binwidth))
    upper_limits.append(min(random_number_expo_list)+((i+1)*binwidth))
    

ok = []

for index in range(degree_of_freedom):
    num_in_range = 0
    l1 = lowerlimit[index]
    ul = upperlimit[index]
    for random_number_expo in random_number_expo_list:
        if random_number_expo >= l1 and random_number_expo < ul:
            num_in_range += 1
    ok.append(num_in_range)
    
ok[-1] += 1


pk = []
ek = []

def Pk(lowerlimit, upperlimit):
    return integrate.quad(lambda x: (lamda_val * exp(-x / theta)), lowerlimit, upperlimit)[0]

for i in range(degree_of_freedom):
    pk.append(Pk(lowerlimit[i], upperlimit[i]))
    
ek = [value*sample_count for value in pk]

def x2i(ok, ek):
    return (ok-ek)**2/ek

x2 = []
for i in range(degree_of_freedom):
    x2.append(x2i(ok[i], ek[i]))

plt.hist(arr, bins=np.arange(min(random_number_expo_list), max(random_number_expo_list) + binwidth, binwidth), edgecolor='black', linewidth=binwidth)
plt.xticks(np.arange(min(random_number_expo_list), max(random_number_expo_list) + binwidth, binwidth), fontsize = 6)
plt.ylabel('Frequency')
plt.xlabel('Range of Inverse Exponential')
plt.title("Inverse Exponential")
plt.show()
plt.savefig("Q2.1.png")<jupyter_output><empty_output><jupyter_text>2.2<jupyter_code>from math import *
import numpy as np
import random
import matplotlib.pyplot as plt
import scipy.integrate as integrate

plt.style.use('ggplot')

theta = 0.2
lamda_val = 1.0/theta 
sample_count = 1000 #n
degree_of_freedom = 6 #k

def generateRandomNumber():
    random_float = random.uniform(0,1)
    while random_float == 0 or random_float == 1:
        random_float = random.uniform(0,1)
    return random_float

def generateExpo(random_number):
    return log(1 - random_number) * (-1.0 / lamda_val)

def getX():
    return generateExpo(generateRandomNumber())

def getNIterations():
    sum_of_x = 0
    no_iterations = 0 
    while sum_of_x < 1:
        sum_of_x += getX()
        no_iterations += 1
    return no_iterations

iterations = [getniterations() for x in range(sample_count)]

plt.hist(iterations, bins=np.arange(min(iterations)-1, max(iterations)+2, 1),edgecolor='black', align='left')

plt.xticks(np.arange(min(iterations) , max(iterations) + 1, 1), fontsize = 10)
plt.ylabel('Frequency')
plt.xlabel('No. of Time Intervals')
plt.title("Histogram of # of Exponentially Distributed Time Intervals in 1 time unit, in 1000 Samples")
plt.savefig("Q2.2.png")<jupyter_output><empty_output><jupyter_text>1.1<jupyter_code>import networkx as nx
import random
import matplotlib.pyplot as plt

probability = 0.12 # 0.02, 0.09
number_of_samples = 50

graph = nx.Graph()

edges = []

for row in range(1, number_of_samples + 1):
    for col in range(1, number_of_samples + 1):
        if row != col:
            if([row, col] not in edges and [col, row] not in edges):
                edges.append([row, col])

total_edges = len(edges)
        
selected_edges = []

for i in range(0, total_edges):
    random_number = random.randint(1, 100)
    if random_number <= probability * 100:
        selected_edges.append(edges[i])

graph.add_edges_from(selected_edges)

positions = nx.spring_layout(graph, k=20.0,iterations=100)

nx.draw(graph, positions, edgelist=tuple(list(graph.edges())), with_labels=True, node_color='r', width=2, edge_cmap=plt.cm.Blues)
# plt.show()
plt.title('p = %f' % (probability))
plt.savefig('1.1-%.2f.png' % probability)<jupyter_output><empty_output><jupyter_text>1.2<jupyter_code>import random
import numpy as np
import matplotlib.pyplot as plt

probability = 0.12
number_of_samples = 50

edges = []
for row in range(1, number_of_samples + 1):
    for col in range(1, number_of_samples + 1):
        if row != col:
            if([row, col] not in edges and [col, row] not in edges):
                edges.append([row, col])

total_edges = len(edges)
        
selected_edges = []

for i in range(0, total_edges):
    random_number = random.randint(1, 100)
    if random_number <= probability * 100:
        selected_edges.append(edges[i])

degree = {}
for i in range(1, number_of_samples + 1):
    degree[i] = 0

for edge in selected_edges:
    degree[edge[0]] += 1
    degree[edge[1]] += 1

degrees = {}

for i in range(0, number_of_samples):
    degrees[i] = 0

for i in degree.values():
    degrees[i] += 1

x_ticks = list(range(0, number_of_samples))
x = np.arange(number_of_samples)
y = degrees.values()

plt.bar(x, y, align='center', alpha=0.5, width = 1.0, linewidth = 0.5)
plt.xticks(x, x_ticks, fontsize = 6)
plt.ylabel('Frequency')
plt.xlabel('Degree of Vertex')
plt.title("Histogram of Degree of Vertex for p = %f" % (probability))
plt.savefig("Q1.2.png")<jupyter_output><empty_output><jupyter_text>1.3<jupyter_code>import random
import numpy as np
import matplotlib.pyplot as plt

probability = 0.06
number_of_samples = 100

edges = []
for row in range(1, number_of_samples + 1):
    for col in range(1, number_of_samples + 1):
        if row != col:
            if([row, col] not in edges and [col, row] not in edges):
                edges.append([row, col])

total_edges = len(edges)
        
selected_edges = []

for i in range(0, total_edges):
    random_number = random.randint(1, 100)
    if random_number <= probability * 100:
        selected_edges.append(edges[i])

degree = {}
for i in range(1, number_of_samples + 1):
    degree[i] = 0

for edge in selected_edges:
    degree[edge[0]] += 1
    degree[edge[1]] += 1

degrees = {}

for i in range(0, number_of_samples):
    degrees[i] = 0

for i in degree.values():
    degrees[i] += 1

x_ticks = list(range(0, number_of_samples))
x = np.arange(number_of_samples)
y = degrees.values()

plt.bar(x, y, align='center', alpha=0.5, width = 1.0, linewidth = 0.5)
plt.xticks(x, x_ticks, fontsize = 6)
plt.ylabel('Frequency')
plt.xlabel('Degree of Vertex')
plt.title("Histogram of Degree of Vertex for p = %f" % (probability))
plt.savefig("Q1.3.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13057733428367782, 'fraction_numerical': 0.026799714896650034, 'mean_word_length': 4.263315828957239, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '374476', 'license': 'MIT', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2561, 'n_words': 586, 'path': '.ipynb_checkpoints/TrainModel-checkpoint.ipynb', 'repo_name': 'amitasviper/HMM-Part-of-Speech-Tagging', 'size': '7015'}