예제 #1
0
6 Inference2
"""
lib.title("#############6 Inference2#############")
#####データを読み取る#####
HPRICE_dataset = lib.load(filename="HPRICE1.csv")
explanatories = ["const", "sqrft", "bdrms"]
explained = ["lprice"]

#####各変数を定義#####
X = lib.df2mat(df=HPRICE_dataset, columns=explanatories)
Y = lib.df2mat(df=HPRICE_dataset, columns=explained)
b = lib.reg(X=X, Y=Y)
"""
6-1の解答
"""
lib.chaper("<6.1の解答>")
#####解答#####
lib.add_suffix(b, labels=explanatories)
print("\n")
"""
6-2の解答
"""
lib.chaper("<6.2の解答>")
#####回帰係数の取得#####
const, sqrft, bdrms = b[0], b[1], b[2]
y_bdrms = bdrms

#####解答#####
print("bdrmsの数が1単位上がると、priceは" + str(round(y_bdrms * 100, 4)) + "%上がる")
print("\n")
"""
예제 #2
0
#!-*-coding:utf-8-*-
import lib
import sys
import numpy as np
"""
3 Multiple Regression
"""
lib.title("#############3 Multiple Regression#############")
#####データを読み取る#####
CEOSAL2_dataset = lib.load(filename="CEOSAL2.csv")
explanatories = ["const", "lsales", "lmktval"]
explained = ["lsalary"]
"""
3-1の解答
"""
lib.chaper("大問3.1の回答")
X = lib.df2mat(df=CEOSAL2_dataset, columns=explanatories)
Y = lib.df2mat(df=CEOSAL2_dataset, columns=explained)
b = lib.reg(X=X, Y=Y)
lib.add_suffix(coefs=b, labels=explanatories)
print("\n")
"""
3-2の解答
"""
lib.chaper("大問3.2の回答")
CEOSAL2_dataset = lib.load(filename="CEOSAL2.csv")
explanatories = ["const", "lsales", "lmktval", "profits"]
explained = ["lsalary"]

X = lib.df2mat(df=CEOSAL2_dataset, columns=explanatories)
Y = lib.df2mat(df=CEOSAL2_dataset, columns=explained)
예제 #3
0
1 Matrix Calculus
"""
lib.title("#############1 Matrix Calculus#############")
#####データを読み取る#####
artifical_dataset = lib.load(filename="artificial.csv")
explanatories = ["x1", "x2"]
explained = ["y"]

#####各変数を定義#####
X = lib.df2mat(df=artifical_dataset, columns=explanatories)
Y = lib.df2mat(df=artifical_dataset, columns=explained)
b = lib.reg(X=X, Y=Y)
"""
1-1の解答
"""
lib.chaper("<大問1.1の回答>")

#####解答#####
lib.add_suffix(b)
print("\n")
"""
1-2の解答
"""
lib.chaper("<大問1.2の回答>")

#####各変数を定義#####
t = lib.t(X=X, Y=Y, beta=b)[0]
print("t value :", t)
print("自由度998で、2.581を基準にしたところ 2.581 <", t, "なので、1%水準でβ0 = 0は棄却される")
print("\n")
"""
예제 #4
0
import sys
import numpy as np
"""
2 Simple Regression
"""
lib.title("#############2 Simple Regression#############")
CEOSAL2_dataset = lib.load(filename="CEOSAL2.csv")
explanatories = ["salary"]
explained = ["lsalary"]

#####各変数を定義#####
X = lib.df2mat(df=CEOSAL2_dataset, columns=explanatories)
"""
2-1の解答
"""
lib.chaper("<大問2.1の回答>")
print("Salaryの平均値 :", round(np.average(X), 3))
explanatories = ["ceoten"]
X = lib.df2mat(df=CEOSAL2_dataset, columns=explanatories)
print("Tenureの平均値 :", round(np.average(X), 3))
print("\n")
"""
2-2の解答
"""
lib.chaper("<大問2.2の回答>")
ceoten = CEOSAL2_dataset[CEOSAL2_dataset["ceoten"] <= 0]
ceoten_max = np.max(np.array(CEOSAL2_dataset["ceoten"]))

#####解答#####
print("CEOの在任期間が0の人の数 :", len(ceoten))
print("CEOの在任期間の最大値 :", ceoten_max)
예제 #5
0
#!-*-coding:utf-8-*-
import lib
import sys
import numpy as np
"""
8 Instrumental Variable And 2SLS
"""
lib.title("#############8 Instrumental Variable And 2SLS#############")

FERTIL2_dataset = lib.load( filename="FERTIL2.csv" )
FERTIL2_dataset = lib.cross_var( df=FERTIL2_dataset , var1="age" , var2="age" )

"""
8-1の解答
"""
lib.chaper("<8.1の解答>")
#####説明変数を定義#####
explanatories = ["const","educ","age","age*age"]
explained = ["children"]

X = lib.df2mat( df=FERTIL2_dataset , columns=explanatories )
Y = lib.df2mat( df=FERTIL2_dataset , columns=explained )
b = lib.reg( X=X , Y=Y )
lib.add_suffix( coefs=b , labels=explanatories )
const , educ , age , age_aqure = b[0] , b[1] , b[2] , b[3]
y = educ * 100

print( y )
print( "9人減る" )

print("\n")
예제 #6
0
"""
4 Multiple Regression
"""
lib.title("#############4 Multiple Regression#############")
LOANAPP_dataset = lib.load( filename="LOANAPP.csv" )
explanatories = ["const","white"]
explained = ["approve"]

#####各変数を定義#####
X = lib.df2mat( df=LOANAPP_dataset , columns=explanatories )
Y = lib.df2mat( df=LOANAPP_dataset , columns=explained )

"""
4-1の解答
"""
lib.chaper( "大問4.1の回答" )
print( "係数が正の状態で、統計的に有意な値をとる" )
print("\n")

"""
4-2の解答
"""
lib.chaper( "大問4.2の回答" )
b = lib.reg( X=X , Y=Y )
lib.add_suffix( b )
t = lib.t( X=X , Y=Y , beta=b )[0]
print( "t値 :", t )
print( "1%有意で" , round( b[1] , 3  ) )
print( "大きい" )
print("\n")
예제 #7
0
#!-*-coding:utf-8-*-
import lib
import sys
import numpy as np
"""
5 Inference
"""
lib.title("#############5 Inference#############")
VOTE_dataset = lib.load( filename="VOTE1.csv" )

"""
5-1の解答
"""
lib.chaper("<5.1の解答>")
print("expendAが1%上がると、voteAが100分のβ1だけ上昇する")
print("\n")

"""
5-2の解答
"""
lib.chaper("<5.2の解答>")
print( "β1 - β2 = 0" )
print("\n")

"""
5-3の解答
"""
lib.chaper("<5.3の解答>")
#####説明変数を定義#####
explanatories = ["const","lexpendA","lexpendB","prtystrA"]
explained = ["voteA"]
예제 #8
0
#!-*-coding:utf-8-*-
import lib
import sys
import numpy as np
"""
7 Panel Data
"""
lib.title("#############7 Panel Data#############")

grunfeld_dataset = lib.load(filename="grunfeld.csv")
"""
7-1の解答
"""
lib.chaper("<7.1の解答>")
explanatories = ["const", "value", "capital"]
explained = ["invest"]

#B = lib.random_effect( df=grunfeld_dataset , group="year" , X_cols=explanatories , Y_cols=explained )
X = lib.df2mat(df=grunfeld_dataset, columns=explanatories)
Y = lib.df2mat(df=grunfeld_dataset, columns=explained)
b = lib.reg(X=X, Y=Y)
print("提出の時は、βにちゃんと係数をいれましょう ")
lib.add_suffix(coefs=b, labels=explanatories)
print("\n")
"""
7-2の解答
"""
lib.not_done("<7.2の解答>")
print("土屋くんに聞く")
firms = list(set(list(grunfeld_dataset["firm"])))