<jupyter_start><jupyter_text>Fibonacci 1In this notebook I have my first explorations with data ecoded with the Fibonacci sequence.The approach here is to identify "events" (e.g. flares or transits) and then explore the wait-time distribution to see if it follows a Fibonacci sequence.The positive aspect of this approach is that the sequence matching is shape agnostic, just exploring inter-event times. In the case of Fibonacci, it can pick up the sequence at any pointThe negative aspect is of course requiring events to be found, and is therefore not "signal detection" but insted "pattern detection" https://en.wikipedia.org/wiki/Integer_sequencehttps://en.wikipedia.org/wiki/Fibonacci_number<jupyter_code>fib(15)
fib_approx(15)
# let's make a Fib-spaced light curve pulse sequence

std = 1.8
tau = 9.123
amp = 2.
t0 = 10

time = np.arange(-10, 2048, 0.5)

flux = np.random.random(len(time))*1 - 0.5 
flux = flux + fib_gaus1(time, tau, amp, std, t0=t0)

plt.figure(figsize=(16,4))
plt.plot(time, flux)
plt.xlabel('Time')
plt.ylabel('Flux')
plt.savefig('big_lc.png', dpi=300, bbox_inches='tight', pad_inches=0.25)
plt.figure(figsize=(10,4))
plt.plot(time, flux)
plt.xlabel('Time')
plt.ylabel('Flux')
plt.xlim(-10,150)
# plt.arrow(4,2,3,0)
plt.savefig('small_lc.png', dpi=300, bbox_inches='tight', pad_inches=0.25)
GR = 1.61803398875 # the GOLDEN RATIO (of course...)<jupyter_output><empty_output><jupyter_text>Lomb Scargle<jupyter_code>fspace = tau

frequency, power = LombScargle(time, flux).autopower()

plt.plot(1. / frequency, power)

plt.xscale('log')
plt.ylim(0,.02)

plt.plot([fspace, fspace],[0,1], c='r', linestyle='--', alpha=0.5)

# plt.plot([fspace*2, fspace*2],[0,1], c='r', linestyle=':', alpha=0.5)
plt.plot([fspace*GR, fspace*GR],[0,1], c='k', linestyle='--', lw=2)

plt.plot([fspace * GR**2, fspace * GR**2],[0,1], c='k', linestyle=':', alpha=0.7)
plt.plot([fspace * GR**3, fspace * GR**3],[0,1], c='k', linestyle=':', alpha=0.5)
plt.plot([fspace * GR**4, fspace * GR**4],[0,1], c='k', linestyle=':', alpha=0.3)
plt.plot([fspace * GR**5, fspace * GR**5],[0,1], c='k', linestyle=':', alpha=0.15)


plt.xlim(1, 250)
plt.xlabel('Period')
plt.ylabel('Power')
plt.savefig('LS.png', dpi=300, bbox_inches='tight', pad_inches=0.25)<jupyter_output><empty_output><jupyter_text>Rayleigh<jupyter_code>ps = rayleigh.RayleighPowerSpectrum(time[np.where((flux > 1.75))[0]], minper=1, maxper=250, nper=2500)

plt.figure()
plt.plot(ps)
plt.xlabel('Period (days)')
plt.ylabel('Rayleigh power (z)')
plt.xscale('log')

plt.plot([fspace, fspace],[0,30], c='r', linestyle='--', alpha=0.5)
# plt.plot([fspace*2, fspace*2],[0,30], c='r', linestyle=':', alpha=0.5)
# plt.plot([fspace*3, fspace*3],[0,30], c='r', linestyle=':', alpha=0.3)
# plt.plot([fspace*4, fspace*4],[0,30], c='r', linestyle=':', alpha=0.2)

plt.plot([fspace * GR, fspace * GR],[0,30], c='k', linestyle='--', alpha=0.5)
plt.plot([fspace * GR**2, fspace * GR**2],[0,30], c='k', linestyle=':', alpha=0.5)
plt.plot([fspace * GR**3, fspace * GR**3],[0,30], c='k', linestyle=':', alpha=0.3)
plt.plot([fspace * GR**4, fspace * GR**4],[0,30], c='k', linestyle=':', alpha=0.2)
plt.plot([fspace * GR**5, fspace * GR**5],[0,30], c='k', linestyle=':', alpha=0.1)
plt.figure()
plt.plot(np.linspace(1, 250, 2500), ps)
plt.xlabel('Period (days)')
plt.ylabel('Rayleigh power (z)')
plt.xscale('log')

plt.plot([fspace, fspace],[0,30], c='r', linestyle='--', alpha=0.5)
plt.plot([fspace*2, fspace*2],[0,30], c='r', linestyle=':', alpha=0.5)
plt.plot([fspace*3, fspace*3],[0,30], c='r', linestyle=':', alpha=0.3)
plt.plot([fspace*4, fspace*4],[0,30], c='r', linestyle=':', alpha=0.2)

# plt.plot([fspace*GR, fspace*GR],[0,30], c='k', linestyle='--', alpha=0.5)
# plt.plot([fspace * GR**2, fspace * GR**2],[0,30], c='k', linestyle=':', alpha=0.5)
# plt.plot([fspace * GR**3, fspace * GR**3],[0,30], c='k', linestyle=':', alpha=0.3)
# plt.plot([fspace * GR**4, fspace * GR**4],[0,30], c='k', linestyle=':', alpha=0.2)
# plt.plot([fspace * GR**5, fspace * GR**5],[0,30], c='k', linestyle=':', alpha=0.1)<jupyter_output><empty_output><jupyter_text>Testing a FIB sequence w/ brute force<jupyter_code>true_fib = fib(10)
true_fib
# the true Fib sequence + a small amount of noise
test1 = true_fib + np.random.random(10) * 0.01
test1
# random numbers
test2 = np.random.random(10) * true_fib.max()
test2.sort()
test2<jupyter_output><empty_output><jupyter_text>can we tell that `test1` is a Fib sequence, and `test2` is not?<jupyter_code>def fib_test1(x, tol=0.1):
    '''
    the most simple, brute-force tester, assume the sequence starts from the beginning!
    
    x = the timestamps of the events
    tol = the fudge-factor to accept
    '''
    # make the real Fib sequence for this size of numbers
    Fseq = fib(x.size)

    # assume the x[0] - x[1] sequence defines the key length
    # keylen = x[1] - x[0]

    # assume first event is exactly the start
    zp = x[0]
    
    # average the two first steps, since that should be the repeat. 
    # Helps reduce uncertainty by sqrt(N)
    keylen = ((x[2] - x[0]) + (x[1] - x[0])) / 2.

    # using the key-length, test against the perfect Fib seq
    test_seq = ((x[1:] - zp) / keylen) / Fseq[1:] - 1 
    isok = np.abs(test_seq) < tol

    print('keylen = ' + str(keylen))

    return isok
(fib_test1(test1))
fib_test1(test2)
def fib_test2(x, tol=0.1):
    '''
    2nd simple approach:
    given a sequence of spacings, 
    does the previous two predict the one time via The Fib(TM)?
    
    x.size must be > 3
    '''
    
    pred = (x[0:-2] + x[1:-1]) - x[2:]
    
    test = np.abs(pred) < tol
    print(test)
    
    return sum(test) / np.float(x.size-2)
fib_test2(test1)
fib_test2(test2)
test3 = np.concatenate([test2, test1, test2])
fib_test2(test3)
# plt.plot(np.arange(10), fib_approx(10), '-o')
# plt.plot((test1[0:-2] + test1[1:-1]) - test1[2:] , '-o')
def fib_dt(x, tol=0.1, extra = False):
    '''
    simple approach 2b:
    given a sequence of spacings, 
    does the previous three obs predict the next one?
    
    x.size must be > 3
    '''
    
    pred = ((x[2:-1] - x[1:-2]) + (x[1:-2] - x[0:-3])  + x[2:-1]) - x[3:]
    
    test = np.abs(pred) < tol

    if extra:
        print(test)
        
    return float(sum(test)) / np.float(x.size-2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20704985709749127, 'fraction_numerical': 0.05446173388377262, 'mean_word_length': 4.4869337979094075, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 2, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '165288', 'license': 'MIT', 'n_tokens_mistral': 2782, 'n_tokens_neox': 2578, 'n_words': 582, 'path': 'code/explore_fibonacci.ipynb', 'repo_name': 'TheAstroFactory/SETI_ZTF', 'size': '6298'}