import%20marimo%0A%0A__generated_with%20%3D%20%220.13.15%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20cvxpy%20as%20cvx%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20import%20statsmodels.tsa.stattools%20as%20sts%0A%20%20%20%20from%20numpy.linalg%20import%20lstsq%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%20Constructing%20estimators%0A%0A%20%20%20%20https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAutoregressive_model%0A%0A%20%20%20%20**Thomas%20Schmelzer**%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20A%20very%20common%20estimator%20is%20based%20on%20AR%20models%20(autoregressive)%0A%0A%20%20%20%20%24%24R_T%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20w_i%20r_%7BT-i%7D%24%24%0A%0A%20%20%20%20Predict%20the%20(unknown)%20return%20%24R_T%24%20using%20the%20last%20%24n%24%20previous%20returns.%0A%20%20%20%20**Attention**%3A%20You%20may%20want%20to%20use%20volatility%20adjusted%20returns%2C%20apply%20filters%20etc.%0A%0A%20%20%20%20How%20to%20pick%20the%20%24n%24%20free%20parameters%20in%20%24%5Cmathbf%7Bw%7D%24%3F%20(Partial)%20autocorrelations%3F%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20convolution(ts%2C%20weights)%3A%0A%20%20%20%20%22%22%22Apply%20a%20convolution%20filter%20to%20a%20time%20series%20using%20specified%20weights.%0A%0A%20%20%20%20This%20function%20performs%20a%20one-sided%20convolution%20operation%20on%20a%20time%20series%2C%0A%20%20%20%20which%20is%20useful%20for%20creating%20autoregressive%20models%20and%20moving%20averages.%0A%0A%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20ts%3A%20The%20time%20series%20data%20to%20filter%20(pandas%20Series%20or%20array-like).%0A%20%20%20%20%20%20%20%20weights%3A%20The%20weights%20to%20use%20in%20the%20convolution%20filter.%0A%0A%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20A%20filtered%20time%20series%20resulting%20from%20the%20convolution%20operation.%0A%20%20%20%20%22%22%22%0A%20%20%20%20from%20statsmodels.tsa.filters.filtertools%20import%20convolution_filter%0A%0A%20%20%20%20return%20convolution_filter(ts%2C%20weights%2C%20nsides%3D1)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20_r%20%3D%20pd.Series(%5B1.0%2C%20-2.0%2C%201.0%2C%201.0%2C%201.5%2C%200.0%2C%202.0%5D)%0A%20%20%20%20_weights%20%3D%20%5B2.0%2C%201.0%5D%0A%20%20%20%20%23%20trendfollowing%20%3D%3D%20positive%20weights%0A%20%20%20%20_x%20%3D%20pd.DataFrame()%0A%20%20%20%20_x%5B%22r%22%5D%20%3D%20_r%0A%20%20%20%20_x%5B%22pred%22%5D%20%3D%20convolution(_r%2C%20_weights)%0A%20%20%20%20_x%5B%22before%22%5D%20%3D%20_x%5B%22pred%22%5D.shift(1)%0A%20%20%20%20print(_x)%0A%20%20%20%20print(_x.corr())%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20mean-reversion%20%3D%3D%20negative%20weights%0A%20%20%20%20_r%20%3D%20pd.Series(%5B1.0%2C%20-2.0%2C%201.0%2C%201.0%2C%201.5%2C%200.0%2C%202.0%5D)%0A%20%20%20%20_weights%20%3D%20%5B-2.0%2C%20-1.0%5D%0A%20%20%20%20_x%20%3D%20pd.DataFrame()%0A%20%20%20%20_x%5B%22r%22%5D%20%3D%20_r%0A%20%20%20%20_x%5B%22pred%22%5D%20%3D%20convolution(_r%2C%20_weights)%0A%20%20%20%20_x%5B%22before%22%5D%20%3D%20_x%5B%22pred%22%5D.shift(1)%0A%20%20%20%20print(_x)%0A%20%20%20%20print(_x.corr())%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Looking%20only%20at%20the%20last%20two%20returns%20might%20be%20a%20bit%20...%0A%0A%20%20%20%20Is%20it%20a%20good%20idea%20to%20have%20%24n%3D200%24%20free%20parameters%3F%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20generate%20random%20returns%0A%20%20%20%20r%20%3D%20(%0A%20%20%20%20%20%20%20%20pd.read_csv(%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.notebook_location()%20%2F%20%22public%22%20%2F%20%22SPX_Index.csv%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20index_col%3D0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20header%3DNone%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20parse_dates%3DTrue%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.pct_change()%0A%20%20%20%20%20%20%20%20.dropna()%5B1%5D%0A%20%20%20%20)%0A%20%20%20%20%23%20let's%20compute%20the%20optimal%20convolution!%0A%20%20%20%20weights%20%3D%20sts.pacf(r%2C%20nlags%3D200)%0A%0A%20%20%20%20%23%20Create%20a%20bar%20chart%20with%20plotly%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%20%20%20%20_fig.add_trace(go.Bar(x%3Dlist(range(1%2C%20len(weights)))%2C%20y%3Dweights%5B1%3A%5D))%0A%20%20%20%20_fig.update_layout(title%3D%22Partial%20Autocorrelation%22%2C%20xaxis_title%3D%22Lag%22%2C%20yaxis_title%3D%22PACF%22)%0A%20%20%20%20_fig%0A%20%20%20%20return%20r%2C%20weights%0A%0A%0A%40app.cell%0Adef%20_(r%2C%20weights)%3A%0A%20%20%20%20%23%20The%20trading%20system!%0A%20%20%20%20_pos%20%3D%20convolution(r%2C%20weights%5B1%3A%5D)%0A%20%20%20%20pos%20%3D%201e6%20*%20(_pos%20%2F%20_pos.std())%0A%20%20%20%20%23%20profit%20%3D%20return%5Btoday%5D%20*%20position%5Byesterday%5D%0A%0A%20%20%20%20%23%20Create%20a%20line%20chart%20with%20plotly%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%20%20%20%20_fig.add_trace(go.Scatter(x%3Dr.index%2C%20y%3D(r%20*%20pos.shift(1)).cumsum()%2C%20mode%3D%22lines%22))%0A%20%20%20%20_fig.update_layout(title%3D%22Cumulative%20Profit%22%2C%20xaxis_title%3D%22Time%22%2C%20yaxis_title%3D%22Profit%22)%0A%20%20%20%20_fig%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Bias%0A%0A%20%20%20%20We%20assume%20the%20weights%20are%20exponentially%20decaying%2C%20e.g.%0A%0A%20%20%20%20%24%24w_i%20%3D%20%5Cfrac%7B1%7D%7BS%7D%5Clambda%5Ei%24%24%0A%0A%20%20%20%20where%20%24S%24%20is%20a%20suitable%20scaling%20constant%20and%20%24%5Clambda%20%3D%201-1%2FN%24.%20Note%20that%20%24N%20%5Cneq%20n%24.%0A%0A%20%20%20%20**Everything**%20that%20is%20**not**%20an%20exponentially%20weighted%20moving%20average%20is%20**wrong**.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20exp_weights(m%2C%20n%3D100)%3A%0A%20%20%20%20%22%22%22Generate%20normalized%20exponentially%20decaying%20weights.%0A%0A%20%20%20%20This%20function%20creates%20a%20vector%20of%20exponentially%20decaying%20weights%20with%20decay%20rate%0A%20%20%20%20determined%20by%20parameter%20m.%20The%20weights%20are%20normalized%20to%20have%20unit%20norm.%0A%0A%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20m%3A%20The%20decay%20parameter%20controlling%20the%20rate%20of%20exponential%20decay.%0A%20%20%20%20%20%20%20%20%20%20%20Larger%20values%20of%20m%20result%20in%20slower%20decay.%0A%20%20%20%20%20%20%20%20n%3A%20The%20number%20of%20weights%20to%20generate.%20Defaults%20to%20100.%0A%0A%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20A%20numpy%20array%20of%20normalized%20exponentially%20decaying%20weights.%0A%20%20%20%20%22%22%22%0A%20%20%20%20x%20%3D%20np.power(1.0%20-%201.0%20%2F%20m%2C%20range(1%2C%20n%20%2B%201))%0A%20%20%20%20s%20%3D%20np.linalg.norm(x)%0A%20%20%20%20return%20x%20%2F%20s%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Create%20a%20bar%20chart%20with%20plotly%0A%20%20%20%20_weights%20%3D%20exp_weights(m%3D16%2C%20n%3D40)%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%20%20%20%20_fig.add_trace(go.Bar(x%3Dlist(range(1%2C%20len(_weights)%20%2B%201))%2C%20y%3D_weights))%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Exponential%20Weights%20(m%3D16%2C%20n%3D40)%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Index%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Weight%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20periods%20%3D%20%5B2%2C%204%2C%206%2C%208%2C%2012%2C%2016%2C%2024%2C%2032%2C%2048%2C%2064%2C%2096%2C%20192%5D%0A%20%20%20%20%23%20matrix%20of%20weights%0A%20%20%20%20w_matrix%20%3D%20pd.DataFrame(%7B_period%3A%20exp_weights(m%3D_period%2C%20n%3D200)%20for%20_period%20in%20periods%7D)%0A%0A%20%20%20%20%23%20Create%20a%20line%20chart%20with%20plotly%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%20%20%20%20for%20_period%20in%20periods%3A%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dlist(range(1%2C%20201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dw_matrix%5B_period%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Period%20%7B_period%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Exponential%20Weights%20for%20Different%20Periods%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Index%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Weight%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig%0A%0A%20%20%20%20return%20periods%2C%20w_matrix%0A%0A%0A%40app.cell%0Adef%20_(periods%2C%20r%2C%20w_matrix)%3A%0A%20%20%20%20%23%20each%20column%20of%20a_matrix%20is%20a%20convoluted%20return%20time%20series%0A%20%20%20%20a_matrix%20%3D%20pd.DataFrame(%7B_period%3A%20convolution(r%2C%20w_matrix%5B_period%5D).shift(1)%20for%20_period%20in%20periods%7D)%0A%0A%20%20%20%20a_matrix%20%3D%20a_matrix.dropna(axis%3D0)%0A%20%20%20%20r_filtered%20%3D%20r%5Ba_matrix.index%5D.dropna()%0A%0A%20%20%20%20%23%20Create%20a%20line%20chart%20with%20plotly%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%20%20%20%20for%20_period%20in%20%5B2%2C%2016%2C%2064%5D%3A%0A%20%20%20%20%20%20%20%20_fig.add_trace(go.Scatter(x%3Da_matrix.index%2C%20y%3Da_matrix%5B_period%5D%2C%20mode%3D%22lines%22%2C%20name%3Df%22Period%20%7B_period%7D%22))%0A%20%20%20%20_fig.update_layout(title%3D%22Convoluted%20Return%20Time%20Series%22%2C%20xaxis_title%3D%22Date%22%2C%20yaxis_title%3D%22Value%22)%0A%20%20%20%20_fig%0A%0A%20%20%20%20return%20a_matrix%2C%20r_filtered%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20(Naive)%20regression%0A%0A%20%20%20%20%5Cbegin%7Balign%7D%0A%20%20%20%20%5Cmathbf%7Bw%7D%5E%7B*%7D%3D%5Carg%5Cmin_%7B%5Cmathbf%7Bw%7D%20%5Cin%20%5Cmathbb%7BR%7D%5Em%7D%26%20%5CrVert%7B%5Cmathbf%7BA%7D%5Cmathbf%7Bw%7D%20-%20%5Cmathbf%7Br%7D%7D%5ClVert_2%0A%20%20%20%20%5Cend%7Balign%7D%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(a_matrix%2C%20periods%2C%20r_filtered%2C%20w_matrix)%3A%0A%20%20%20%20%23%20sometimes%20you%20don't%20need%20to%20use%20MOSEK%20%3A-)%0A%20%20%20%20_weights%20%3D%20pd.Series(index%3Dperiods%2C%20data%3Dlstsq(a_matrix.values%2C%20r_filtered.values)%5B0%5D)%0A%20%20%20%20print(_weights)%0A%0A%20%20%20%20%23%20Create%20bar%20chart%0A%20%20%20%20_fig1%20%3D%20go.Figure()%0A%20%20%20%20_fig1.add_trace(go.Bar(x%3D_weights.index.astype(str)%2C%20y%3D(w_matrix%20*%20_weights).sum(axis%3D1)))%0A%20%20%20%20_fig1.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Weights%20Distribution%20(Bar%20Chart)%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Period%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Weight%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig1.show()%0A%0A%20%20%20%20%23%20Create%20line%20chart%0A%20%20%20%20_fig2%20%3D%20go.Figure()%0A%20%20%20%20_fig2.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dlist(range(1%2C%20len((w_matrix%20*%20_weights).sum(axis%3D1))%20%2B%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D(w_matrix%20*%20_weights).sum(axis%3D1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20_fig2.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Weights%20Distribution%20(Line%20Chart)%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Index%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Weight%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig2.show()%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Mean%20variation%0A%0A%20%20%20%20We%20provide%20a%20few%20indicators.%20Avoid%20fast%20indicators.%20Prefer%20slower%20indicators%20as%20they%20induce%20less%20trading%20costs.%0A%20%20%20%20Use%20the%20mean%20variation%20of%20the%20signal%20(convoluted%20returns%20here)%0A%0A%20%20%20%20%24%24f(%5Cmathbf%7Bx%7D)%20%3D%20%5Cfrac%7B1%7D%7Bn%7D%5Csum%7B%5Clvert%20x_i%20-%20x_%7Bi-1%7D%5Crvert%7D%3D%5Cfrac%7B1%7D%7Bn%7D%5CrVert%7B%5CDelta%20%5Cmathbf%7Bx%7D%7D%5ClVert_1%24%24%0A%0A%20%20%20%20The%20%24i%24th%20column%20of%20%24%5Cmathbf%7BA%7D%24%20has%20a%20mean%20variation%20%24d_i%24.%0A%20%20%20%20We%20introduce%20the%20diagonal%20penalty%20matrix%20%24%5Cmathbf%7BD%7D%24%20with%20%24D_%7Bi%2Ci%7D%3Dd_i%24.%0A%0A%20%20%20%20%24%24%5Cmathbf%7Bw%7D%5E%7B*%7D%3D%5Carg%5Cmin_%7B%5Cmathbf%7Bw%7D%20%5Cin%20%5Cmathbb%7BR%7D%5Em%7D%20%5ClVert%7B%5Cmathbf%7BAw%7D-%5Cmathbf%7Br%7D%7D%5ClVert_2%20%2B%0A%20%20%20%20%5Clambda%20%5CrVert%7B%5Cmathbf%7BDw%7D%7D%5ClVert_1%24%24%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20minimize(objective%2C%20constraints%3DNone)%3A%0A%20%20%20%20%22%22%22Minimizes%20a%20given%20objective%20function%20subject%20to%20optional%20constraints.%0A%0A%20%20%20%20This%20function%20creates%20and%20solves%20a%20convex%20optimization%20problem%20to%20find%20the%0A%20%20%20%20minimum%20value%20of%20the%20provided%20objective%20function%2C%20subject%20to%20any%20specified%0A%20%20%20%20constraints.%0A%0A%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20objective%3A%20The%20objective%20function%20to%20minimize.%0A%20%20%20%20%20%20%20%20constraints%3A%20Optional%20list%20of%20constraints%20for%20the%20optimization%20problem.%0A%0A%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20The%20optimal%20value%20of%20the%20objective%20function.%0A%20%20%20%20%22%22%22%0A%20%20%20%20return%20cvx.Problem(cvx.Minimize(objective)%2C%20constraints).solve()%0A%0A%0A%40app.function%0Adef%20mean_variation(ts)%3A%0A%20%20%20%20%22%22%22Calculate%20the%20mean%20absolute%20difference%20between%20consecutive%20values%20in%20a%20time%20series.%0A%0A%20%20%20%20This%20function%20computes%20the%20average%20of%20absolute%20differences%20between%20adjacent%0A%20%20%20%20elements%20in%20a%20time%20series%2C%20which%20is%20a%20measure%20of%20the%20series'%20variability.%0A%0A%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20ts%3A%20A%20time%20series%20(pandas%20Series%20or%20array-like).%0A%0A%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20The%20mean%20variation%20(average%20absolute%20difference)%20of%20the%20time%20series.%0A%20%20%20%20%22%22%22%0A%20%20%20%20return%20ts.diff().abs().mean()%0A%0A%0A%40app.function%0Adef%20ar(a_matrix%2C%20r%2C%20lamb%3D0.0)%3A%0A%20%20%20%20%22%22%22Fit%20an%20autoregressive%20model%20with%20regularization%20based%20on%20signal%20variation.%0A%0A%20%20%20%20This%20function%20solves%20a%20regularized%20regression%20problem%20to%20find%20optimal%20weights%0A%20%20%20%20for%20an%20autoregressive%20model.%20The%20regularization%20penalizes%20weights%20for%20signals%0A%20%20%20%20with%20high%20mean%20variation%2C%20favoring%20smoother%20signals.%0A%0A%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20a_matrix%3A%20Matrix%20of%20predictor%20signals%2C%20where%20each%20column%20is%20a%20different%20signal.%0A%20%20%20%20%20%20%20%20r%3A%20Target%20return%20series%20to%20predict.%0A%20%20%20%20%20%20%20%20lamb%3A%20Regularization%20parameter%20controlling%20the%20penalty%20on%20signal%20variation.%0A%20%20%20%20%20%20%20%20%20%20%20%20Higher%20values%20result%20in%20smoother%20weights.%20Defaults%20to%200.0.%0A%0A%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20A%20pandas%20Series%20containing%20the%20optimal%20weights%20for%20each%20predictor%20signal.%0A%20%20%20%20%22%22%22%0A%20%20%20%20%23%20introduce%20the%20variable%20for%20the%20var%0A%20%20%20%20x%20%3D%20cvx.Variable(a_matrix.shape%5B1%5D)%0A%20%20%20%20d%20%3D%20np.diag(a_matrix.apply(mean_variation))%0A%20%20%20%20minimize(objective%3Dcvx.norm(a_matrix.values%20%40%20x%20-%20r%2C%202)%20%2B%20lamb%20*%20cvx.norm(d%20%40%20x%2C%201))%0A%20%20%20%20return%20pd.Series(index%3Da_matrix.keys()%2C%20data%3Dx.value)%0A%0A%0A%40app.cell%0Adef%20_(a_matrix%2C%20r_filtered%2C%20w_matrix)%3A%0A%20%20%20%20t_weight%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20_lamb%3A%20(w_matrix%20*%20ar(a_matrix%2C%20r_filtered.values%2C%20lamb%3D_lamb)).sum(axis%3D1)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20_lamb%20in%20%5B0.0%2C%201.0%2C%202.0%2C%203.0%2C%205.0%2C%207.0%2C%209.0%2C%2012.0%2C%2015.0%5D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Create%20a%20line%20chart%20with%20plotly%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%20%20%20%20for%20_lamb%20in%20%5B0.0%2C%205.0%2C%2015.0%5D%3A%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dlist(range(1%2C%20len(t_weight)%20%2B%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dt_weight%5B_lamb%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Lambda%20%7B_lamb%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Weight%20Distribution%20for%20Different%20Lambda%20Values%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Index%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Weight%22%2C%0A%20%20%20%20%20%20%20%20width%3D1200%2C%0A%20%20%20%20%20%20%20%20height%3D400%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.show()%0A%0A%20%20%20%20return%20(t_weight%2C)%0A%0A%0A%40app.cell%0Adef%20_(r%2C%20t_weight)%3A%0A%20%20%20%20%23%20for%20lamb%20in%20sorted(_t_weight.keys())%3A%0A%0A%20%20%20%20_pos%20%3D%20pd.DataFrame(%7B_lamb%3A%20convolution(r%2C%20t_weight%5B_lamb%5D)%20for%20_lamb%20in%20t_weight%7D)%0A%20%20%20%20_pos%20%3D%201e6%20*%20(_pos%20%2F%20_pos.std())%0A%0A%20%20%20%20_profit%20%3D%20pd.DataFrame(%7Blamb%3A%20(r%20*%20_pos%5Blamb%5D.shift(1)).cumsum()%20for%20lamb%20in%20_pos%7D)%0A%0A%20%20%20%20%23%20Create%20a%20line%20chart%20with%20plotly%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%20%20%20%20for%20_lamb%20in%20%5B0.0%2C%205.0%2C%2015.0%5D%3A%0A%20%20%20%20%20%20%20%20_fig.add_trace(go.Scatter(x%3D_profit.index%2C%20y%3D_profit%5B_lamb%5D%2C%20mode%3D%22lines%22%2C%20name%3Df%22Lambda%20%7B_lamb%7D%22))%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Cumulative%20Profit%20for%20Different%20Lambda%20Values%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Date%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Profit%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.show()%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Summary%0A%0A%20%20%20%20-%20The%20problem%20of%20constructing%20an%20estimator%20is%20corresponds%20to%20tracking%20an%20index.%0A%20%20%20%20%20%20The%20index%20is%20here%20a%20historic%20return%20time%20series.%0A%20%20%20%20%20%20The%20**assets**%20are%20standard%20estimators.%0A%0A%0A%20%20%20%20-%20Using%20the%20(mean)%20total%20variation%20of%20the%20signals%20can%20help%20to%20prefer%20slower%20signals%20rather%20than%0A%20%20%20%20%20%20expensive%20fast%20signals.%0A%0A%0A%20%20%20%20-%20Using%20a%20penalty%20induced%20by%20the%20%241%24-norm%20(see%20LARS%2C%20LASSO)%20it%20is%20possible%20to%20establish%20a%20ranking%0A%20%20%20%20%20%20amongst%20the%20indicators%20and%20construct%20them%20robustly.%0A%0A%0A%20%20%20%20-%20It%20is%20possible%20to%20(vertical)%20stack%20the%20resulting%20systems%20to%20find%20optimal%20weights%20across%20a%20group%20of%20assets.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
42b5dfc9734221db0ed2b8a391b875fd24ed724f508b217e477123ab2550b6ea