piecewise

Python — confused by numpy's piecewise function

馋奶兔 提交于 2019-12-11 04:19:56
问题 I'm trying to implement a piecewise function in Python. Since I'm using quite a few tools from numpy, I simply import everything from it (i.e. from numpy import * ). My piecewise function is defined as LinQuad = piecewise( t, [t < 1, t >= 1], [lambda t : t, lambda t : t**2] ) which results in the error NameError: global name 't' is not defined . I don't understand why I should define t — after all, it is not necessary to define t for a simple lambda function Lin = lambda t : t . In some

How to implement a piecewise function and then plot it on certain intervals in MATLAB

≯℡__Kan透↙ 提交于 2019-12-11 04:08:21
问题 I am actually attempting to write code for the cubic spline interpolation. Cubic spline boils down to a series of n-1 segments where n is the number of original coordinates given initially and the segments are each represented by some cubic function. I have figured out how to get all the coefficients and values for each segment, stored in vectors a,b,c,d , but I don't know how to plot the function as a piecewise function on different intervals. Here is my code so far. The very last for loop

How can I obtain segmented linear regressions with a priori breakpoints?

泄露秘密 提交于 2019-12-09 22:33:50
问题 I need to explain this in excruciating detail because I don't have the basics of statistics to explain in a more succinct way. Asking here in SO because I am looking for a python solution, but might go to stats.SE if more appropriate. I have downhole well data, it might be a bit like this: Rt T 0.0000 15.0000 4.0054 15.4523 25.1858 16.0761 27.9998 16.2013 35.7259 16.5914 39.0769 16.8777 45.1805 17.3545 45.6717 17.3877 48.3419 17.5307 51.5661 17.7079 64.1578 18.4177 66.8280 18.5750 111.1613 19

Maxima - differentiating a piecewise function

百般思念 提交于 2019-12-07 19:03:16
问题 Suppose you have a function defined by intervals, such as f(x):=block(if x<0 then x^2 else x^3); When we differentiate it with diff(f(x),x); we get d/dx (if x<0 then x^2 else x^3) whereas I'd like to get (if x<0 then 2*x else 3*x^2) Is there a way to obtain such result? 回答1: This may help in a simple case: (%i1) f(x):= charfun(x<0)*x^2 + charfun(x>=0)*x^3$ (%i2) gradef(charfun(y), 0)$ (%i3) diff(f(x),x); 2 (%o3) 2 x charfun(x < 0) + 3 x charfun(x >= 0) charfun, gradef You can try also Pw.mac

Maxima - differentiating a piecewise function

醉酒当歌 提交于 2019-12-06 05:56:30
Suppose you have a function defined by intervals, such as f(x):=block(if x<0 then x^2 else x^3); When we differentiate it with diff(f(x),x); we get d/dx (if x<0 then x^2 else x^3) whereas I'd like to get (if x<0 then 2*x else 3*x^2) Is there a way to obtain such result? This may help in a simple case: (%i1) f(x):= charfun(x<0)*x^2 + charfun(x>=0)*x^3$ (%i2) gradef(charfun(y), 0)$ (%i3) diff(f(x),x); 2 (%o3) 2 x charfun(x < 0) + 3 x charfun(x >= 0) charfun , gradef You can try also Pw.mac package from Richard Hennessy. Here's a different approach using a simplification rule for "if" expressions

Piecewise regression with a straight line and a horizontal line joining at a break point

☆樱花仙子☆ 提交于 2019-12-06 03:09:42
问题 I want to do a piecewise linear regression with one break point, where the 2nd half of the regression line has slope = 0 . There are examples of how to do a piecewise linear regression, such as here. The problem I'm having is I'm not clear how to fix the slope of half of the model to be 0. I tried lhs <- function(x) ifelse(x < k, k-x, 0) rhs <- function(x) ifelse(x < k, 0, x-k) fit <- lm(y ~ lhs(x) + rhs(x)) where k is the break point, but the segment on the right is not a flat / horizontal

How can I obtain segmented linear regressions with a priori breakpoints?

好久不见. 提交于 2019-12-04 18:02:19
I need to explain this in excruciating detail because I don't have the basics of statistics to explain in a more succinct way. Asking here in SO because I am looking for a python solution, but might go to stats.SE if more appropriate. I have downhole well data, it might be a bit like this: Rt T 0.0000 15.0000 4.0054 15.4523 25.1858 16.0761 27.9998 16.2013 35.7259 16.5914 39.0769 16.8777 45.1805 17.3545 45.6717 17.3877 48.3419 17.5307 51.5661 17.7079 64.1578 18.4177 66.8280 18.5750 111.1613 19.8261 114.2518 19.9731 121.8681 20.4074 146.0591 21.2622 148.8134 21.4117 164.6219 22.1776 176.5220 23

Piecewise regression with a straight line and a horizontal line joining at a break point

99封情书 提交于 2019-12-04 07:14:52
I want to do a piecewise linear regression with one break point, where the 2nd half of the regression line has slope = 0 . There are examples of how to do a piecewise linear regression, such as here . The problem I'm having is I'm not clear how to fix the slope of half of the model to be 0. I tried lhs <- function(x) ifelse(x < k, k-x, 0) rhs <- function(x) ifelse(x < k, 0, x-k) fit <- lm(y ~ lhs(x) + rhs(x)) where k is the break point, but the segment on the right is not a flat / horizontal one. I want to constrain the slope of the second segment at 0. I tried: fit <- lm(y ~ x * (x < k) + x *

Piecewise regression with R: plotting the segments

房东的猫 提交于 2019-12-03 02:26:09
问题 I have 54 points. They represent offer and demand for products. I would like to show there is a break point in the offer. First, I sort the x-axis (offer) and remove the values that appears twice. I have 47 values, but I remove the first and last ones (doesn't make sense to consider them as break points). Break is of length 45: Break<-(sort(unique(offer))[2:46]) Then, for each of these potential break points, I estimate a model and I keep in "d" the residual standard error (sixth element in

Piecewise regression with R: plotting the segments

戏子无情 提交于 2019-12-02 15:57:53
I have 54 points. They represent offer and demand for products. I would like to show there is a break point in the offer. First, I sort the x-axis (offer) and remove the values that appears twice. I have 47 values, but I remove the first and last ones (doesn't make sense to consider them as break points). Break is of length 45: Break<-(sort(unique(offer))[2:46]) Then, for each of these potential break points, I estimate a model and I keep in "d" the residual standard error (sixth element in model summary object). d<-numeric(45) for (i in 1:45) { model<-lm(demand~(offer<Break[i])*offer + (offer