<jupyter_start><jupyter_text>Non-linear least square* `DataProblem2019.csv`<jupyter_code>import pandas as pd
import numpy as np
from scipy.optimize import minimize
df = pd.read_csv('DataProblem201904.csv', header=None)
data = df.values
y = data[:,0]
y.shape
n = data.shape[0]
X = np.hstack([np.ones((n,1)), data[:,1:3]])
#x0 = np.ones(n)
#x1 = data[:,1]
#x2 = data[:,2]
X[1:5]
X.shape
X[1].shape
def loss(beta):
    sum = 0
    for i in range(n):
        sum += (y[i] - np.dot(X[i], beta))**2
    return sum
minimize(loss, x0=[1,1,1])
# another initial guess
minimize(loss, x0=[0,0,0])<jupyter_output><empty_output><jupyter_text>`np.newaxis`* `np.dot`は多少shapeがおかしくても動いてくれるが，@はshapeをキチンと合わせないとだめっぽい* `np.newaxis`を使ってshapeを調整<jupyter_code>def loss_(beta):
    sum = 0
    for i in range(n):
        sum += (y[i] - X[i][:np.newaxis] @ beta)**2
    return sum
minimize(loss, x0=[1,1,1])<jupyter_output><empty_output><jupyter_text>Discrete Choice* `DataMat.csv`<jupyter_code>import pandas as pd
import numpy as np
from scipy.optimize import minimize
df = pd.read_csv('DataMat.csv', header=None)
data = df.values
data.shape
mu, beta = 0, 1 # location and scale
s = np.random.gumbel(mu, beta, 1000)
s.shape
import matplotlib.pyplot as plt
%matplotlib inline

count, bins, ignored = plt.hist(s, 30, density=True)
plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta) * np.exp( -np.exp( -(bins - mu) /beta) ), linewidth=2, color='r')
plt.show()<jupyter_output><empty_output><jupyter_text>\[    u_{ij} = X_i \beta_j + \epsilon_{ij}\]Assuming $\epsilon_{ij}$ are independently drawn from Type I extreme value distribution, we have\[    \Pr(d_i = j \mid X_t) = \frac{\exp(X_i \beta_j)}{\sum_{k=1}^J \exp(X_i \beta_k)}\]Obtain $\beta$ that maximizes the following value\[    \prod_{i=1}^n \left( \prod_j \left \{ \frac{\exp(X_i \beta_j)}{\sum_{k=1}^J \exp(X_i \beta_k)} \right\}^{y_{ij}} \right)\]where $y_{ij} = 1$ means agent $i$ chooses option $j$.<jupyter_code>n = data.shape[0]
k = data.shape[1] - 1
y = 
X = 
def likelihood(beta):<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18545632015617375, 'fraction_numerical': 0.027330405075646658, 'mean_word_length': 4.790960451977401, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '464703', 'license': 'MIT', 'n_tokens_mistral': 904, 'n_tokens_neox': 871, 'n_words': 185, 'path': 'notebook/week2.ipynb', 'repo_name': 'okuchap/GradIO', 'size': '2049'}