Код: Выделить всё
import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
import random
import math
np.random.seed(2)
n_samples = 180
time = np.arange(n_samples)
mean_value = random.randrange(60, 90)
mean = np.full(n_samples, mean_value)
# The fixed mean segment is generated randomly
K = random.randint(10, 40)
for i in range(K, n_samples, K):
mean[i:] = mean[i - K] - 10
noise = np.random.randn(n_samples) * random.normalvariate(4, 2)
y = mean + noise
BIC = -2Log(L)
код, который у меня есть,
Код: Выделить всё
def find_optimal_change_point(data):
min_bic = float('inf')
bics = np.full(len(data),min_bic)
change_points = [0] * K
for i in range(1, len(data)):
segment = data[:i]
mean, var = np.mean(data[:i - 1]), np.var(data[:i - 1])
N = len(segment)
S = var
new_bic = bic(N, S, 4, segment, v, len(segment),index=i)
if bics[i-1] > new_bic:
bics[i] = new_bic
change_points[k] = data[i]
elif bics[i] < bics[i-1] :
break
return change_points
Подробнее здесь: https://stackoverflow.com/questions/793 ... -number-of