@@ -13,6 +13,7 @@ def lomb_scargle_model(
13
13
tone_control = 5.0 ,
14
14
normalize = False ,
15
15
default_order = 1 ,
16
+ freq_grid = None ,
16
17
):
17
18
"""Simultaneous fit of a sum of sinusoids by weighted least squares.
18
19
@@ -51,6 +52,14 @@ def lomb_scargle_model(
51
52
Order of polynomial to fit to the data while
52
53
fitting the dominant frequency. Defaults to 1.
53
54
55
+ freq_grid : dict or None, optional
56
+ Grid parameters to use. If None, then calculate the frequency
57
+ grid automatically. To supply the grid, keys
58
+ ["f0", "fmax"] are expected. "f0" is the smallest frequency
59
+ in the grid and "df" is the difference between grid points. If
60
+ ""df" is given (grid spacing) then the number of frequencies
61
+ is calculated. if "numf" is given then "df" is inferred.
62
+
54
63
Returns
55
64
-------
56
65
dict
@@ -73,12 +82,27 @@ def lomb_scargle_model(
73
82
dy0 = np .sqrt (error ** 2 + sys_err ** 2 )
74
83
wt = 1.0 / dy0 ** 2
75
84
chi0 = np .dot (signal ** 2 , wt )
76
-
77
- # TODO parametrize?
78
- f0 = 1.0 / max (time )
79
- df = 0.8 / max (time ) # 20120202 : 0.1/Xmax
80
- fmax = 33.0 # pre 20120126: 10. # 25
81
- numf = int ((fmax - f0 ) / df ) # TODO !!! this is off by 1 point, fix?
85
+ if freq_grid is None :
86
+ f0 = 1.0 / max (time )
87
+ df = 0.8 / max (time ) # 20120202 : 0.1/Xmax
88
+ fmax = 33.0 # pre 20120126: 10. # 25
89
+ numf = int ((fmax - f0 ) / df ) + 1
90
+ else :
91
+ f0 = freq_grid ["f0" ]
92
+ fmax = freq_grid ["fmax" ]
93
+ df = freq_grid .get ("df" )
94
+ tmp_numf = freq_grid .get ("numf" )
95
+ if df is not None :
96
+ # calculate numf
97
+ numf = int ((fmax - f0 ) / df ) + 1
98
+ elif tmp_numf is not None :
99
+ df = (fmax - f0 ) / (tmp_numf - 1 )
100
+ numf = tmp_numf
101
+ else :
102
+ raise Exception ("Both df and numf cannot be None." )
103
+
104
+ if f0 >= fmax :
105
+ raise Exception (f"f0 { f0 } should be smaller than fmax { fmax } " )
82
106
83
107
model_dict = {"freq_fits" : []}
84
108
lambda0_range = [
@@ -127,6 +151,7 @@ def lomb_scargle_model(
127
151
model_dict ["nharm" ] = nharm
128
152
model_dict ["chi2" ] = current_fit ["chi2" ]
129
153
model_dict ["f0" ] = f0
154
+ model_dict ["fmax" ] = fmax
130
155
model_dict ["df" ] = df
131
156
model_dict ["numf" ] = numf
132
157
@@ -429,6 +454,10 @@ def fit_lomb_scargle(
429
454
ncp = norm .cumprod ()
430
455
out_dict ["trend_coef" ] = coef / ncp
431
456
out_dict ["y_offset" ] = out_dict ["trend_coef" ][0 ] - cn0
457
+ out_dict ["trend_coef_error" ] = np .sqrt (
458
+ (1.0 / s0 + np .diag (np .dot (hat0 .T , np .dot (hat_hat , hat0 )))) / ncp ** 2
459
+ )
460
+ out_dict ["y_offset_error" ] = out_dict ["trend_coef_error" ][0 ]
432
461
433
462
prob = stats .f .sf (
434
463
0.5
0 commit comments