Skip to content

Commit c7a8945

Browse files
committed
Add some problem solutions
These problems come from Intro to Statistics contest.
1 parent 9cf5ec8 commit c7a8945

7 files changed

+129
-0
lines changed

‎basic-statistics-warmup-2.cpp‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#include <vector>
4+
#include <numeric>
5+
#include <algorithm>
6+
#include <map>
7+
using namespace std;
8+
9+
vector<int> v;
10+
map<int, int> m;
11+
12+
int main() {
13+
int n;
14+
cin >> n;
15+
cout.setf(ios::fixed);
16+
cout.precision(1);
17+
for (int i = 0; i < n; i++) {
18+
int num;
19+
cin >> num;
20+
v.push_back(num);
21+
}
22+
double mean = accumulate(v.begin(), v.end(), 0) / (double)n;
23+
cout << mean << endl;
24+
sort(v.begin(), v.end());
25+
double median;
26+
if (n % 2 == 1)
27+
median = v[n / 2];
28+
else
29+
median = (v[n / 2 - 1] + v[n / 2]) / 2.0;
30+
cout << median << endl;
31+
for (int i = 0; i < n; i++) {
32+
auto it = m.find(v[i]);
33+
if (it != m.end())
34+
(it->second)++;
35+
else
36+
m.insert(make_pair(v[i], 1));
37+
}
38+
int mode, maxTimes = 0;
39+
for (auto it = m.begin(); it != m.end(); ++it) {
40+
if (it->second > maxTimes || (it->second == maxTimes && it->first < mode)) {
41+
mode = it->first;
42+
maxTimes = it->second;
43+
}
44+
}
45+
cout << mode << endl;
46+
double accum = 0.0;
47+
for (int i = 0; i < n; i++) {
48+
accum += (v[i] - mean) * (v[i] - mean);
49+
}
50+
double stdev = sqrt(accum / (double)n);
51+
cout << stdev << endl;
52+
return 0;
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#day-5-introduction-to-linear-regression.py
2+
#Day 5: Introduction to Linear Regression
3+
#Intro to Statistics
4+
#By derekhh
5+
#Apr 1, 2016
6+
7+
import numpy as np
8+
from sklearn import linear_model
9+
10+
x = [95, 85, 80, 70, 60]
11+
x = np.reshape(x, (5, 1))
12+
13+
y = [85, 95, 70, 65, 70]
14+
y = np.reshape(y, (5, -1))
15+
16+
reg = linear_model.LinearRegression()
17+
reg.fit(x, y)
18+
print("%.1f" % reg.predict([[80]]))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#day-5-introduction-to-correlation.py
2+
#Day 5: Introduction to Correlation
3+
#Intro to Statistics
4+
#By derekhh
5+
#Apr 1, 2016
6+
7+
from scipy import stats
8+
9+
x = [10, 9.8, 8, 7.8, 7.7, 7, 6, 5, 4, 2]
10+
y = [200, 44, 32, 24, 22, 17, 15, 12, 8, 4]
11+
p = stats.pearsonr(x,y)
12+
r = stats.spearmanr(x, y)
13+
print("%.3f" % p[0])
14+
print("%.1f" % r[0])

‎normal-distribution-1.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#normal-distribution-1.py
2+
#Normal Distribution #1
3+
#Intro to Statistics
4+
#By derekhh
5+
#Mar 31, 2016
6+
7+
from scipy.stats import norm
8+
9+
distribution = norm(30, 4)
10+
print("%.3f" % distribution.cdf(40))
11+
print("%.3f" % (1.0 - distribution.cdf(21)))
12+
print("%.3f" % (distribution.cdf(35) - (1.0 - distribution.cdf(30))))

‎normal-distribution-2.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#normal-distribution-2.py
2+
#Normal Distribution #2
3+
#Intro to Statistics
4+
#By derekhh
5+
#Mar 31, 2016
6+
7+
from scipy.stats import norm
8+
9+
distribution = norm(20, 2)
10+
print("%.3f" % distribution.cdf(19.5))
11+
print("%.3f" % (distribution.cdf(22) - (1.0 - distribution.cdf(20))))
12+

‎the-central-limit-theorem-1.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#the-central-limit-theorem-1.py
2+
#The Central Limit Theorem #1
3+
#Intro to Statistics
4+
#By derekhh
5+
#Mar 31, 2016
6+
7+
from scipy.stats import norm
8+
9+
distribution = norm(205 * 49, 15 * 7)
10+
print("%.4f" % distribution.cdf(9800))

‎the-central-limit-theorem-2.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#the-central-limit-theorem-2.py
2+
#The Central Limit Theorem #2
3+
#Intro to Statistics
4+
#By derekhh
5+
#Mar 31, 2016
6+
7+
from scipy.stats import norm
8+
9+
distribution = norm(2.4 * 100, 2.0 * 10)
10+
print("%.4f" % distribution.cdf(250))

0 commit comments

Comments
 (0)