Skip to content

Instantly share code, notes, and snippets.

@sld
Created October 24, 2014 08:29
Show Gist options
  • Save sld/4226c8ff9ff474dde0a5 to your computer and use it in GitHub Desktop.
Save sld/4226c8ff9ff474dde0a5 to your computer and use it in GitHub Desktop.
Statistics' tasks
# coding: utf-8
import numpy as np
import cmath as math
import scipy.stats as stats
import random
def generate_exponential(exp_lambda, size):
scale = 1 / exp_lambda
return np.random.exponential(scale, size)
def exponential_mean(exp_lambda):
return 1 / exp_lambda
def exponential_variance(exp_lambda):
return exponential_mean(exp_lambda)**2
def sample_variance(sample):
diff = sum([(x - sample.mean())**2 for x in sample])
return (1.0 / (len(sample) - 1)) * diff
def noncentral_student(df, noncentral, size_ = 1):
return stats.nct.rvs(df, noncentral, size=size_)
def combination_of_distributions(size, conditioner = 0.1):
result = []
for i in range(0, size):
is_exp = (1 - conditioner) >= random.random()
if is_exp:
result.append(generate_exponential(0.25, 1)[0])
else:
result.append(noncentral_student(3, 4, 1)[0])
return np.array(result)
def confidential_interval(sample):
low = sample.mean() - 3*sample_variance(sample) / math.sqrt( len(sample) )
high = sample.mean() + 3*sample_variance(sample) / math.sqrt( len(sample) )
return [low, high]
def is_mean_in_conf_interval(conf_interval, mean):
return (mean >= conf_interval[0] and mean <= conf_interval[1])
def task1():
size = 8
exp_lambda = 0.25
sample = generate_exponential(exp_lambda, size)
print "Sample: " + str(sample)
print "Sample mean: " + str(sample.mean())
print "Sample variance: " + str(sample_variance(sample))
mean = exponential_mean(exp_lambda)
print "Theoretical mean: " + str(mean)
print "Theoretical variance: " + str(exponential_variance(exp_lambda))
conf_interval = confidential_interval(sample)
print "Condidence interval: " + str(conf_interval)
print "Mean in conf interval: " + str(is_mean_in_conf_interval(conf_interval, mean))
def task2():
size = 8000
exp_lambda = 0.25
conditioner = 0.05
for i in range(0,4):
conditioner = conditioner*2
print "conditioner is " + str(conditioner)
sample = combination_of_distributions(size, conditioner)
# print sample
print "Sample mean: " + str(sample.mean())
print "Sample variance: " + str(sample_variance(sample))
mean = exponential_mean(exp_lambda)
print "Theoretical mean: " + str(mean)
conf_interval = confidential_interval(sample)
print "Condidence interval: " + str(conf_interval)
print "Mean in conf interval: " + str(is_mean_in_conf_interval(conf_interval, mean))
print "\n"
def task3():
h0_m = -1
h0_s = 1
h1_m = -9
h1_s = 3
vec = np.random.uniform(-18, 2, 6)
norms = np.random.normal(h0_m, h0_s)
print vec
print norms
# Uncomplete yet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment