importfastloessasflimportnumpyasnpimportmatplotlib.pyplotasplt# Simulate noisy time series with trendnp.random.seed(42)t=np.linspace(0,100,500)trend=10+0.5*t+3*np.sin(t/10)noise=np.random.normal(0,3,len(t))y=trend+noise# Extract trend with LOESSmodel=fl.Loess(fraction=0.1,iterations=3)result=model.fit(t,y)# Plotplt.figure(figsize=(12,5))plt.plot(t,y,"gray",alpha=0.5,label="Observed")plt.plot(t,result.y,"b-",linewidth=2,label="Trend (LOESS)")plt.xlabel("Time")plt.ylabel("Value")plt.legend()plt.title("Trend Extraction")plt.show()
usefastLoess::prelude::*;letn=500usize;lett:Vec<f64>=(0..n).map(|i|iasf64*100.0/(n-1)asf64).collect();lety:Vec<f64>=t.iter().enumerate().map(|(i,&ti)|10.0+0.5*ti+3.0*(ti/10.0).sin()+((i*7+3)asf64%1.7-0.85)*3.0).collect();letmodel=Loess::new().fraction(0.1).iterations(3).build()?;letresult=model.fit(&t,&y)?;// result.y contains the trend
constfl=require('fastloess');// t and y are your time series arrays (Float64Array)constmodel=newfl.Loess({fraction:0.1,iterations:3});constresult=model.fit(t,y);console.log("Extracted trend:",result.y);
import{Loess}from'fastloess-wasm';constmodel=newLoess({fraction:0.1,iterations:3});constresult=model.fit(t,y);// Trend values in result.y
fastloess::LoessOptionstrend_opts;trend_opts.fraction=0.1;trend_opts.iterations=3;fastloess::Loessbasic_model(trend_opts);autobasic_result=basic_model.fit(t,y).value();// Trend in basic_result.y_vector()
# Smooth to get trendmodel=fl.Loess(fraction=0.3,iterations=3,return_residuals=True)result=model.fit(t,y)trend=result.ydetrended=result.residuals# Analyze residuals for seasonality, etc.plt.figure(figsize=(12,4))plt.subplot(1,2,1)plt.plot(t,trend)plt.title("Extracted Trend")plt.subplot(1,2,2)plt.plot(t,detrended)plt.title("Detrended (Residuals)")plt.tight_layout()
# Smooth to get trend and residualsmodel=Loess(;fraction=0.3,iterations=3,return_residuals=true)result=fit(model,t,y)trend=result.ydetrended=result.residualsprintln("Detrended variance: ",var(detrended))
model=fl.Loess(fraction=0.2,iterations=3,confidence_intervals=0.95,prediction_intervals=0.95)result=model.fit(t,y)# Plot with uncertainty bandsplt.figure(figsize=(12,5))plt.plot(t,y,"gray",alpha=0.3)plt.plot(t,result.y,"b-",linewidth=2,label="Trend")plt.fill_between(t,result.prediction_lower,result.prediction_upper,alpha=0.2,color="blue",label="95% Prediction")plt.legend()
letmodel=Loess::new().fraction(0.2).iterations(3).confidence_intervals(0.95).prediction_intervals(0.95).build()?;letresult=model.fit(&t,&y)?;// Access result.prediction_lower and result.prediction_upper
model=Loess(;fraction=0.2,iterations=3,confidence_intervals=0.95,prediction_intervals=0.95)result=fit(model,t,y)# Intervals are available in result.prediction_lower/upperprintln("First point 95% PI: [$(result.prediction_lower[1]), $(result.prediction_upper[1])]")
import{Loess}from'fastloess-wasm';constmodel=newLoess({fraction:0.2,iterations:3,prediction_intervals:0.95});constresult=model.fit(t,y);// Access result.prediction_lower and result.prediction_upper
#include"fastloess.hpp"fastloess::Loessforecast_model({.fraction=0.2,.iterations=3,.confidence_intervals=0.95,.prediction_intervals=0.95});autoresult=forecast_model.fit(t,y).value();// Access result.prediction_lower() and result.prediction_upper()
# Irregular time points (gaps in data)t_irregular=np.sort(np.random.uniform(0,100,200))y_irregular=10+t_irregular*0.3+np.random.normal(0,2,200)# LOESS handles this seamlesslymodel=fl.Loess(fraction=0.2)result=model.fit(t_irregular,y_irregular)
// Irregular sampling - no special handling neededletmodel=Loess::new().fraction(0.2).build()?;letresult=model.fit(&t_irregular,&y_irregular)?;
# Irregular time points (gaps in data)t_irregular=sort(rand(200).*100.0)y_irregular=10.0.+t_irregular.*0.3.+randn(200).*2.0# LOESS handles this seamlesslymodel=Loess(;fraction=0.2)result=fit(model,t_irregular,y_irregular)
constfl=require('fastloess');// No special handling needed for irregular spacingconstmodel=newfl.Loess({fraction:0.2});constresult=model.fit(tIrregular,yIrregular);
letfractions=[0.05,0.2,0.5];forfinfractions{letmodel=Loess::new().fraction(f).build()?;letresult=model.fit(&t,&y)?;// Store or plot result.y for each scale}
fractions=[0.05,0.2,0.5]results=[fit(Loess(;fraction=f),t,y)forfinfractions]# results[i].y contains smoothed values for each fraction