Skip to content

Instantly share code, notes, and snippets.

@y-mitsui
y-mitsui / bayesiannet.c
Created May 6, 2022 00:16
最も基本的なベイジアンネットワーク
/* ベイズ情報量基準 */
double bic(double logLike,int numSample,int numModel){
return -2*logLike+numModel*log(numSample);
}
/* 学習スコア */
double score(int numNode,int *edge,int numSample,double **likelies,int *variablePattern){
double like;
int i,j,numModel,numParents;
unsigned char parentPattern;
@y-mitsui
y-mitsui / g_test.py
Created February 19, 2019 10:50
G検定による独立性の検定
"""
G検定による独立性の検定
"""
import numpy as np
from scipy.stats import chisqprob, chisquare
def gtest(f_obs, f_exp=None, ddof=0):
"""
http://en.wikipedia.org/wiki/G-test
The G test can test for goodness of fit to a distribution
@y-mitsui
y-mitsui / bn.R
Created February 17, 2019 06:29
Rでベイジアンネットワーク
# Refererce:http://ushi-goroshi.hatenablog.com/entry/2018/01/11/233317
# install.packages("bnlearn")
# install.packages("BNSL")
library(bnlearn)
library(BNSL)
set.seed(123)
norm <- rnorm(4000)
@y-mitsui
y-mitsui / bayesian_linear_regression.py
Created June 11, 2018 11:52
bayesian linear regression in Python
import numpy as np
n_sample = 1000
true_coef = np.array([2.0, 3.0])
u0 = np.array([0.0, 0.0])
A0 = 0.1 * np.eye(2)
sample_X = np.random.normal(size=(n_sample, 2))
sample_y = np.dot(sample_X, true_coef.reshape(-1, 1)).reshape(-1,) + np.random.normal(size=n_sample)
tmp1 = np.linalg.inv(np.dot(sample_X.T, sample_X))
@y-mitsui
y-mitsui / string_kernel.py
Created June 11, 2018 11:45
String Kernel in Python
"""
String Kernel
Reference: "5.構造化データに対するカーネル" 福水健次
http://www.ism.ac.jp/~fukumizu/ISM_lecture_2010/Kernel_5_structured.pdf
"""
import numpy as np
def subSequenceJustLength(result, str, cur_str, length, depth=0):
if depth == length:
result.append(cur_str)
# -*- coding: utf-8 -*-
"""
Reference:
トピックモデルによる統計的潜在意味解析 4章
"""
from __future__ import print_function
from scipy.special import digamma
import numpy as np
import time
@y-mitsui
y-mitsui / slda_logistic_vb.py
Last active June 3, 2018 12:08
logistic supervied topic model using variational bayesian
# -*- coding: utf-8 -*-
"""
Reference:
トピックモデルによる統計的潜在意味解析 4章
"""
from __future__ import print_function
from scipy.special import digamma
import numpy as np
import time
'''
Created on 2017/05/24
'''
from sklearn.grid_search import GridSearchCV
import numpy as np
from sklearn.cross_validation import KFold
from sklearn.metrics import mean_squared_error
import pystan
import numpy as np
stan_code = """
data {
int<lower=0> N;
int<lower=0> K;
matrix[N,K] x;
vector[N] y;
}
import pystan
import numpy as np
stan_code = """
data {
int<lower=0> N;
real<lower=0> x[N];
}
parameters {
real mu;